Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Date() problem in Javascript/QML

Date() problem in Javascript/QML

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    o.coder
    wrote on 11 May 2016, 08:04 last edited by
    #1

    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

    P 1 Reply Last reply 11 May 2016, 08:28
    0
    • O o.coder
      11 May 2016, 08:04

      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

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 11 May 2016, 08:28 last edited by
      #2

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

      157

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 11 May 2016, 08:30 last edited by
        #3

        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")
            }
        }
        
        1 Reply Last reply
        0
        • O Offline
          O Offline
          o.coder
          wrote on 11 May 2016, 08:42 last edited by
          #4

          With Qt 4.8.4...

          P 1 Reply Last reply 11 May 2016, 09:14
          0
          • O o.coder
            11 May 2016, 08:42

            With Qt 4.8.4...

            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 11 May 2016, 09:14 last edited by
            #5

            @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.

            157

            1 Reply Last reply
            0

            1/5

            11 May 2016, 08:04

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved