Ho to Parse STRING to Date/Time field?

hyperjam
Giga Contributor

Hello, i have an inbound email action to create a new records from emails.

The email body_test contains string date, presented in such way, for example - Thursday, July 14, 2016 11:00:43 PM

I'm extracting that date, and i need it to be inserted in the DATE/Time field [u_date] (so for example -   2016-07-14 23:00:43)

Do you have suggestion on how to do that?

Any input will be welcomed, because i'm lost with that

Thank you!

1 ACCEPTED SOLUTION

Oh wow, that is a lot of coding. If I am not wrong, this can be achieved in 4 lines


var sec=Date.parse("Thursday, July 14, 2016 11:00:43 PM");


var gdt = new GlideDateTime();


gdt.setNumericValue(sec);


gdt.getDisplayValue(); //this will give you the date in your format


View solution in original post

25 REPLIES 25

Here you go. Had to do some manipulation with the time zones



var str = "Thursday, July 14, 2016 11:00:43 PM";


var sec='';


if(str.toUpperCase().indexOf("PM")>-1){


var date= new Date(str);


str=str.replace(/\s[0-9][0-9]:/g,' '+date.getHours()+':').replace(/PM/gi,'GMT');


sec=Date.parse(str);


}


if(str.toUpperCase().indexOf("AM")>-1){


str=str.replace(/AM/gi,'GMT');


sec=Date.parse(str);


gs.print(sec);


}


var gdt = new GlideDateTime();


gdt.setNumericValue(sec);


gdt.getValue(); //this will get you the desired value


Hey Abhinay E and Chuck,




Can you help with this one also. Everytime they update the record they will send one splited line abnd I need to get latest date from this code. For example




First udpated date: 2017-10-25 1:00:00


Next update: 2017-10-25 1:02:00


Next update: 2017-10-25 1:04:0


Next update: 2017-10-25 1:04:00






PRD Example 0000001234 @ 2017-10-25 1:00:00 (   )


This is test record 1




PRD Example 0000001235 @ 2017-10-25 1:02:00 (   )


This is test record 2






PRD Example 0000001252 @ 2017-10-25 1:04:00 (   )


This is test record 3






PRD Example 0000001259 @ 2017-10-25 1:04:00 (   )


This is test record 4






Appreciate your help in advance.


Your method doesn't work for me at all. The system has a mind of it's own! I need better control of the process, so I'm going to try with the 50 line version.

We have date String field as "20070213" (yyyymmdd) coming from csv and need to convert into date field in Service Now.

Below code is giving null pointer exception. Please suggest. Thank you.

var sec=Date.parse("20070213");

var gdt = new GlideDateTime();

gdt.setNumericValue(sec);

gdt.getDisplayValue();

verda
Mega Expert

How do you parse this: 20200722T080000