Help to calculate the difference between two fields in minutes

Thrupthi
Tera Expert

I have a two fields on my table which are of date/time type; I have start date and end date, inorder to calculate the difference i have created a new field of type duration

Business rule is been for this it returns a value in "days:hours:minutes"seconds"

Instead i just want the field to calculate the value in minutes; can someone please help me with this

Business rule:

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

if (current.u_incident_start_date.nil() || current.u_incident_end.nil()) {
current.setValue('u_incident_duration_calculation', '');
return;
}

var st = new GlideDateTime();


st.setValue(current.getValue("u_incident_start_date"));

var ed = new GlideDateTime();
ed.setValue(current.getValue("u_incident_end"));

var dur = GlideDateTime.subtract(st, ed);
current.setValue("u_incident_duration_calculation", dur.getValue());

})(current, previous);




1 ACCEPTED SOLUTION

That could be the issue. If you donot want decimals , try the updated code

(function executeRule(current, previous /*null when async*/) {
	
	if (current.u_incident_start_date.nil() || current.u_incident_end.nil()) {
		current.setValue('u_incident_duration_calculation', '');
		return;
	}
	
	var st = new GlideDateTime(current.u_incident_start_date);
	var ed = new GlideDateTime(current.u_incident_end);
	
	var sec = gs.dateDiff(st, ed, true);
	
	var minutes = sec/60;
	
	current.u_incident_duration_calculation = parseInt(minutes,10); // Remove decimals 

	
})(current, previous);

View solution in original post

15 REPLIES 15

Yes u_incident_start_date (This is the start field of date/time type)

u_incident_end (This is the end field of date/time type)

u_incident_duration_calculation (this is the field created to calculate the difference between start and end in minutes; and its of integer type)

Ok try this and let me know if you see any message at the top after you update the record

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

if (current.u_incident_start_date.nil() || current.u_incident_end.nil()) {
current.setValue('u_incident_duration_calculation', '');
return;
}

 var gd = gs.dateDiff(gr.getDisplayValue('u_incident_end'), gr.getDisplayValue('u_incident_start_date'), true); // gd will be in seconds

var min = gd/1440; //Do your calculation, 

gs.addInfoMessage("Second printed:--->"+gd+"Minutes Printed:---->"+min);
current.u_incident_duration_calculation= min;

})(current, previous);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hi Prateek,

 

Nothing worked, it din't display any value

Just to inform you i have written a Before Business rule with update and insert checked and condition when end date changes.

 

can you try this for testing purposes. Make sure you are changing End Date

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

var gd = gs.dateDiff(gr.getDisplayValue('u_incident_end'), gr.getDisplayValue('u_incident_start_date'), true); // gd will be in seconds

var min = gd/1440; //Do your calculation, 

gs.addInfoMessage("Second printed:--->"+gd+"Minutes Printed:---->"+min);
current.u_incident_duration_calculation= min;

})(current, previous);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

No Prateek nothing helped again

It displays blank

find_real_file.png