Moment.js检查日期是今天 [英] Moment.js Check a date is today

查看:4571
本文介绍了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
}

这篇关于Moment.js检查日期是今天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆