4

i need to convert date string "28/01/2018" (dd/mm/yyyy) into a Date() in qml.

i'm tried this:

  var dateBoard = masterPAGEMAIN.getData();

  var locale = Qt.locale()

  var someDateTest = new Date()
  someDateTest = Date.fromLocaleString(locale, dateBoard, "dd/MM/yyyy");
  var test = someDateTest.getDate().toString();

Also saw this: conversione from string , but my problem is that i continue to receive a "NaN" or "Invalid Date", how to can i get Date() from string in qml ?

Thanks

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110

1 Answers1

5

The string passed to fromLocaleString must be in the expected format. Try this code:

var dateBoard = "01/31/2018"
var someDateTest = Date.fromLocaleString(Qt.locale(), dateBoard, "dd/MM/yyyy")
var test = someDateTest.getDate() //nan

Since the string in dateBoard represents a date in MM/dd/yyyy format, fromLocaleString will return an invalid date and getDate nan, accordingly.

Same applies if dateBoard is an empty string, null or undefined.

NG_
  • 6,895
  • 7
  • 45
  • 67
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35