Javascript 将今天的日期与当前的另一个日期进行比较会返回错误的日期,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12593653/
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 10:46:44  来源:igfitidea点击:

Comparing today's date with another date in moment is returning the wrong date, why?

javascriptmomentjs

提问by manafire

I'm using moment.js1.7.0 to try and compare today's date with another date but the difffunction is saying they are 1 day apart for some reason.

我正在使用moment.js1.7.0 尝试将今天的日期与另一个日期进行比较,但该diff函数表示由于某种原因它们相隔 1 天。

code:

代码

var releaseDate = moment("2012-09-25");
var now = moment(); //Today is 2012-09-25, same as releaseDate  
console.log("RELEASE: " + releaseDate.format("YYYY-MM-DD"));
console.log("NOW: " + now.format("YYYY-MM-DD"));
console.log("DIFF: " + now.diff(releaseDate, 'days'));

console:

控制台

RELEASE: 2012-09-25
NOW: 2012-09-25
DIFF: 1 

Ideas?

想法?

回答by RobG

Based on the documentation(and brief testing), moment.js creates wrappers around date objects. The statement:

基于文档(和简短的测试),moment.js 创建了日期对象的包装器。该声明:

var now = moment();

creates a "moment" object that at its heart has a new Date object created as if by new Date(), so hours, minutes and seconds will be set to the current time.

创建一个“时刻”对象,它的核心有一个新的 Date 对象,就像由 一样创建new Date(),因此小时、分钟和秒将设置为当前时间。

The statement:

该声明:

var releaseDate = moment("2012-09-25");

creates a moment object that at its heart has a new Date object created as if by new Date(2012, 8, 25)where the hours, minutes and seconds will all be set to zero for the local time zone.

创建一个 moment 对象,该对象在其核心创建了一个新的 Date 对象,就好像new Date(2012, 8, 25)本地时区的小时、分钟和秒都将设置为零一样。

moment.diffreturns a value based on a the roundeddifference in ms between the two dates. To see the full value, pass trueas the third parameter:

moment.diff返回基于两个日期之间以毫秒为单位的舍入差值的值。要查看完整值,请true作为第三个参数传递:

 now.diff(releaseDate, 'days', true)
 ------------------------------^

So it will depend on the time of day when the code is run and the local time zone whether now.diff(releaseDate, 'days')is zero or one, even when run on the same local date.

因此now.diff(releaseDate, 'days'),即使在同一本地日期运行,这也取决于代码运行的时间和本地时区是零还是一。

If you want to compare just dates, then use:

如果您只想比较日期,请使用:

var now = moment().startOf('day'); 

which will set the time to 00:00:00 in the local time zone.

这会将时间设置为本地时区的 00:00:00。

回答by zacharydl

RobG's answer is correct for the question, so this answer is just for those searching how to compare dates in momentjs.

RobG 的回答对这个问题是正确的,所以这个答案只适用于那些搜索如何在 momentjs 中比较日期的人。

I attempted to use startOf('day') like mentioned above:

我尝试使用上面提到的 startOf('day') :

var compare = moment(dateA).startOf('day') === moment(dateB).startOf('day');

This did not work for me.

这对我不起作用。

I had to use isSame:

我不得不使用isSame

var compare = moment(dateA).isSame(dateB, 'day');