We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to convert any Time zone date to GMT time zone

SivaKumar123
Tera Contributor

I need to convert any time zone to the GMT time zone.

Variable names: Escalation date and Probably date'

Type: Date /Time

Need to update the time zone on the variable name: Start date, End date, and type : single text.

Please help me with the code.

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {

return;
}

var start = g_form.getValue('escalation_date');
var end = g_form.getValue('probably_date');

g_form.setValue('MST_start_date', toMST(start));

g_form.setValue('MST_end_date', toMST(end));
}


function toMST(start){
var time = new GlideDateTime(start);
var tz = Packages.java.util.TimeZone.getTimeZone("GMT");
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
return time;
}

 

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage

@SivaKumar123 

 

GlideDateTime is not working in client side script, please try the Date object

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
     return;
}

var start = g_form.getValue('escalation_date');
var end = g_form.getValue('probably_date');

g_form.setValue('MST_start_date', new Date(start).toUTCString());
g_form.setValue('MST_end_date', new Date(end).toUTCString());
}

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

 

View solution in original post

2 REPLIES 2

Prince Arora
Tera Sage

@SivaKumar123 

 

GlideDateTime is not working in client side script, please try the Date object

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
     return;
}

var start = g_form.getValue('escalation_date');
var end = g_form.getValue('probably_date');

g_form.setValue('MST_start_date', new Date(start).toUTCString());
g_form.setValue('MST_end_date', new Date(end).toUTCString());
}

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

 

shanice_sims
Tera Contributor

Just to add for anyone else. I used the solution in an onSubmit client script and it worked for me too.