我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?


当前回答

使用Moment.js进行所有与JavaScript相关的日期时间计算

你问题的答案是:

var a = moment([2007, 0, 29]);   
var b = moment([2007, 0, 28]);    
a.diff(b) // 86400000  

完整的细节可以在这里找到

其他回答

另一种解决方案是将difference转换为一个新的Date对象,并获得该日期的年(与1970年不同)、月、日等。

var date1 = new Date(2010, 6, 17);
var date2 = new Date(2013, 12, 18);
var diff = new Date(date2.getTime() - date1.getTime());
// diff is: Thu Jul 05 1973 04:00:00 GMT+0300 (EEST)

console.log(diff.getUTCFullYear() - 1970); // Gives difference as year
// 3

console.log(diff.getUTCMonth()); // Gives month count of difference
// 6

console.log(diff.getUTCDate() - 1); // Gives day count of difference
// 4

所以差异就像“3年6个月零4天”。如果你想以一种人类可读的风格呈现不同,这将对你有所帮助。

this should work just fine if you just need to show what time left, since JavaScript uses frames for its time you'll have get your End Time - The Time RN after that we can divide it by 1000 since apparently 1000 frames = 1 seconds, after that you can use the basic math of time, but there's still a problem to this code, since the calculation is static, it can't compensate for the different day total in a year (360/365/366), the bunch of IF after the calculation is to make it null if the time is lower than 0, hope this helps even though it's not exactly what you're asking :)

var now = new Date();
var end = new Date("End Time");
var total = (end - now) ;
var totalD =  Math.abs(Math.floor(total/1000));

var years = Math.floor(totalD / (365*60*60*24));
var months = Math.floor((totalD - years*365*60*60*24) / (30*60*60*24));
var days = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24)/ (60*60*24));
var hours = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24)/ (60*60));
var minutes = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24 - hours*60*60)/ (60));
var seconds = Math.floor(totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24 - hours*60*60 - minutes*60);

var Y = years < 1 ? "" : years + " Years ";
var M = months < 1 ? "" : months + " Months ";
var D = days < 1 ? "" : days + " Days ";
var H = hours < 1 ? "" : hours + " Hours ";
var I = minutes < 1 ? "" : minutes + " Minutes ";
var S = seconds < 1 ? "" : seconds + " Seconds ";
var A = years == 0 && months == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0 ? "Sending" : " Remaining";

document.getElementById('txt').innerHTML = Y + M + D + H + I + S + A;
var date1 = new Date("06/30/2019");
var date2 = new Date("07/30/2019");
  
// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime();
  
// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
  
//To display the final no. of days (result)
document.write("Total number of days between dates  <br>"
               + date1 + "<br> and <br>" 
               + date2 + " is: <br> " 
               + Difference_In_Days);

加上@paresh mayani的答案,像Facebook一样工作-显示了以秒/分钟/小时/周/月/年为单位流逝了多少时间

var DateDiff = {

  inSeconds: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/1000);
    },


  inMinutes: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/60000);
    },

  inHours: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/3600000);
    },

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}







    var dString = "May, 20, 1984"; //will also get (Y-m-d H:i:s)
    
    var d1 = new Date(dString);
    var d2 = new Date();
    
    var timeLaps = DateDiff.inSeconds(d1, d2);
    var dateOutput = "";
    
    
    if (timeLaps<60)
    {
      dateOutput = timeLaps+" seconds";
    }
    else  
    {
      timeLaps = DateDiff.inMinutes(d1, d2);
      if (timeLaps<60)
      {
        dateOutput = timeLaps+" minutes";
      }
      else
      {
        timeLaps = DateDiff.inHours(d1, d2);
        if (timeLaps<24)
        {
          dateOutput = timeLaps+" hours";
        }
        else
        {
            timeLaps = DateDiff.inDays(d1, d2);
            if (timeLaps<7)
            {
              dateOutput = timeLaps+" days";
            }
            else
            {
                timeLaps = DateDiff.inWeeks(d1, d2);
                if (timeLaps<4)
                {
                  dateOutput = timeLaps+" weeks";
                }
                else
                {
                    timeLaps = DateDiff.inMonths(d1, d2);
                    if (timeLaps<12)
                    {
                      dateOutput = timeLaps+" months";
                    }
                    else
                    {
                      timeLaps = DateDiff.inYears(d1, d2);
                      dateOutput = timeLaps+" years";
                    }
                }
            }
        }
      }
    }
    
    alert (dateOutput);

假设你有两个Date对象,你可以减去它们,以毫秒为单位得到差值:

var difference = date2 - date1;

从那里,您可以使用简单的算术来推导其他值。