如何在JavaScript中检查两个日期是否在同一天

How do you detect if a date object instance in JavaScript refers to the same day of another date object?

如何检测JavaScript中的日期对象实例是否引用了另一个日期对象的同一天?

JavaScript does not provide this functionality in its standard library, but you can implement it using the methods

JavaScript在其标准库中未提供此功能,但是您可以使用以下方法来实现它

  • getDate() returns the day

    getDate()返回日期

  • getMonth() returns the month

    getMonth()返回月份

  • getFullYear() returns the 4-digits year

    getFullYear()返回4位数字的年份

This is a simple function you can copy/paste to do the check:

这是一个简单的功能,您可以复制/粘贴以进行检查:

const datesAreOnSameDay = (first, second) =>
    first.getFullYear() === second.getFullYear() &&
    first.getMonth() === second.getMonth() &&
    first.getDate() === second.getDate();

Example usage:

用法示例:

datesAreOnSameDay(new Date(), new Date()) //true

翻译自: https://flaviocopes.com/how-to-check-dates-same-day-javascript/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值