- 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 06:56 AM
Hi Charles,
You can use subtract(int) method for subtracting. For more information, please use the below wiki link.
http://wiki.servicenow.com/?title=GlideDateTime#
Thanks,
Senthil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 07:00 AM
Hi Charles,
In other way you can use getNumericValue() method for converting the date to numeric, then subtract the value. Again you can convert the numeric value to date.
Thanks,
Senthil
Hoffensoft — You Dream We Deliver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 07:13 AM
Hi Charles,
I have given example here.
var gdt = new GlideDateTime('2011-08-31 08:00:00'); // parent.u_incident_start_time.getDisplayValue()
var duration = 43200000; // 12 Hrs
var subtractedvalue = gdt.getNumericValue() - duration ;
var gdt2 = new GlideDateTime();
gdt.setNumericValue(subtractedvalue);
gs.log(gdt.getValue());
Please check the same and let me know any issues.
Thanks,
Senthil
Hoffensoft — You Dream We Deliver
PS: Hit like, Helpful or Correct depending on the impact of the response

- 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);