cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

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

MD_10945249
4-Participant

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

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.

ACCEPTED SOLUTION

Accepted Solutions

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");

 

View solution in original post

3 REPLIES 3

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");

 

MD_10945249
4-Participant
(To:Rocko)

Thank you @Rocko this does work.

Alternatively you can do this:

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


Top Tags