Want to create a custom button

Hello,
I want to create custom button & onClick it should create read only current time field…
Actually i want to create Attendance Form with CHECK IN & CHECK OUT Button.
please help & advise.

Solved.

@clarkej Sure Sir, i’ll contribute.”

Thanks good to hear @shahid

1 Like

If you find a solution of something, the community will be grateful if you share the solution, and approach how you did it.

I’ve had some posts I think where I have posted something, minutes later found the solution and posted on my posted thread.

Cheers!

And in the event you yourself are stuck, and forget what you did, you can refer back too :slight_smile:

1 Like

Here is my script :

frappe.ui.form.on("Times", {


	check_in_btn: function(frm){
		check_in_time(frm);
	},
	check_out_btn: function(frm){
		check_out_time(frm);
	}



});

var check_in_time = function(frm)
{
		frm.set_value("check_in", frappe.datetime.now_time());
		frm.set_value("check_in_date", frappe.datetime.get_today());
}
var check_out_time = function(frm)
{
		frm.set_value("check_out", frappe.datetime.now_time());
		frm.set_value("check_out_date", frappe.datetime.get_today());
		var start = frm.doc.check_in.split(":");
		var end = frm.doc.check_out.split(":");
		var date1 = new Date(2000, 0, 1,  start[0], start[1]); 
		var date2 = new Date(2000, 0, 1, end[0], end[1]); 

		if (date2 < date1) {
			date2.setDate(date2.getDate() + 1);
		}

		var diff = date2 - date1;
		var msec = diff;
		var hh = Math.floor(msec / 1000 / 60 / 60);
		msec -= hh * 1000 * 60 * 60;
		var mm = Math.floor(msec / 1000 / 60);
		msec -= mm * 1000 * 60;
		var ss = Math.floor(msec / 1000);
		msec -= ss * 1000;
		
		frm.set_value("total", hh+":"+mm+":"+ss);
}
2 Likes

Thank you much appreciated shahid