ERPNext Conference 2019* ERPNext.com Blog

Sum two Time fields


#1

Hello Dears

I have created a new custom field in sales invoice with time type called Duration

and put its default value 00:45

and another field which is also time called end_time

what i need is to sum ( posting_time which is a standard field in sales invoice + duration) and set value to end_time

how can i do it ??


#2

You can do it using moment https://momentjs.com/ and custom scripts.


#3

@Mahmoud_Ghoneem

        var duration = cur_frm.doc.duration;
        var postingTime = cur_frm.doc.posting_time;
		var hour=0;
		var minute=0;
		var second=0;

		var splitDuration= duration.split(':');
		var splitPostingTime= posting_time.split(':');

		hour = parseInt(splitDuration[0])+parseInt(splitPostingTime[0]);
		minute = parseInt(splitDuration[1])+parseInt(splitPostingTime[1]);
		hour = hour + minute/60;
		minute = minute%60;
		second = parseInt(splitDuration[2])+parseInt(splitPostingTime[2]);
		minute = minute + second/60;
		second = second%60;
		cur_frm.set_value("end_time",hour+':'+minute+':'+second);
		cur_frm.refresh_fields("end_time")

hope you find it useful


Calculate Age for a Student
DateTime field difference
Child table fetching data problem
#4

Thank you for your reply

i`v changed

var splitPostingTime= posting_time.split(':');

to

var splitPostingTime= postingTime.split(':');

but the output be like that


#5

@Mahmoud_Ghoneem
when you are assigning the value to end_time just typecast it into int

cur_frm.set_value('end_time',parseInt(hour)+':'+parseInt(minute)+':'+parseInt(second))
cur_frm.refresh_fields('end_time')

#6

Excellent Thanks for your efforts