Subtract Minutes from date/time field

CharlesR1
Kilo Guru

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

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

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


View solution in original post

8 REPLIES 8

senthilkannandp
Kilo Expert

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


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


senthilkannandp
Kilo Expert

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


Abhinay Erra
Giga Sage

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