- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 06:01 AM
Hello,
How can I subtract 12 hours from a date time field? I need to retrieve one date/time (a) and then subtract 12 hours from it and then show that date/time too (b).
I have tried the following three scripts, but in the first two cases the returned values for (a) and (b) are identical, and then in the last the results are 'undefined'.
Any ideas?
Thanks
var a = parent.u_incident_start_time.getDisplayValue();
var b = parent.u_incident_start_time.getDisplayValue();
b.subtract(43200000);
gs.log('12 Hours before is: '+ b.toString());
gs.log('Actual Start time is: '+ a.toString());
---------------
var a = parent.u_incident_start_time.getDisplayValue();
var b = parent.u_incident_start_time.getDisplayValue();
var hours = 432000;
b.addSeconds(-hours);
gs.log('12 Hours before is: '+ b.toString());
gs.log('Actual Start time is: '+ a.toString());
------------
var a = parent.u_incident_start_time.getDisplayValue();
var b = parent.u_incident_start_time.getDisplayValue();
var result = b.addSeconds(-43200)
gs.log('12 Hours before is: '+ result.toString());
gs.log('Actual Start time is: '+ a.toString());
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 07:32 AM
Hi charlesr
Use GlideDateTime API for this,
var actual_time= parent.u_incident_start_time.getDisplayValue();
var gdt = new GlideDateTime();
gdt.setValue( parent.u_incident_start_time.getDisplayValue());
gdt.addSeconds(-43200);
var new_time=gdt.getValue();
gs.log('12 Hours before is: '+new_time);
gs.log('Actual Start time is: '+ actual_time);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 08:25 AM
Thanks Abhinay - works as required!
Ch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2018 08:05 AM
Hi abhinay,
Can u please check below code and let me know if there is any mistake
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var start = current.start_date.getGlideObject();
//subtract 2 hours (60 mins * 60 secs)
start.addSeconds(-7200);
var end = current.u_end_date.getGlideObject();
//add hour (60 mins * 60 secs)
end.addSeconds(7200);
var schedRec = new GlideRecord('cmn_schedule_span');
schedRec.addQuery('start_date_time', '<=', gs.hoursAgo(-2));
schedRec.addQuery('end_date_time', '<=', gs.hoursAgo(2));
schedRec.query();
while(schedRec.next()){
gs.print(schedRec.start_date_time); //resulting data
current.u_custom_field="This is testing";
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 09:03 AM
Glad you got your question answered.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2019 03:39 AM
Hello,
I am trying to use addSeconds API in the Server Script of a widget in a scoped App. I am getting a failing widget: Server JavaScript error Cannot find function addSeconds in object 03/25/2019 06:33:04. Any thoughts of what I can do to easily subtract 10 seconds from a datetime without having to write a huge functions to manipulate the datetime using substring?
Here is the code:
var DTS = data.surveyTimeStamp = new GlideDateTime().getDisplayValue();
var ADJDate = DTS.addSeconds(-10);
console.log('ADJDate is '+ ADJDate);
Thanks,
Trena