- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 08:37 PM
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?
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
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 12:39 AM - edited 08-14-2023 12:40 AM
@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 !!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 12:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 12:39 AM - edited 08-14-2023 12:40 AM
@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 !!