最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

I'm using moment.js 1.7.0 to try and compare today's date with another date but the diff function is saying they are 1 day apart for some reason.

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?

I'm using moment.js 1.7.0 to try and compare today's date with another date but the diff function is saying they are 1 day apart for some reason.

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?

Share Improve this question edited Jul 8, 2019 at 8:54 Liam 29.6k28 gold badges137 silver badges200 bronze badges asked Sep 26, 2012 at 2:13 manafiremanafire 6,0844 gold badges45 silver badges53 bronze badges 2
  • i have 1.7.0, i tested your code and i got DIFF: 0 - are you sure nothing was changed in your moment.js? – rationalboss Commented Sep 26, 2012 at 2:25
  • I think it's something to do with timezones or utc/local. If I log the .hours() of releaseDate and now it yields different results for me. If I use var now = moment().sod() (docs) it currently works as expected, but I'm not sure how much I trust that. – manafire Commented Sep 26, 2012 at 2:30
Add a comment  | 

2 Answers 2

Reset to default 64

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

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.

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.diff returns a value based on a the rounded difference in ms between the two dates. To see the full value, pass true as the third parameter:

 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.

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.

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

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

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

This did not work for me.

I had to use isSame:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论