Difference of two time type field need to store it another time type field

Mansi roy
Tera Contributor

Hello All,

 

Can anyone please help me how to acheive this.

 

Mansiroy_0-1726829722470.png

i need to store diierence of actual end and actual start field value in total onhold time.

 

It is giving 1 hour 10minutes and 06 second.But it should give only 10 min 06 second.

 

Can anyone please help me how to get this correct value.

 

below is the before business rule i wrote.

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

    var startTime = current.u_actual_start.dateNumericValue();
var endTime = current.u_actual_end.dateNumericValue();
current.u_total_onhold_time.setDateNumericValue(endTime-startTime);
})(current, previous);
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Mansi roy ,

Fields with data type - Time will append 1970-01-01 before the time value in UTC.

Ex: let us consider Actual Start : 11:38:19, it'll be 1970-01-01 06:08:19 (you can find this in XML).

if you want to set value to time field you've to use this format.

I made modifications to your code, It should work as desired.

 

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var actualStart = current.u_start;
	var actualEnd = current.u_end;
	// gs.info('Actual Start'+actualStart +'Actual End'+actualEnd);
	var gdt1 = new GlideDateTime(actualStart);
	var gdt2 = new GlideDateTime(actualEnd);
	var diffIs = gs.dateDiff(gdt1,gdt2);
	gs.addErrorMessage(diffIs);

	var gdt= new GlideDateTime('1970-01-01 '+diffIs);
	gdt.addSeconds(gdt.getTZOffset()/-1000);
	gs.addErrorMessage(gdt);

	current.u_time_diff= gdt;
	// current.update();


})(current, previous);

 

KhasimPathan_0-1726984463409.png

 

Mark my response as helpful 🙂

View solution in original post

10 REPLIES 10

Sai_Charan_K
Kilo Sage

Hi @Mansi roy ,

I tried the below code in the background script and it seems to work fine. Can you please try checking it.

 

var start=new GlideDateTime("2024-09-20 11:38:19"); //your start date
var end= new GlideDateTime("2024-09-20 11:48:25"); //your end date
var start_mill=start.getNumericValue(); //turning to milliseconds
var end_mill=end.getNumericValue();/turning to milliseconds
var diff=(end_mill-start_mill); //difference in milliseconds
var final_date=new Date(diff).toISOString(); //turning to date once again from milliseconds
gs.print(final_date); //printing the date and time

 

Please find the screenshot attached for the output which I received. 

Please mark my answer "Helpful" and "mark as complete" if you feel that it has helped you in any way.

Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

var start=new GlideDateTime('u_actual_start');
var end = new GlideDateTime('u_actual_end');
var start_mill=start.getNumericValue(); //turning to milliseconds
var end_mill=end.getNumericValue();//turning to milliseconds
var diff=(end_mill-start_mill); //difference in milliseconds
var final_date=new Date(diff).toISOString(); //turning to date once again from milliseconds
//current.u_total_onhold_time.setValue(new Date(diff).toISOString());
current.setValue('u_total_onhold_time','final_date');
 
@Sai_Charan_K :Can you please let ,e know what is the wrong in code.

May I know what error are you getting? Can you please share the screenshot?

I am not getting the value the actual on hold field.it is coming blank.

Mansiroy_0-1726836289668.png