Date computaion

gooday to all… can you help me to solve my problem to automaticaly compute the number of days in custom script… tnx in advance :smiling_face_with_three_hearts:

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffDays + " days");

you can use set_value instead of console.

1 Like

thank you for your response can you please teach me step by step in custom script…thank you very much :smiling_face_with_three_hearts:

you can use this code.

can you give me example sir …thankyou :smiling_face_with_three_hearts::heart_eyes:

frappe.ui.form.on(‘Voucher’, {
refresh: function(frm) {
const date1 = new Date(‘2010/12/05’);
const date2 = new Date(‘2010/10/10’);
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffDays + " days");
frm.set_value(‘days’,diffDays);
}
});

i have an error…thankyou very much sir :smiling_face_with_three_hearts:

instead of new Date(f_date) you replace it with frm.doc.f_date

image

still error sir :pensive:

dont use new Date
only frm.doc.f_date

image

still error sir :pensive:

check documentation


this is my doctype

i edited this script based on your doctype fields info, you can test

frappe.ui.form.on("Calc Date Diff", {

	t_date: function(frm) {
		if(frm.doc.f_date && frm.doc.t_date) {
			var diffDays = Math.round(Math.abs(( (new Date(frm.doc.t_date) - new Date(frm.doc.f_date)) / (24 * 60 * 60 * 1000))))
			frm.doc.diff_date = diffDays;
			console.log(diffDays)
			frm.refresh_fields()
		}
	}
})

thank you sir it’s working…

thank you very much sir @Rehan_Ansari and sir @PyJumper . God bless you :relaxed::blush::pray:

1 Like

According to the old dating system, dates before the birth of Christ are counted backward and the letters BC (Before Christ) are added at their end . And for dates after the birth of Christ, AD is added. But in the new system, we use BCE and CE instead of BC and AD. For more detail see https://discuss.frappe.io/t/date-computaion/86970/15-apkbikI hope your question is solved

Thanks!