例如,在输入框中给定两个日期:

<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>

<script>
  alert(datediff("day", first, second)); // what goes here?
</script>

如何在JavaScript中获得两个日期之间的天数?


当前回答

使用以下公式可以计算出两个不同TZs的休止日期之间的完整证明天数差:

var start = new Date('10/3/2015'); var end = new Date('11/2/2015'); Var天数= (end - start) / 1000 / 60 / 60 / 24; console.log(天); //实际上是30;但由于夏令时,将显示31.0xxx //你需要抵消如下 days = days - (end.getTimezoneOffset() - start.getTimezoneOffset()) / (60 * 24); console.log(天);

其他回答

Date.prototype.days =函数(到){ 返回Math.abs(Math.floor(to.getTime() / (3600 * 24 * 1000)) - Math.floor(this.getTime() / (3600 * 24 * 1000))) } console.log(新日期(“2014/05/20”)。天(新日期(2014/05/23)));// 3天 console.log(新日期(“2014/05/23”)。天(新日期(2014/05/20)));// 3天

我使用下面的代码来试验新闻帖子的发布日期功能。我根据发布日期和当前日期计算分钟、小时、天或年。

var startDate= new Date("Mon Jan 01 2007 11:00:00");
var endDate  =new Date("Tue Jan 02 2007 12:50:00");
var timeStart = startDate.getTime();
var timeEnd = endDate.getTime();
var yearStart = startDate.getFullYear();
var yearEnd   = endDate.getFullYear();
if(yearStart == yearEnd)
 {
  var hourDiff = timeEnd - timeStart; 
  var secDiff = hourDiff / 1000;
  var minDiff = hourDiff / 60 / 1000; 
  var hDiff = hourDiff / 3600 / 1000; 
  var myObj = {};
  myObj.hours = Math.floor(hDiff);
  myObj.minutes = minDiff  
  if(myObj.hours >= 24)
   {
    console.log(Math.floor(myObj.hours/24) + "day(s) ago")
   } 
 else if(myObj.hours>0)
  {
   console.log(myObj.hours +"hour(s) ago")
  }
 else
  {
   console.log(Math.abs(myObj.minutes) +"minute(s) ago")
  }
}
else
{
var yearDiff = yearEnd - yearStart;
console.log( yearDiff +" year(s) ago");
}

使用以下公式可以计算出两个不同TZs的休止日期之间的完整证明天数差:

var start = new Date('10/3/2015'); var end = new Date('11/2/2015'); Var天数= (end - start) / 1000 / 60 / 60 / 24; console.log(天); //实际上是30;但由于夏令时,将显示31.0xxx //你需要抵消如下 days = days - (end.getTimezoneOffset() - start.getTimezoneOffset()) / (60 * 24); console.log(天);

这可能不是最优雅的解决方案,但我认为它似乎用一段相对简单的代码就回答了这个问题。你不能用这样的词吗?

function dayDiff(startdate, enddate) {
  var dayCount = 0;

  while(enddate >= startdate) {
    dayCount++;
    startdate.setDate(startdate.getDate() + 1);
  }

return dayCount; 
}

这是假设您将日期对象作为参数传递。

function formatDate(seconds, dictionary) {
    var foo = new Date;
    var unixtime_ms = foo.getTime();
    var unixtime = parseInt(unixtime_ms / 1000);
    var diff = unixtime - seconds;
    var display_date;
    if (diff <= 0) {
        display_date = dictionary.now;
    } else if (diff < 60) {
        if (diff == 1) {
            display_date = diff + ' ' + dictionary.second;
        } else {
            display_date = diff + ' ' + dictionary.seconds;
        }
    } else if (diff < 3540) {
        diff = Math.round(diff / 60);
        if (diff == 1) {
            display_date = diff + ' ' + dictionary.minute;
        } else {
            display_date = diff + ' ' + dictionary.minutes;
        }
    } else if (diff < 82800) {
        diff = Math.round(diff / 3600);
        if (diff == 1) {
            display_date = diff + ' ' + dictionary.hour;
        } else {
            display_date = diff + ' ' + dictionary.hours;
        }
    } else {
        diff = Math.round(diff / 86400);
        if (diff == 1) {
            display_date = diff + ' ' + dictionary.day;
        } else {
            display_date = diff + ' ' + dictionary.days;
        }
    }
    return display_date;
}