How to get the difference Duration from two Datetime

Hello,
I have two Datetime fields and one Duration type field.
What I want is to calculate the difference between these two date fields in D, H, m, s using the client script (Javascript)

For example I have these two dates which are already have them

let d1 = "08-05-2022 14:00:25"
let d2 = "09-05-2022 15:10:35"      

The difference duration should be as shown in the photo below:

141414114

Is there a specific method in the frappe framework to do it?

frappe.utils.date_diff Try this

are there arguments in this function?

TypeError: frappe.utils.date_diff is not a function

I could get the day and the hour diff using these two functions but how to get the minutes?

        let d1 = "2022-05-19 15:03:12"
        let d2 = "2022-05-20 15:13:12"   
        console.log(frappe.datetime.get_hour_diff(d2,d1))
        console.log(frappe.datetime.get_day_diff(d2,d1))


Hi @Mohsin1990

Use date_diff, and convert the difference to minutes

@Mohsin1990 multiply it by 60

multiply what?

//get new date given a date, and a duration
let exp_end_time = moment(exp_start_date).add(row.expected_installation_duration, 'seconds');

//calculate new duration, based on two dates

let time_diff_in_seconds = moment(row.actual_end_time).diff(row.actual_start_time, 'seconds', true);
frappe.model.set_value(cdt, cdn, 'actual_installation_duration', time_diff_in_seconds);
1 Like

There is no function named date_diff, I think you mean get_diff
but it just returns the same as the get_day_diff

let d1 = "2022-05-19 15:03:12"
let d2 = "2022-05-20 15:13:12"   
console.log(frappe.datetime.get_hour_diff(d2,d1))
console.log(frappe.datetime.get_day_diff(d2,d1))
console.log(frappe.datetime.get_diff(d2,d1))

//output:
//24
//1
//1

Hi @Mohsin1990

frappe.utils.date_diff() is a Python method, so you can’t use it in the front-end with JS.

But you can use (moment(d2) - moment(d1))/1000/60 β†’ 1450 minutes

1 Like

Thank you brother this is the function I want it

        let d1 = "2022-05-29 15:33:42"
        let d2 = "2022-05-19 15:03:12"
        let days = moment(d1).diff(d2, 'days', true);
        let hours = moment(d1).diff(d2, 'hours', true);
        let minutes = moment(d1).diff(d2, 'minutes', true);
        let seconds = moment(d1).diff(d2, 'seconds', true);
        
        console.log(Math.trunc(days))
        console.log(Math.trunc(hours))
        console.log(Math.trunc(minutes))
        console.log(Math.trunc(seconds))