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

The SN Nerd
Giga Sage
Giga Sage

Date formats explained below:

https://community.servicenow.com/community?id=community_blog&sys_id=bc0e6a2ddbd0dbc01dcaf3231f961931


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thanks Paul. The document looks really helpful.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

hi Paul,

I made some changes in my script. It is not working. Can you check where it is wrong.

var tz = Packages.java.util.TimeZone.getTimeZone("GMT");
var sd = new GlideDateTime();
sd.setTZ(tz);
var st_dte= sd.setValue(current.start_date);
var frmtdt = 'yyyyMMdd HH:mm:ss';
variable.inputs.planned_start_date =sd.setDisplayValue(st_dte,frmtdt);

var ed = new GlideDateTime();
ed.setTZ(tz);
var end_dte= sd.setValue(current.start_date);
var end_frmtdt = 'yyyyMMdd HH:mm:ss';
variable.inputs.planned_end_date = ed.setDisplayValue(end_dte,end_frmtdt);

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

setDisplayValue() does not return anything, so you are setting your input variables to null.

Try getDisplayValue() instead:

var tz = Packages.java.util.TimeZone.getTimeZone("GMT");
var sd = new GlideDateTime();
sd.setTZ(tz);
var st_dte= sd.setValue(current.start_date);
var frmtdt = 'yyyyMMdd HH:mm:ss';
sd.setDisplayValue(st_dte,frmtdt);
variable.inputs.planned_start_date = sd.getDisplayValue();

var ed = new GlideDateTime();
ed.setTZ(tz);
var end_dte= ed.setValue(current.start_date); // this was sd, i think you meant ed
var end_frmtdt = 'yyyyMMdd HH:mm:ss';
ed.setDisplayValue(end_dte,end_frmtdt);
variable.inputs.planned_end_date = ed.getDisplayValue();

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

This didn't work Paul. 😞

 

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023