Iterate through a range of dates in NodeJS

前端 未结 5 1622
抹茶落季
抹茶落季 2020-12-23 11:48

I would like to iterate through a range of calender dates, each iteration is +1 day. I would use something built around JodaTime in Java - is there something similar in Node

5条回答
  •  囚心锁ツ
    2020-12-23 12:03

    Use the https://github.com/JerrySievert/node-date-utils framework, then you can iterate easily like this:

    require('date-utils');
    
    var d = new Date('2013-01-01');
    var e = new Date('2013-06-01');
    
    for(var i = d; i.isBefore(e); i.addDays(1)) {
      console.log(i.toFormat("YYYY-MM-DD"));  
    }
    

提交回复
热议问题