Time conversion to 24hr format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2020 07:15 AM
Hi,
I was asked to write a script to convert CST time to GMT time. I developed that successfully but now i am asked to convert 12 hour format to 24 hour format in same script. Can you please suggest what changes i can do to achieve this in below script.
var tz = Packages.java.util.TimeZone.getTimeZone("GMT");
var sd = new GlideDateTime();
sd.setTZ(tz);
sd.setValue(current.start_date);
variable.inputs.planned_start_date = sd.getDisplayValue().toString();
var ed = new GlideDateTime();
ed.setTZ(tz);
ed.setValue(current.end_date);
variable.inputs.planned_end_date = ed.getDisplayValue().toString();
ServiceNow Community Rising Star 2022/2023
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2020 05:19 PM
Hopefully, this code will work for you.
If not, please reply with the log outputs.
var simpleDateFormat = 'yyyyMMdd HH:mm:ss';
var gStartDate = new GlideDate(); //GlideDate is not documented but has function to output date in any format
gStartDate.setValue(current.start_date);
var displayStartDate = gStartDate.getByFormat(simpleDateFormat); //This will already be in GMT
variable.inputs.planned_start_date = displayStartDate ;
var gEndDate = new GlideDate();
gEndDate.setValue(current.end_date);
var displayEndDate = gEndDate.getByFormat(simpleDateFormat);
variable.inputs.planned_end_date = displayEndDate ;
// Reply with these log values if the code doesn't work as you expect
gs.log("displayStartDate : " + displayStartDate );
gs.log("displayEndDate : " + displayEndDate );
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
‎09-15-2020 11:02 PM
Sure Paul. Let me try this.
ServiceNow Community Rising Star 2022/2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 08:05 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 07:08 PM
Hey,
You just need to change the format as follows
var simpleDateFormat = 'yyyy-MM-dd HH:mm:ss';
Please mark as correct if this works for you.
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
‎09-18-2020 07:31 AM
we are going to test it on Monday. I'll let you know if it worked fine in all cases.
ServiceNow Community Rising Star 2022/2023