Number of days between two dates in ISO8601 date format

后端 未结 4 431
谎友^
谎友^ 2021-01-14 17:29

I want to do same thing as How do I get the number of days between two dates in JavaScript?

but I want do the same on this date format: 2000-12-31.

4条回答
  •  滥情空心
    2021-01-14 17:59

    function daysBetween(date1String, date2String){
      var d1 = new Date(date1String);
      var d2 = new Date(date2String);
      return (d2-d1)/(1000*3600*24);
    }
    
    console.log( daysBetween('2000-12-31', '2005-05-04') );  //-> 1585
    

    ISO8601 date strings are recognized by JavaScript directly. No need to parse them yourself.

提交回复
热议问题