Iterate through a range of dates in NodeJS

前端 未结 5 1650
抹茶落季
抹茶落季 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:00

    Here's one solution without external libraries:

      var start = new Date('October 1, 2020 03:00:00Z');
      var now = new Date();
    
      for (var d = start; d < now; d.setDate(d.getDate() + 1)) {
        console.log(d);
      }
    

    Result:

    2020-10-01T03:00:00.000Z
    2020-10-02T03:00:00.000Z
    2020-10-03T03:00:00.000Z
    2020-10-04T03:00:00.000Z
    2020-10-05T03:00:00.000Z
    2020-10-06T03:00:00.000Z
    

    The Z at the end of the first date is for UTC. If you want your time zone, just remove the Z.

提交回复
热议问题