I need to add 5 minutes to a DateTime in a client script but addSeconds not working

tkh
Kilo Expert

The have been asked to remove a choice in a select box if a change goes over the planned end date.

So basically once you hit the Review phase and the Actual End date has been populated by the system, we want to evaluate if the Actual End time, exceeds the Planned End time.

If true, then we want to remove the choice option with a value of 1 from the Closure Code select box.

The following code is working, however, they decided they wanted to allow users a 5 minute grace period.  As is it works fine, but I am not able to get the addSeconds(300) to work.

Here s working code:

function onLoad() {
if (g_form.getValue('work_end') != '') {
var planEnd = g_form.getValue('end_date');
var actEnd = g_form.getValue('work_end');
var answer = false;

if(planEnd < actEnd) {
answer = true;
if (answer==true){
g_form.removeOption('u_qs_closure_code', 1);
}
}
}
}

 

All I need to do is add 5 the minutes to the planEnd time.

Any ideas?

3 REPLIES 3

Mike Patel
Tera Sage

you can do below

var planEndStr = g_form.getValue('end_date');
var planEndNum = getDateFromFormat(planEndStr, g_user_date_format);

var time = 5 * 60 * 1000; //5 mins * 60 secs * 1000 ms

planEndNum + time;

\\take action 

 

sachin_namjoshi
Kilo Patron
Kilo Patron

You need to use GlideAjax to achieve this.

Please see below for available code

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

 

Regards,

Sachin

tkh
Kilo Expert

First, thank you everyone for your responses.  I really appreciate it.

 

However nothing was working so I ended up go at the problem from a different angle.

Since ultimately my goal was to compare the Planned End DateTime with the Actual End DateTime, while allowing a 5 minute grace period I ended up going with a solution where I

  • subtracted the Actual End from the Planned End, (actEnd - planEnd)
  • then converted the difference to minutes.   [Math.round(((diffMs % 86400000) % 3600000) / 60000); // convert to minutes]
  • After that I just added a line where if the answer was greater then 5 do something. if(diffMins > 5)

The actual script is below:

function onLoad() {
if (g_form.getValue('Actual_End') != '') {
var planEnd = g_form.getValue('planned_end'); //Planned End date, the time the window i supposed to end by
var actEnd = g_form.getValue('actual_end'); //Actual End the DateTime is actually ended
var planned_End_obj = new Date(Date.parse(planned_end)); //convert DateTime into useful formate
var actual_End_obj = new Date(Date.parse(actual_end)); //convert DateTime into useful format
var diffMs = Math.abs(actual_End_obj - planned_End_obj); //The actual calculation of ActualEnd - PlannedEnd
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // convert calculation answer to minutes

//We are allowing a 5 minute grace period, so if the time difference in minutes is more then the 5 minute grace period we remove the option from the list.
if(diffMins > 5) {
g_form.removeOption('u_qs_closure_code', 1);
}
}
}