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


当前回答

<html lang="en">
<head>
<script>
function getDateDiff(time1, time2) {
  var str1= time1.split('/');
  var str2= time2.split('/');

  //                yyyy   , mm       , dd
  var t1 = new Date(str1[2], str1[0]-1, str1[1]);
  var t2 = new Date(str2[2], str2[0]-1, str2[1]);

  var diffMS = t1 - t2;    
  console.log(diffMS + ' ms');

  var diffS = diffMS / 1000;    
  console.log(diffS + ' ');

  var diffM = diffS / 60;
  console.log(diffM + ' minutes');

  var diffH = diffM / 60;
  console.log(diffH + ' hours');

  var diffD = diffH / 24;
  console.log(diffD + ' days');
  alert(diffD);
}

//alert(getDateDiff('10/18/2013','10/14/2013'));
</script>
</head>
<body>
  <input type="button" 
       onclick="getDateDiff('10/18/2013','10/14/2013')" 
       value="clickHere()" />

</body>
</html>

其他回答

我做了一个下面的函数来得到现在和“2021-02-26T21:50:42.123”之间的区别。

差值返回以毫秒为单位的答案,所以我使用以下公式进行转换:

(1000 * 3600 * 24).

function getDiff(dateAcquired) {
      let calDiff = Math.floor(
        (new Date() - new Date(dateAcquired)) / (1000 * 3600 * 24)
      );
      return calDiff;
    }
    console.log(getDiff("2021-02-26T21:50:42.123"));
            // the idea is to get time left for new year.
           // Not considering milliseconds as of now, but that 
           //  can be done
           
            var newYear = '1 Jan 2023';
            const secondsInAMin = 60;
            const secondsInAnHour = 60 * secondsInAMin;
            const secondsInADay = 24 * secondsInAnHour;

            function DateDiffJs() {
                var newYearDate = new Date(newYear);
                var currDate = new Date();

                var remainingSecondsInDateDiff = (newYearDate - currDate) / 1000;
                var days = Math.floor(remainingSecondsInDateDiff / secondsInADay);

                var remainingSecondsAfterDays = remainingSecondsInDateDiff - (days * secondsInADay);
                var hours = Math.floor(remainingSecondsAfterDays / secondsInAnHour);

                var remainingSecondsAfterhours = remainingSecondsAfterDays - (hours * secondsInAnHour);
                var mins = Math.floor(remainingSecondsAfterhours / secondsInAMin);

                var seconds = Math.floor(remainingSecondsAfterhours - (mins * secondsInAMin));


                console.log(`days :: ${days}`)
                console.log(`hours :: ${hours}`)
                console.log(`mins :: ${mins}`)
                console.log(`seconds :: ${seconds}`)

            }

            DateDiffJs();
var d1=new Date(2011,0,1); // jan,1 2011
var d2=new Date(); // now

var diff=d2-d1,sign=diff<0?-1:1,milliseconds,seconds,minutes,hours,days;
diff/=sign; // or diff=Math.abs(diff);
diff=(diff-(milliseconds=diff%1000))/1000;
diff=(diff-(seconds=diff%60))/60;
diff=(diff-(minutes=diff%60))/60;
days=(diff-(hours=diff%24))/24;

console.info(sign===1?"Elapsed: ":"Remains: ",
             days+" days, ",
             hours+" hours, ",
             minutes+" minutes, ",
             seconds+" seconds, ",
             milliseconds+" milliseconds.");

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

你问题的答案是:

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

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

function DateDiff(b, e)
{
    let
        endYear = e.getFullYear(),
        endMonth = e.getMonth(),
        years = endYear - b.getFullYear(),
        months = endMonth - b.getMonth(),
        days = e.getDate() - b.getDate();
    if (months < 0)
    {
        years--;
        months += 12;
    }
    if (days < 0)
    {
        months--;
        days += new Date(endYear, endMonth, 0).getDate();
    }
    return [years, months, days];
}

[years, months, days] = DateDiff(
    new Date("October 21, 1980"),
    new Date("July 11, 2017")); // 36 8 20