Skip to main content
4-Participant
November 27, 2024
Solved

using "new Date" function of javascript in my service is not providing me with the desired output

  • November 27, 2024
  • 2 replies
  • 809 views

I have this function which takes lastVisitTime of type string.

 

string date value is "2024-11-12 10:00:01"
result of code when compiled on js compiler

let dateFromString = new Date("date in string");

output -> 

2024-11-12T10:00:01.000Z

 output type is 

object

result of code when thingworx service is executed

let dateFromString = new Date("date in string");

output -> invalid date

output type is 

object

 could someone help me with this I want to convert my date string to date.

Thanks in advance.

Best answer by Rocko

When you give a string to new Date(), it must be in  "YYYY-MM-DDTHH:mm:ss.sssZ" format.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format

Try replacing the space with a T:

2024-11-12 10:00:01 -> 2024-11-12T10:00:01 -> new Date("2024-11-12T10:00:01");

 

2 replies

Rocko
Rocko19-TanzaniteAnswer
19-Tanzanite
November 27, 2024

When you give a string to new Date(), it must be in  "YYYY-MM-DDTHH:mm:ss.sssZ" format.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format

Try replacing the space with a T:

2024-11-12 10:00:01 -> 2024-11-12T10:00:01 -> new Date("2024-11-12T10:00:01");

 

4-Participant
November 28, 2024

Thank you @Rocko this does work.

18-Opal
November 28, 2024

Alternatively you can do this:

let date = parseDate("2024-11-12 10:00:01", "YYYY-MM-dd HH:mm:ss");