Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Date() problem in Javascript/QML



  • I have a small problem with Javascript/QML.

    I'll format a date. When I'am testing this script with tryit, all is ok. I get as result "Mi, 11.5.2016".

    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo"></p>
    
    <script>
    function formatEndTime(dateString) {
        var newDate = new Date(dateString);
        var TagInWoche = newDate.getDay();
        var Wochentag = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"];
        var showDate = Wochentag[TagInWoche] + ", " + newDate.getDate() + "." + (newDate.getMonth() +1 ) + "." + newDate.getFullYear();
        return showDate;
    }
    var rohdatum = "2016-05-11";
    
    document.getElementById("demo").innerHTML = formatEndTime(rohdatum);
    
    </script>
    
    </body>
    </html>
    
    

    In Qt I use a similar Javascript function

    function formatEndTime(dateString) {
        var newDate = new Date(dateString);
        var TagInWoche = newDate.getDay();
        var Wochentag = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"];
        var showDate = Wochentag[TagInWoche] + ", " + newDate.getDate() + "." + (newDate.getMonth() +1 ) + "." + newDate.getFullYear();
        return showDate;
    }
    

    But here I get an error for newDate: "Invalid Date"
    (dateString is "2016-05-11")

    What's wrong?
    Olaf


  • Moderators

    @o.coder It works with Qt 5.6. Which version are you using ?



  • Hallo! It works for me:

    import QtQuick 2.6
    import QtQuick.Window 2.0
    import QtQuick.Controls 1.4
    
    ApplicationWindow {
        id: mainWindow
        visible: true
        width: 500
        height: 500
    
        function formatEndTime(dateString) {
            var newDate = new Date(dateString);
            var TagInWoche = newDate.getDay();
            var Wochentag = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"];
            var showDate = Wochentag[TagInWoche] + ", " + newDate.getDate() + "." + (newDate.getMonth() +1 ) + "." + newDate.getFullYear();
            return showDate;
        }
    
        Text {
            anchors.centerIn: parent
            font.pixelSize: 20
            text: formatEndTime("2016-05-11")
        }
    }
    


  • With Qt 4.8.4...


  • Moderators

    @o.coder That's quite old. May be it doesnot support dateString format.
    Try with other constructors like
    var newDate = new Date("2016", "05", "11");
    Also if possible you to migrate to Qt5 so that you get all latest JavaScript functions support.


Log in to reply