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

3 REPLIES 3

Fabian Kunzke
Mega Sage
Mega 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 !! 

Krecker
Tera Expert

While Sandeeps solution is working fine, you can also use this shorter and cleaner code using built-in gdt mutators:

var gdt = new GlideDateTime(current.u_start_time); // get start date
gdt.addSeconds(-1 * 60);             // mutate gdt by minus one minute
current.u_end_time = gdt;             // assign to end date
current.update();