Date() problem in Javascript/QML
-
wrote on 11 May 2016, 08:04 last edited by
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 -
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@o.coder It works with Qt 5.6. Which version are you using ?
-
wrote on 11 May 2016, 08:30 last edited by
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") } }
-
wrote on 11 May 2016, 08:42 last edited by
With Qt 4.8.4...
-
@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.
1/5