Time conversion to 24hr format

JagjeetSingh
Kilo Sage
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

Ankur Bawiskar
Tera Patron
Tera Patron

@Jagjeet Singh 

GlideDateTime values are already stored in 24-hour format.

what is the exact requirement here?

Regards
Ankur

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

We have B2B with other instance.

In change module we are passing values for Planned start date and Planned end date in our instance. Our instance is in 12hr format but the other instance is in 24hr format.

So whenever anyone passes the values from our side it goes in 12hr format and timing is not updated correctly on other side.

 

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

@Jagjeet Singh 

just to inform the Package call won't work in scope application.

please share what value you are receiving and what value you want to get from that?

Regards
Ankur

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

i didn't understand what do you mean by saying that package call won't work in scope application. The code earlier was working fine where it just converted CST time to GMT time before sending the transaction to other instance.

Here i the value in start_date variable is stored in 12Hr format. like- 2020-09-17 08:00:00 PM, in CST time.

I want it to be in GMT time zone and in 24hr format and this will be passed in planned_start_date variable.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

If you're not getting an error like below, you're probably not using scope.

java.lang.SecurityException: Use of Packages calls is not permitted in scoped applications