Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Time conversion to 24hr format

JagjeetSingh
Kilo Sage

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();

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023
20 REPLIES 20

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

Sure Paul. Let me try this.

 

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

@Paul Morris - I guess we are close. Conversion is fine but i need hyphen in date. Like it should be: 2020-09-22

find_real_file.png

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

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

we are going to test it on Monday. I'll let you know if it worked fine in all cases.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023