Convert 12 hour date format to 24 hour format

dhruva
Kilo Contributor

In my inbound action date format is in form of "Thursday, May 18, 2017 4:00 PM" to dd-MMM-yyyy hh:mm:ss ? has anyone done this conversion in   servcienow before ?

6 REPLIES 6

Hi Dhruva,



If the format which you are receiving will be constant always here is the string manipulation. you need to enhance this further to convert time from 12hour to 24hour format.



var data =   JSON.parse('{"Jan":"01","February":"02","March":"03","April":"04","May":"05","June":"06","July":"07","August":"08","September":"09","October":"10","November":"11","December":"12"}');



var str = 'Thursday, May 18, 2017 4:00 PM';



var monthDay = str.split(",")[1].trim();


var monthName = monthDay.split(" ")[0];


var day = monthDay.split(" ")[1];



var yearTime = str.split(",")[2].trim();



var year = yearTime.split(" ")[0];


var time = yearTime.split(" ")[1];


var amPm = yearTime.split(" ")[2];



var date = day + '-' + data[monthName] + '-' + year;


gs.print(date);



It prints 18-05-2017 pending part is convert 04:00 PM to 16:00:00.


Seconds will always be 00 since you are not receiving it from them.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Are you converting to dd-MMM-yyyy hh:mm:ss because that is your system format, and you'd like to store the date string from your inbound action in a date/time field? If so, you can accomplish that in just three lines of code:

var gdt = new GlideDateTime();
gdt.setDisplayValue(YOUR_INBOUND_ACTION_DATE_STRING, 'EEEE, MMMM d, yyyy hh:mm aaa');
current.setValue('start_date', gdt.getValue());