How to compare date string using javascript

前端 未结 3 1474
醉梦人生
醉梦人生 2021-01-23 22:46

I have the following string value of a date, Sun Apr 07 2019 00:00:00 GMT-0300, and I need to compare with the following date form

3条回答
  •  不思量自难忘°
    2021-01-23 22:57

    Build a date from the strings and compare the days (ie number of seconds since epoch / number of seconds in a day):

    const sameDay = (dateString1, dateString2) => {
      let time1 = (new Date(dateString1)).getTime();
      let time2 = (new Date(dateString2)).getTime();
      return Math.floor(Math.abs((time1-time2))/(1000*60*60*24))==0;
    }
    
    console.log(
      sameDay('Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)','2019-04-08T03:00:00.000Z'),
      sameDay('Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)','2019-04-13T03:00:00.000Z'),
    );

提交回复
热议问题