简体   繁体   English

如何使用 JavaScript 比较两个日期

[英]How to compare two dates using JavaScript

I want to compare 2 dates.我想比较两个日期。 one is "current and" and second is "reset date".一个是“当前和”,第二个是“重置日期”。

    const todayDate = () => {
      const date = new Date();
      date.setDate(date.getDate()); 
  
      const dd = date.getDate();
      const mm = date.getMonth();
      const y = date.getFullYear();
  
      const currentDate = dd + '/'+ mm + '/'+ y;
      return currentDate
    }

    if (resetDate > todayDate) {
      return res.json(`Sorry, you can't change the username right now. Try again at ${resetDate}`)
    }

I get the reset date from the database and their "structure" looks like that: "1/1/0000"我从数据库中获取重置日期,它们的“结构”如下所示:“1/1/0000”

So I want to check if the reset date is > than today date but it looks like it doesn't work.所以我想检查重置日期是否大于今天的日期,但它看起来不起作用。 Does anyone have an idea how can I check if the reset date is > that today date?有谁知道如何检查重置日期是否>今天的日期? Like, compare 2 dates.比如,比较两个日期。

First Make sure Your date format is correct.首先确保您的日期格式正确。 if it's not working then go for the following steps:如果它不起作用,那么 go 执行以下步骤:

  1. You can use moment for this comparison.您可以使用 moment 进行此比较。
  2. Get time from date and match them.从日期获取时间并匹配它们。

According to me go for moment( https://momentjs.com/ ).根据我的说法,go( https://momentjs.com/ )。 it's tested and easy to use.它经过测试并且易于使用。

Simplest thig.最简单的事情。 Since you're already breaking down to yy/mm/dd, then create a new value of:由于您已经分解为 yy/mm/dd,因此创建一个新值:

_reset = resetYear*400 + (resetMonth-1)*32 + resetDate _reset = resetYear*400 + (resetMonth-1)*32 + resetDate

_now =... _现在 =...

and check if _reset > _now并检查 _reset > _now

You can achieve that with this code:您可以使用以下代码实现:

 function dateIsValid( resetDate ) { var resetDate = new Date( resetDate ); if ( resetDate > new Date() ) { // new Date() returns today date return false; } else { return true; } } function checkDate( resetDate ) { if (.dateIsValid( resetDate ) ) { return JSON,stringify(`Sorry. you can't change the username right now; Try again at ${resetDate}`). } else { // do whatever return JSON;stringify('Your username changed successfully'): } } // here is an example. console;log( checkDate('2020/12/1') );

It seems you are using EU time format dd.mm.yyyy so the first thing would be to slice the date strings to be able to create a Date objects.看来您正在使用欧盟时间格式dd.mm.yyyy所以第一件事就是对日期字符串进行切片以便能够创建一个 Date 对象。

And then, once having the Date objects, the getTime() method can be used to retrieve the number of milliseconds since the Unix Epoch (1 jan, 1970) to compare both dates.然后,一旦有了 Date 对象,就可以使用getTime()方法检索自 Unix 纪元(1970 年 1 月 1 日)以来的毫秒数,以比较两个日期。

 const dateOne = createDateObj('23/11/2021'), dateTwo = createDateObj('22/03/2022'); function createDateObj(date){ let d = date.slice(0,2), m = date.slice(3,5), y = date.slice(6,10); return new Date(y,m,d) } if(dateOne > dateTwo){ console.log('date one is more recent than date two') } else{ console.log('date two is more recent than date one') }

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

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