Get the time difference between two datetimes

前端 未结 19 1989
花落未央
花落未央 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 06:01

    If we want only hh:mm:ss, we can use a function like that:

    //param: duration in milliseconds
    MillisecondsToTime: function(duration) {
        var seconds = parseInt((duration/1000)%60)
            , minutes = parseInt((duration/(1000*60))%60)
            , hours = parseInt((duration/(1000*60*60))%24)
            , days  = parseInt(duration/(1000*60*60*24));
    
        var hoursDays = parseInt(days*24);
        hours += hoursDays;
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        return hours + ":" + minutes + ":" + seconds;
    }
    

提交回复
热议问题