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

Abhinay Erra
Giga Sage

Anna,



  Wow!! I do not know why you want achieve this in 50 lines, when you can achieve this in 4 lines!!!


I agree with Abhinay on this one, Anna. Please mark his answer as correct so that others can use the most efficient method possible if they have a similar question/issue in the future.


Okay Chuck,



but maybe you know what causes the change of the initial date from my mail?



The solution proposed 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 isThursday, July 14, 2016 11:00:43 PM)


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


When you do getDisplayValue() it will convert that to date/time in user timeZone(). If you want the exact date/time in the email. go with chuck's solution by parsing the actual values


It is applying the timezone.



When you use



gdt.getDisplayValue();



the timezone is applied. If you use



gdt.getValue();



no timezone. It's just a matter of which one you want to store.



GlideDateTime - ServiceNow Wiki