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

Thanks Abhinay - works as required!



Ch


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

Abhinay Erra
Giga Sage

Glad you got your question answered.


TrenaFritsche
Tera Expert

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