Dealing with time in ServiceNow scripts

ServiceNowSteve
Giga Guru

Good Afternoon Team,

I am working on a request where an ITIL user can insert a time in a Time object on a form and at that time a script will execute.

I have it all working but the time is really messy and I'll explain what I mean.

To begin with my Date is showing in PDT but my instance is set to Central and my personal settings are Eastern.

var d = new Date();
var pdtTime = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
gs.print(pdtTime);

This means the time is 3 hours behind Eastern and 2 behind Central.

Then top top it off the Time object on the form automatically adds 5 hours to what the user inputs so if I put the timer at 00:00:00 it will tell me on a gs.print that it's 05:00:00 which means that I need to take that 5 hour difference plus the three hour difference and calculate it out to be the time I need.

Here's my code for the time object on the form.

var groupCheck = new GlideRecord('u_group_management');
groupCheck.query();

var dateTimeStr = groupCheck.getValue('u_timer_auto_start')
var dateArr = dateTimeStr.split(' ');
var time = dateArr[1];
gs.print(time); // Shows +5 hours

Any idea how I can rein in this crazy mess?

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

to get the instance time (I think in your case it's CST), you may use getDisplayValue() function and i think getValue() will get you the GMT time (which is 5 hours ahead of CST). 

Can we try with getDisplayValue() and see what you get.

var groupCheck = new GlideRecord('u_group_management');
// have some condition here to filter the query for specific record;
groupCheck.query();
if(groupCheck.next()){
var dateTimeStr = groupCheck.getDisplayValue('u_timer_auto_start');
var dateArr = dateTimeStr.split(' ');
var time = dateArr[1];
gs.print(time); // Shows +5 hours
}

View solution in original post

6 REPLIES 6

Can you please check if system timezone is set to Pacific?

 

find_real_file.png

puneetgoels1
Tera Guru

use this

 

var gdt = new GlideDateTime();
gdt.setDisplayValue(tdate.toString());

gs.info(gdt.getValue());