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

前端 未结 5 1466
时光取名叫无心
时光取名叫无心 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条回答
  •  余生分开走
    2020-12-21 09:03

    Update: IE10, FX30 (and likely more) will understand "18 Aug 2010" without the dashes - Chrome handles either

    so Date.parse("18-Aug-2010".replace("/-/g," ")) works in these browsers (and more)

    Live Demo

    Hence

    function compareDates(str1,str2) {
      var d1 = Date.parse(str1.replace("/-/g," ")),
          d2 = Date.parse(str2.replace("/-/g," "));
      return d1

提交回复
热议问题