Moment.js检查日期是今天
问题描述:
我如何检查日期实际上是今天(同一日期),而不是一天中的小时数之间的差异?
How do I check a date is actually today (the same date) rather than the difference between hours in a day?
我有三个时间戳作为例子,一个是今天(22/07/14),另外两个是昨天(21/07/14)
I have three timestamps as examples, one is today (22/07/14) and the other two are yesterday (21/07/14)
1406019110000 // today
1405951867000 // yesterday
1405951851000 // yesterday
全部返回false:
timestamp = moment.tz(timestamp, tz.name());
var today = moment('DD/MM/YYYY').isAfter(timestamp, 'DD/MM/YYYY');
console.log(today);
答
您可以使用 isSame )
,将粒度限制为一天:
You can use isSame()
, limiting the granularity to a day:
var today = moment(1406019110000);
var yesterday = moment(1405951867000);
if (today.isSame(yesterday, 'd')) {
// They are on the same day
} else {
// They are not on the same day
}