Validate two dates of this “dd-MMM-yyyy” format in javascript

前端 未结 5 1461
时光取名叫无心
时光取名叫无心 2020-12-21 08:33

I have two dates 18-Aug-2010 and 19-Aug-2010 of this format. How to find whether which date is greater?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 09:13

    The native Date can parse "MMM+ dd yyyy", which gives:

    function parseDMY(s){
      return new Date(s.replace(/^(\d+)\W+(\w+)\W+/, '$2 $1 '));
    }
    +parseDMY('19-August-2010') == +new Date(2010, 7, 19) // true
    parseDMY('18-Aug-2010') < parseDMY('19-Aug-2010')     // true
    

提交回复
热议问题