Automatically set the difference between actual start date and actual end date

nath0507
Tera Contributor

Hello experts 

 

I am needing help with populatig dirrence between actual start and actual end date in the duration field on in the outage form 

 

when the "actual start date" and actual end date fields are populated on the change form, then the duration field should automatically be set to the difference between the actual end date and the actual start date

 

 

4 REPLIES 4

Prasad Dhumal
Mega Sage
Mega Sage

Hello Nath,

 

Store the Start and End Date in var:
var actualStartDate = g_form.getValue('actual_start_date');
var actualEndDate = g_form.getValue('actual_end_date');

 

Get the difference between:
var startTime = new GlideDateTime(actualStartDate);
var endTime = new GlideDateTime(actualEndDate);
var duration = endTime.getDifference(startTime);

 

Set duration:
g_form.setValue('duration', duration / 1000 / 60);

@Prasad Dhumal 

 

Thank you sir,

 

This would be a client script correct?

 

No, GlideDateTime will not work client side.

You will have ise to GlideAjax and Script Includes.

Do the calculation part in script include and return the difference to the client script 

Johns Marokky
Tera Guru

Hi @nath0507 ,

if you are looking for exact scripts, you can go through this community article which might help.

https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and...

 

Mark helpful if it helps in solving your query,

 

Regards,

Johns