Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Vivektietsood
Tera Guru

Please use class- java.text.SimpleDateFormat

The format string consists of the following abbreviations.

Time format
FieldFull formShort form
Hour (1-12)hh (2 digits)h (1 or 2 digits)
Hour (0-23)HH (2 digits)H (1 or 2 digits)
Minutemm (2 digits)m (1 or 2 digits)
Secondss (2 digits)s (1 or 2 digits)

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/administer/time/reference/r_TimeFormat.html

 

Please like or comment if helpful or accept solution.

Can you please accept the solution if this helps ? This would inturn help others with similar questions.Thanks

Hitoshi Ozawa
Giga Sage
Giga Sage

Try script below:

var tz = Packages.java.util.TimeZone.getTimeZone("GMT");

var sd = new GlideDateTime();
sd.setTZ(tz);
sd.setValue(current.start_date);
var sdDate = sd.getLocalDate();
var sdTime = sd.getLocalTime();
variable.inputs.planned_start_date =sdDate + " " + sdTime.getFormat("HH:mm:ss");

var ed = new GlideDateTime();
ed.setTZ(tz);
ed.setValue(current.end_date);

var edDate = sd.getLocalDate();
var edTime = sd.getLocalTime();

variable.inputs.planned_end_date = edDate + " " + edTime.getFormat("HH:mm:ss");

Hozawa Hi,

This script didn't work.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023