简体   繁体   English

如何在javascript中比较两种不同格式的两种不同日期?

[英]How to compare two different dates which are in two different formats in javascript?

I am trying to compare two different time formats in JavaScript. 我正在尝试比较JavaScript中的两种不同的时间格式。

Day 1: Thu Dec 31 00:00:00 GMT 2020 第1天: Thu Dec 31 00:00:00 GMT 2020

Day 2: Today's date. 第2天:今天的日期。 I am using today=new Date(); 我正在使用today=new Date(); which gives me Thu Sep 21 2017 11:42:17 GMT-0400 (Eastern Daylight Time) 这给了我Thu Sep 21 2017 11:42:17 GMT-0400 (Eastern Daylight Time)

I want to check if Day 1 is today or in future, basically if Day 1 >= Day 2. I am only looking for dates and not datetime. 我想检查第1天是今天还是将来,基本上是如果第1天> =第2天。我只是在寻找日期,而不是日期时间。 Any help is appreciated. 任何帮助表示赞赏。

I think this should work 我认为这应该工作

let myDate = Date.parse('Thu Dec 31 00:00:00 GMT 2020')
console.log('myDate:', myDate)

let today  = Date.parse(new Date())
console.log('Today:', today)

if (myDate >= today) {
  console.log("True")
} else {
  console.log("False")
}

If you use Date.parse() it breaks it down into (I believe) epoch time and makes them comparable. 如果使用Date.parse(),它将分解为(我相信)时代,并使其具有可比性。 This works with a ton of different date formats. 这适用于大量不同的日期格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM