Check time difference in Javascript

后端 未结 18 1127
梦毁少年i
梦毁少年i 2020-11-22 04:27

How would you check time difference from two text-boxes in Javascript?

18条回答
  •  隐瞒了意图╮
    2020-11-22 04:37

    This is an addition to dmd733's answer. I fixed the bug with Day duration (well I hope I did, haven't been able to test every case).

    I also quickly added a String property to the result that holds the general time passed (sorry for the bad nested ifs!!). For example if used for UI and indicating when something was updated (like a RSS feed). Kind of out of place but nice-to-have:

    function getTimeDiffAndPrettyText(oDatePublished) {
    
      var oResult = {};
    
      var oToday = new Date();
    
      var nDiff = oToday.getTime() - oDatePublished.getTime();
    
      // Get diff in days
      oResult.days = Math.floor(nDiff / 1000 / 60 / 60 / 24);
      nDiff -= oResult.days * 1000 * 60 * 60 * 24;
    
      // Get diff in hours
      oResult.hours = Math.floor(nDiff / 1000 / 60 / 60);
      nDiff -= oResult.hours * 1000 * 60 * 60;
    
      // Get diff in minutes
      oResult.minutes = Math.floor(nDiff / 1000 / 60);
      nDiff -= oResult.minutes * 1000 * 60;
    
      // Get diff in seconds
      oResult.seconds = Math.floor(nDiff / 1000);
    
      // Render the diffs into friendly duration string
    
      // Days
      var sDays = '00';
      if (oResult.days > 0) {
          sDays = String(oResult.days);
      }
      if (sDays.length === 1) {
          sDays = '0' + sDays;
      }
    
      // Format Hours
      var sHour = '00';
      if (oResult.hours > 0) {
          sHour = String(oResult.hours);
      }
      if (sHour.length === 1) {
          sHour = '0' + sHour;
      }
    
      //  Format Minutes
      var sMins = '00';
      if (oResult.minutes > 0) {
          sMins = String(oResult.minutes);
      }
      if (sMins.length === 1) {
          sMins = '0' + sMins;
      }
    
      //  Format Seconds
      var sSecs = '00';
      if (oResult.seconds > 0) {
          sSecs = String(oResult.seconds);
      }
      if (sSecs.length === 1) {
          sSecs = '0' + sSecs;
      }
    
      //  Set Duration
      var sDuration = sDays + ':' + sHour + ':' + sMins + ':' + sSecs;
      oResult.duration = sDuration;
    
      // Set friendly text for printing
      if(oResult.days === 0) {
    
          if(oResult.hours === 0) {
    
              if(oResult.minutes === 0) {
                  var sSecHolder = oResult.seconds > 1 ? 'Seconds' : 'Second';
                  oResult.friendlyNiceText = oResult.seconds + ' ' + sSecHolder + ' ago';
              } else { 
                  var sMinutesHolder = oResult.minutes > 1 ? 'Minutes' : 'Minute';
                  oResult.friendlyNiceText = oResult.minutes + ' ' + sMinutesHolder + ' ago';
              }
    
          } else {
              var sHourHolder = oResult.hours > 1 ? 'Hours' : 'Hour';
              oResult.friendlyNiceText = oResult.hours + ' ' + sHourHolder + ' ago';
          }
      } else { 
          var sDayHolder = oResult.days > 1 ? 'Days' : 'Day';
          oResult.friendlyNiceText = oResult.days + ' ' + sDayHolder + ' ago';
      }
    
      return oResult;
    }
    

提交回复
热议问题