Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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 🙂