Difference between dates in JavaScript

后端 未结 8 2386
渐次进展
渐次进展 2020-11-22 13:22

How to find the difference between two dates?

8条回答
  •  被撕碎了的回忆
    2020-11-22 13:42

    You can also use it

    export function diffDateAndToString(small: Date, big: Date) {
    
    
        // To calculate the time difference of two dates 
        const Difference_In_Time = big.getTime() - small.getTime()
    
        // To calculate the no. of days between two dates 
        const Days = Difference_In_Time / (1000 * 3600 * 24)
        const Mins = Difference_In_Time / (60 * 1000)
        const Hours = Mins / 60
    
        const diffDate = new Date(Difference_In_Time)
    
        console.log({ date: small, now: big, diffDate, Difference_In_Days: Days, Difference_In_Mins: Mins, Difference_In_Hours: Hours })
    
        var result = ''
    
        if (Mins < 60) {
            result = Mins + 'm'
        } else if (Hours < 24) result = diffDate.getMinutes() + 'h'
        else result = Days + 'd'
        return { result, Days, Mins, Hours }
    }
    

    results in { result: '30d', Days: 30, Mins: 43200, Hours: 720 }

提交回复
热议问题