Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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
}

Hello again Shishir,

 

When I use getDisplayValue it returns undefined.

 

 

can you please print this variable "dateTimeStr" what you get? and what is the full format of the date/time?

Wait I got it with getDisplayValue...I changed the code to return the value before the 

var dateArr = dateTimeStr.split(' ');
var time = dateArr[1];

lines and it worked.

Any idea how I can get the PDT timezone part fixed?