在 JavaScript 中比较没有时间的日期
要比较没有时间的日期。
- 创建每个日期的副本。
-
使用
setUTCHours()
方法将复制的日期上的时间设置为午夜。 -
比较在这些日期上调用
getTime()
方法的输出。
const date1 = new Date('2022-01-23T06:55:31.820Z');
const date2 = new Date('2022-01-23T09:30:24.820Z');
const date1WithoutTime = new Date(date1.getTime());
const date2WithoutTime = new Date(date2.getTime());
date1WithoutTime.setUTCHours(0, 0, 0, 0);
date2WithoutTime.setUTCHours(0, 0, 0, 0);
if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) {
// ✅ This runs 👇️ (dates are the same)
console.log('dates are the same');
} else {
console.log('dates are not the same');
}
if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) {
console.log('date1 comes after date2');
} else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) {
// ✅ This runs 👇️ (dates are the same)
console.log('date1 and date2 are the same');
} else {
console.log('date2 comes after date1');
}
我们使用Date()
构造函数创建了2个 Date 对象,它们都存储了2022年1月23日的时间。
为了能够比较日期而忽略时间,我们必须将日期的时间重置为午夜(或者简单地说是相同的小时、分钟、秒钟和毫秒)。
setUTCHours
方法将小时、分钟、秒和毫秒作为参数,并根据世界时间在给定的日期上设置这些值。
然而,
setUTCHours
方法改变了Date对象,这可能不是你想要的。
这就是为什么我们使用 getTime
方法来获取1970年1月1日00:00:00和给定日期之间经过的毫秒的时间戳。
我们可以将时间戳传递给Date()
构造函数来创建一个Date对象的副本。
我们使用
setUTCHours
方法将日期的时间设置为午夜,这样我们就可以比较日期而忽略时间。
getTime
方法返回一个数字,代表Unix Epoch和给定日期之间经过的毫秒。
console.log(new Date().getTime());
如果我们有两个存储相同年份、月份和日期的日期,那么时间戳将是相等的,因为我们将两个日期的时间设置为午夜。
如果一个日期大于另一个,那么它的时间戳将存储一个更大的数字,因为在Unix Epoch和特定日期之间经过了更多时间。
我们可以比较getTime()
的输出,就像我们在JavaScript中比较数字一样。
下面是一个例子,date1
存储的是2022年2月23日的数值,date2
存储的是2022年4月27日的数值。
const date1 = new Date('2022-02-23T06:55:31.820Z');
const date2 = new Date('2022-04-27T09:30:24.820Z');
const date1WithoutTime = new Date(date1.getTime());
const date2WithoutTime = new Date(date2.getTime());
date1WithoutTime.setUTCHours(0, 0, 0, 0);
date2WithoutTime.setUTCHours(0, 0, 0, 0);
if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) {
console.log('dates are the same');
} else {
// ✅ This runs 👇️ (dates are NOT the same)
console.log('dates are not the same');
}
if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) {
console.log('date1 comes after date2');
} else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) {
console.log('date1 and date2 are the same');
} else {
// ✅ This runs 👇️ (date2 comes after date1)
console.log('date2 comes after date1');
}
date2
变量中的值在date1
变量中的值之后,因此它的时间戳是一个更大的数字,所以else
语句在两个条件中都运行。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。