Get the time difference between two datetimes

前端 未结 19 1963
花落未央
花落未央 2020-11-22 05:25

I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I\'m having a hard time trying to do something that seems simple: geting the differ

相关标签:
19条回答
  • 2020-11-22 05:46

    The following approach is valid for all cases (difference between dates less than 24 hours and difference greater than 24 hours):

    // Defining start and end variables
    let start = moment('04/09/2013 15:00:00', 'DD/MM/YYYY hh:mm:ss');
    let end   = moment('04/09/2013 14:20:30', 'DD/MM/YYYY hh:mm:ss');
    
    // Getting the difference: hours (h), minutes (m) and seconds (s)
    let h  = end.diff(start, 'hours');
    let m  = end.diff(start, 'minutes') - (60 * h);
    let s  = end.diff(start, 'seconds') - (60 * 60 * h) - (60 * m);
    
    // Formating in hh:mm:ss (appends a left zero when num < 10)
    let hh = ('0' + h).slice(-2);
    let mm = ('0' + m).slice(-2);
    let ss = ('0' + s).slice(-2);
    
    console.log(`${hh}:${mm}:${ss}`); // 00:39:30
    
    0 讨论(0)
  • 2020-11-22 05:50
    var a = moment([2007, 0, 29]);
    var b = moment([2007, 0, 28]);
    a.diff(b, 'days') //[days, years, months, seconds, ...]
    //Result 1 
    

    Worked for me

    See more in http://momentjs.com/docs/#/displaying/difference/

    0 讨论(0)
  • 2020-11-22 05:50

    This should work fine.

    var now  = "04/09/2013 15:00:00";
    var then = "02/09/2013 14:20:30";
    
    var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
    var d = moment.duration(ms);
    
    console.log(d.days() + ':' + d.hours() + ':' + d.minutes() + ':' + d.seconds());
    
    0 讨论(0)
  • 2020-11-22 05:55

    EPOCH TIME DIFFERENCE USING MOMENTJS:

    To Get Difference between two epoch times:

    Syntax: moment.duration(moment(moment(date1).diff(moment(date2)))).asHours()

    Difference in Hours: moment.duration(moment(moment(1590597744551).diff(moment(1590597909877)))).asHours()

    Difference in minutes: moment.duration(moment(moment(1590597744551).diff(moment(1590597909877)))).asMinutes().toFixed()

    Note: You could remove .toFixed() if you need precise values.

    Code:

    const moment = require('moment')
    
    console.log('Date 1',moment(1590597909877).toISOString())
    console.log('Date 2',moment(1590597744551).toISOString())
    console.log('Date1 - Date 2 time diffrence is : ',moment.duration(moment(moment(1590597909877).diff(moment(1590597744551)))).asMinutes().toFixed()+' minutes')
    
    

    Refer working example here: https://repl.it/repls/MoccasinDearDimension

    0 讨论(0)
  • 2020-11-22 05:56

    This will work for any date in the format YYYY-MM-DD HH:mm:ss

    const moment=require("moment");
    let startDate=moment("2020-09-16 08:39:27");
    const endDate=moment();
    
    
    const duration=moment.duration(endDate.diff(startDate))
    console.log(duration.asSeconds());
     console.log(duration.asHours());
    
    0 讨论(0)
  • 2020-11-22 06:00

    Instead of

    Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss")
    

    It's better to do

    moment.utc(total.asMilliseconds()).format("HH:mm:ss");
    
    0 讨论(0)
提交回复
热议问题