Javascript Moment.js - 两个日期的天数差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36600687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:16:00  来源:igfitidea点击:

Moment.js - two dates difference in number of days

javascriptmomentjs

提问by Milo? ?akonovi?

I get incorrect results when trying to find numeric difference between two dates:

尝试查找两个日期之间的数字差异时,我得到了错误的结果:

var startDate = moment( $('[name="date-start"]').val(), "DD.MM.YYYY"), // $('[name="date-start"]').val() === "13.04.2016"
endDate       = moment( $('[name="date-end"]'  ).val(), "DD.MM.YYYY"); // $('[name="date-end"]').val() === "28.04.2016"

var diff = startDate.diff(endDate);

console.log( moment(diff).format('E') );

Between 13.04.2016and 28.04.2016I shouldn't get that difference is 3or 2days...

13.04.201628.04.2016我之间不应该有32天的差异...

I've tried to multiple combinations:

我尝试了多种组合:

  • swap startDate.diff(endDate)with endDate.diff(startDate)
  • format('E')with something I've come up searching the SO
  • 交换startDate.diff(endDate)endDate.diff(startDate)
  • format('E')我想出一些东西来搜索 SO

result: all the time I get that difference is 3 or 2 days.

结果:我一直得到这种差异是 3 或 2 天。

What am I doing wrong? Thanks in advance.

我究竟做错了什么?提前致谢。

回答by dubes

From the moment.js docs: format('E')stands for day of week. thus your diff is being computed on which day of the week, which has to be between 1 and 7.

从 moment.js docs:format('E')代表星期几。因此,您的差异是在一周中的哪一天计算的,必须在 1 到 7 之间。

From the moment.js docs again, here is what they suggest:

再次从 moment.js 文档中,这是他们的建议:

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

Here is a JSFiddle for your particular case:

这是针对您的特定情况的 JSFiddle:

$('#test').click(function() {
  var startDate = moment("13.04.2016", "DD.MM.YYYY");
  var endDate = moment("28.04.2016", "DD.MM.YYYY");

  var result = 'Diff: ' + endDate.diff(startDate, 'days');

  $('#result').html(result);
});
#test {
  width: 100px;
  height: 100px;
  background: #ffb;
  padding: 10px;
  border: 2px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>

<div id='test'>Click Me!!!</div>
<div id='result'></div>

回答by Muneeb

Here's how you can get the comprehensive full fledge difference of two dates.

以下是如何获得两个日期的全面完整差异。

 function diffYMDHMS(date1, date2) {

    let years = date1.diff(date2, 'year');
    date2.add(years, 'years');

    let months = date1.diff(date2, 'months');
    date2.add(months, 'months');

    let days = date1.diff(date2, 'days');
    date2.add(days, 'days');

    let hours = date1.diff(date2, 'hours');
    date2.add(hours, 'hours');

    let minutes = date1.diff(date2, 'minutes');
    date2.add(minutes, 'minutes');

    let seconds = date1.diff(date2, 'seconds');

    console.log(years + ' years ' + months + ' months ' + days + ' days ' + hours + ' 
    hours ' + minutes + ' minutes ' + seconds + ' seconds'); 

    return { years, months, days, hours, minutes, seconds};
}

回答by Sobhan Das

$('#test').click(function() {
  var startDate = moment("01.01.2019", "DD.MM.YYYY");
  var endDate = moment("01.02.2019", "DD.MM.YYYY");

  var result = 'Diff: ' + endDate.diff(startDate, 'days');

  $('#result').html(result);
});
#test {
  width: 100px;
  height: 100px;
  background: #ffb;
  padding: 10px;
  border: 2px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>

<div id='test'>Click Me!!!</div>
<div id='result'></div>

回答by Sobhan Das

$('#test').click(function() {
  var todayDate = moment("01.01.2019", "DD.MM.YYYY");
  var endDate = moment("08.02.2019", "DD.MM.YYYY");

  var result = 'Diff: ' + todayDate.diff(endDate, 'days');

  $('#result').html(result);
});
#test {
  width: 100px;
  height: 100px;
  background: #ffb;
  padding: 10px;
  border: 2px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>

<div id='test'>Click Me!!!</div>
<div id='result'></div>

回答by Harshit Singhai

const FindDate = (date, allDate) => {

const FindDate = (date, allDate) => {

// moment().diff only works on moment(). Make sure both date and elements in allDate array are in moment format
let nearestDate = -1; 

allDate.some(d => {
    const currentDate = moment(d)
    const difference = currentDate.diff(d); // Or d.diff(date) depending on what you're trying to find
    if(difference >= 0){
        nearestDate = d
    }
});

console.log(nearestDate)

}

}

回答by VonD

the diffmethod returns the difference in milliseconds. Instantiating moment(diff)isn't meaningful.

diff方法以毫秒为单位返回差异。实例化moment(diff)没有意义。

You can define a variable :

您可以定义一个变量:

var dayInMilliseconds = 1000 * 60 * 60 * 24;

and then use it like so :

然后像这样使用它:

diff / dayInMilliseconds // --> 15

Edit

编辑

actually, this is built into the diffmethod, dubes' answer is better

实际上,这是内置在diff方法中的,dubes 的答案更好