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 daygetDate()
返回日期getMonth()
returns the monthgetMonth()
返回月份getFullYear()
returns the 4-digits yeargetFullYear()
返回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/