how to compare two string dates in javascript?

前端 未结 6 1902
暗喜
暗喜 2020-12-03 04:06

I have two string dates in the format of m/d/yyyy. For example, “11/1/2012”, “1/2/2013”. I am writing a function in JavaScript to compare two string dates. The signature of

6条回答
  •  执念已碎
    2020-12-03 04:43

    You can simply compare 2 strings

    function isLater(dateString1, dateString2) {
      return dateString1 > dateString2
    }
    

    Then

    isLater("2012-12-01", "2012-11-01")
    

    returns true while

    isLater("2012-12-01", "2013-11-01")
    

    returns false

提交回复
热议问题