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.

Script to add one minute to time field

tsoct
Tera Guru

Hello all,
I have a time field where only the time is kept. Right now, I have a script to minus and add 1 minute to the time. But using the script listed below, I am unable to do so. What might I have missed?

tsoct_0-1691984210965.png

 

var building = GlideRecord('u_building');
building.addQuery('sys_id','hea39b1977220110b3d7be377b5a7788');//Tower A
building.query();

var preblock = 1; // One minutes
if(building.next()){
var currTime = building.u_start_time; 
//If use  building.u_start_time.getDisplayValue(), currTime return 07:00:00. but unable to deduct 1 minutes from this displayvalue

currTime = currTime.replace(/-/g, "/");
currTime = new Date(currTime);
var minutes = preblock;
var millisecondsPerMinute = 60000;
var newTime = new Date(currTime - minutes * millisecondsPerMinute); //one minutes less

gs.log('currTime is ' + currTime);
// returned currTime is Thu Jan 01 1970 06:00:00 GMT-0800 (PST) 
//Incorrect. It should be 07:00:00

gs.log('newTime is ' + newTime);
//returned new time is Thu Jan 01 1970 05:59:00 GMT-0800 (PST) 59
//Incorrect. It should be 06:59:00
}

 

1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@tsoct Add below code inside If block and it will work

 

var gdt = new GlideDateTime(building.u_start_time);
gs.info('current time '+gdt.getDisplayValue());

var output = gdt.getNumericValue() - 60000; //subtracting 1 minute

var gdt1 = new GlideDateTime();
gdt1.setNumericValue(output);
gs.info('new time '+gdt1.getDisplayValue());

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !! 

View solution in original post

2 REPLIES 2

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

 

please use the "GlideDateTime" object for this. It has the "add" function, which adds time in miliseconds (or substracts it, if a negative value is added). A date object does not work the same way.

 

Regards

Fabian

SANDEEP28
Mega Sage

@tsoct Add below code inside If block and it will work

 

var gdt = new GlideDateTime(building.u_start_time);
gs.info('current time '+gdt.getDisplayValue());

var output = gdt.getNumericValue() - 60000; //subtracting 1 minute

var gdt1 = new GlideDateTime();
gdt1.setNumericValue(output);
gs.info('new time '+gdt1.getDisplayValue());

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!