admin管理员组文章数量:1399997
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 |2 Answers
Reset to default 64Based 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');
本文标签:
版权声明:本文标题:javascript - Comparing today's date with another date in moment is returning the wrong date, why? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737347859a1981730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
DIFF: 0
- are you sure nothing was changed in your moment.js? – rationalboss Commented Sep 26, 2012 at 2:25.hours()
ofreleaseDate
andnow
it yields different results for me. If I usevar 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