Get the time difference between two datetimes

前端 未结 19 1964
花落未央
花落未央 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:00

    InTime=06:38,Outtime=15:40

     calTimeDifference(){
            this.start = dailyattendance.InTime.split(":");
            this.end = dailyattendance.OutTime.split(":");
            var time1 = ((parseInt(this.start[0]) * 60) + parseInt(this.start[1]))
            var time2 = ((parseInt(this.end[0]) * 60) + parseInt(this.end[1]));
            var time3 = ((time2 - time1) / 60);
            var timeHr = parseInt(""+time3);
            var  timeMin = ((time2 - time1) % 60);
        }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 06:01

    Use this,

      var duration = moment.duration(endDate.diff(startDate));
    
        var aa = duration.asHours();
    
    0 讨论(0)
  • 2020-11-22 06:01

    In ES8 using moment, now and start being moment objects.

    const duration = moment.duration(now.diff(start));
    const timespan = duration.get("hours").toString().padStart(2, '0') +":"+ duration.get("minutes").toString().padStart(2, '0') +":"+ duration.get("seconds").toString().padStart(2, '0');
    
    0 讨论(0)
  • 2020-11-22 06:01

    Typescript: following should work,

    export const getTimeBetweenDates = ({
      until,
      format
    }: {
      until: number;
      format: 'seconds' | 'minutes' | 'hours' | 'days';
    }): number => {
      const date = new Date();
      const remainingTime = new Date(until * 1000);
      const getFrom = moment([date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()]);
      const getUntil = moment([remainingTime.getUTCFullYear(), remainingTime.getUTCMonth(), remainingTime.getUTCDate()]);
      const diff = getUntil.diff(getFrom, format);
      return !isNaN(diff) ? diff : null;
    };
    
    0 讨论(0)
  • 2020-11-22 06:02

    When you call diff, moment.js calculates the difference in milliseconds. If the milliseconds is passed to duration, it is used to calculate duration which is correct. However. when you pass the same milliseconds to the moment(), it calculates the date that is milliseconds from(after) epoch/unix time that is January 1, 1970 (midnight UTC/GMT). That is why you get 1969 as the year together with wrong hour.

    duration.get("hours") +":"+ duration.get("minutes") +":"+ duration.get("seconds")
    

    So, I think this is how you should do it since moment.js does not offer format function for duration. Or you can write a simple wrapper to make it easier/prettier.

    0 讨论(0)
提交回复
热议问题