Facing issues in Converting JSON datetime field into ServiceNow format

Aditya Banka2
Tera Guru

Hello Geeks, 

I am calling in a API and receiving a response and I am trying to push the values into an import set using a transform map.

Below are the fields that I am receiving and trying to convert them into ServiceNow datetime format. (dd-MM-yyyy HH:mm:ss) but not able to convert them. 

"Start Time": "Apr 13, 2022, 08:00:25 PM"
"End Time": "Apr 18, 2022, 06:57:18 AM"

Please help me out in how to convert them.

4 REPLIES 4

Filipe Cruz
Kilo Sage
Kilo Sage

Hello Aditya,

Check this example:

var json_obj = {"start_time": "Apr 13, 2022, 08:00:25 PM"};
var date = new GlideDateTime();
date.setDisplayValue(json_obj.start_time, "MMM dd, yyyy, hh:mm:ss a");

gs.print(date.getDisplayValue()); //displays according to the timezone and date format of your user

 

I think this is exactly what you need!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Hello Filipe, Thank You so much. I am able to use your logic in OnBefore script and was able to populate the field values. The json response DATE values that I am receiving are in IST time zone. Is there is a way that we can convert it into EST time zone before we update the fields.

Hello Aditya,

Here is an example of converting GMT to Canada timezone.
Use this example to convert to your own timezone (EST in this case):

//converting GMT to Canada/Eastern


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");
var date = new GlideDateTime();

date.setTZ(tz);

 

Hope this helps!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Hello Filip,

 

I am using the code as below.

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var date = new GlideDateTime();
    var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");
    date.setDisplayValue(source.u_start_time, "MMM dd, yyyy, hh:mm:ss a");
    date.setTZ(tz);

    target.u_backup_start = date.getDisplayValue(); // return the value to be put into the target field

    var date1 = new GlideDateTime();
    var tz1 = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");
    date1.setDisplayValue(source.u_end_time, "MMM dd, yyyy, hh:mm:ss a");
    date1.setTZ(tz1);

    target.u_backup_end = date1.getDisplayValue(); // return the value to be put into the target field

})(source, map, log, target);

 

The source fields that I am receiving are in IST. But when I use the above logic it still shows the same datetime which I am receiving which means it's not converting it into EST.