How to convert date format "Day, Month Date, Year HH:SS AM to date/time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2017 02:04 PM
Hello all,
I have a requirement to read Sent: information in email body and convert it into date time value.
For example, Below is the value that comes in email body.
Sent: Friday, August 18, 2017 8:00 AM
I need to convert above info into date/time value i.e, MM-DD-YYYY HH:MM:SS format.
Appreciate any help with this.
Thanks
Mahduri

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2017 09:11 PM
So, GlideDateTime can do everything except parsing the AM/PM and day.
This would be really easy if it wasn't for the AM/PM Timestamp!
function convert12HourTo24(time) {
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ":" + sMinutes;
}
var testDate = 'Sent: Friday, August 18, 2017 8:00 AM';
//Remove Sent: and named day part, irelevant
var dateNoDay = testDate.substr(testDate.indexOf(", ") + 1); // August 18, 2017 8:00 AM;
//Convert to 24/7 time
var dateTime = dateNoDay.substr(dateNoDay.indexOf(":") - 1) ; //8:00 AM
var dateTime24 = convert12HourTo24(dateTime).trim(); //08:00
//Get first part
var firstPart = dateNoDay.substr(0, dateNoDay.indexOf(":") - 1); //August 18, 2017
//Add 24 datet/time to the end
var newDate = firstPart.trim() + ' ' + dateTime24.trim(); // August 18, 2017 08:00
//Use GDT
var gdt = new GlideDateTime();gdt.setDisplayValue(newDate, "MMMM dd, yyyy hh:mm");
var dateTimeForField = gdt.getDisplayValue(); //2017-08-18 08:00:00
current.u_datetime = dateTimeForField;
2017-08-18 08:00:00
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2017 12:52 PM
Hi Paul,
Thanks for the reply.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 01:16 AM
Sorry Madhu - I worked it out. This is much cleaner!
var newDate = 'Friday, August 18, 2017 8:00 AM';
var gdt = new GlideDateTime();
gdt.setDisplayValue(newDate, "E, MMMM dd, yyyy K:mm a");
var dateTimeForField = gdt.getDisplayValue();
gs.print(dateTimeForField);
2017-08-18 08:00:00
Please mark as correct/helpful
References:
SimpleDateFormat (Java Platform SE 7 )
http://wiki.servicenow.com/index.php?title=Scoped_GlideDateTime_API_Reference#gsc.tab=0
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022