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

Well at least for once, I won


I sort of felt like I was writing an exercise for one of my old freshmen computer science courses.


Been there, done that


Abhinary Hello,



thank you for reply. I have one question to the result of your code,


It is keeping to change the date and time of converted string, i.e. for the date which i get from my email (in my example email it is Thursday, July 14, 2016 11:00:43 PM)


the resulting DATE/TIME record is - 2016-07-15 08:00:43



Maybe you can suggest how to prevent the change of the initial date and time from string?



also,



the email is:


Hi Team,


tests are as follows:


Thursday, July 14, 2016 11:00:43 PM


Check 1


Browse Site http://


Try 1


............Title of page returned: Example


Elapsed time 17.0622773 seconds ...




this is how i'm getting the date/time


var testDate = email.body_text.substring(email.body_text.indexOf('follows:')+8, email.body_text.indexOf(' PM')+3).trim();


current.u_date = testdate;


Hi Abhinay,



I tried the following code in Scripts-Background in Kingston version:



var t = Date.parse("2017-12-26T19:50:38.794+05:30")


gs.info(t);



It gives me NaN.



How do I parse this date with timezone and create GlideDateTime object from it?



Thanks!