教程 > typescript 教程 > 阅读:1699

在typescript中如何比较日期——迹忆客-ag捕鱼王app官网

要在 typescript 中比较日期:

  1. 在每个日期调用 gettime() 方法以获取时间戳。
  2. 比较日期的时间戳。
  3. 如果某个日期的时间戳大于另一个日期的时间戳,则该日期在后面。
const date1 = new date('2022-04-16t06:55:31.820z');
const date2 = new date('2022-04-26t09:30:24.820z');
// typescript 中比较日期
if (date1.gettime() === date2.gettime()) {
  console.log('dates are the same');
} else {
  console.log('dates are not the same');
}
if (date1.gettime() > date2.gettime()) {
  console.log('date1 comes after date2');
} else if (date1.gettime() < date2.gettime()) {
  console.log('date2 comes after date1');
} else {
  console.log('dates are the same');
}
// ---------------------------------------------------------
// 在 typescript 中比较没有时间的日期 
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 {
  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 {
  console.log('date2 comes after date1');
}

上面代码运行结果如下

typescript 比较日期

上面代码片段显示了 2 个示例:

  1. 如何比较 typescript 中的日期,包括时间。
  2. 如何在没有时间的情况下比较 typescript 中的日期。

我们使用 date() 构造函数创建了 2 个日期对象。 第一个日期是 2022 年 4 月 16 日,第二个日期是 2022 年 4 月 26 日。

gettime 方法返回 1970 年 1 月 1 日 00:00:00 和给定日期之间经过的毫秒的时间戳。

如果一个日期的时间戳大于另一个日期的时间戳,则第一个日期在第二个日期之后。

另一方面,如果两个日期的时间戳相等,则这两个日期表示完全相同的时刻(包括小时、分钟、秒和毫秒)。

我们可以比较 gettime() 的输出,就像比较 typescript 中的数字一样。

有时我们需要比较两个日期而忽略时间分量。

要在 typescript 中比较两个没有时间的日期:

  1. 创建每个日期的副本。
  2. 使用 setutchours() 方法将复制日期的时间设置为午夜。
  3. 比较在日期上调用 gettime() 方法的输出。
const date1 = new date('2022-04-16t06:55:31.820z');
const date2 = new date('2022-04-16t09: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 {
  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 {
  console.log('date2 comes after date1');
}

示例中的两个日期对象表示 2022 年 4 月 16 日。

为了能够比较日期并忽略时间,我们必须将日期的时间重置为午夜(或简单的相同的小时、分钟、秒和毫秒)。

setutchours 方法将小时、分钟、秒和毫秒作为参数,并根据通用时间设置给定日期的值。

但是,setutchours 方法会更改 date 对象,这可能不是您想要的。

这就是为什么我们使用 gettime 方法来获取从 1970 年 1 月 1 日 00:00:00 到给定日期之间经过的毫秒的时间戳。

我们可以将时间戳传递给 date() 构造函数以创建 date 对象的副本。

我们使用 setutchours 方法将日期的时间设置为午夜,因此我们可以忽略时间来比较日期。

gettime 方法返回一个数字,表示 unix 纪元和给定日期之间经过的毫秒数。

// 结果 1650094409568
console.log(new date().gettime());

如果我们有两个日期存储相同的年、月和日,那么时间戳将相等,因为我们将两个日期的时间都设置为午夜。

如果一个日期大于另一个日期,那么它的时间戳将存储一个更大的数字,因为在 unix 纪元和特定日期之间已经过去了更多的时间。

查看笔记

扫码一下
查看教程更方便
网站地图