How to compare date string using javascript

前端 未结 3 1521
醉梦人生
醉梦人生 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 23:12

    Sun Apr 07 2019 00:00:00 GMT-0300

    2019-04-08T03:00:00.000Z

    note that both are the same day

    No, they are not.

    You can convert them both to ISO string and just compare their date parts as strings (if I understood the question correctly, you want to compare date only, without time):

    function isSameDate(date1, date2) {
      const [d1, ] = (new Date(date1)).toISOString().split('T');
      const [d2, ] = (new Date(date2)).toISOString().split('T');
      return d1 === d2;
    }
    

提交回复
热议问题