Luxon: Array of Dates from Interval

Created on 10 Feb 2020  路  1Comment  路  Source: moment/luxon

Following the simple example:

        let start= DateTime.local();
        let end= DateTime.local(2020, 10, 17);
        let interval = Interval.fromDateTimes(start, end);

How can I loop over the dates of the interval? I cannot find it anything like that in the Docs. Anyone?

Most helpful comment

There's nothing built-in for that the way there is in Twix, but you can just iterate over the days between the start and the end. Something like:

const { Interval, DateTime} = require("luxon");

// adjust this for your exact needs
function* days(interval) {
  let cursor = interval.start.startOf("day");
  while (cursor < interval.end) {
    yield cursor;
    cursor = cursor.plus({ days: 1 });
  }
}

let start = DateTime.local();
let end = DateTime.local(2020, 10, 17);
let interval = Interval.fromDateTimes(start, end);

// you can iterate over it
for(var d of days(interval)) {
  console.log(d.toISO())
}

// or construct an array out of it
var arr = Array.from(days(interval));
console.log(arr.length);

>All comments

There's nothing built-in for that the way there is in Twix, but you can just iterate over the days between the start and the end. Something like:

const { Interval, DateTime} = require("luxon");

// adjust this for your exact needs
function* days(interval) {
  let cursor = interval.start.startOf("day");
  while (cursor < interval.end) {
    yield cursor;
    cursor = cursor.plus({ days: 1 });
  }
}

let start = DateTime.local();
let end = DateTime.local(2020, 10, 17);
let interval = Interval.fromDateTimes(start, end);

// you can iterate over it
for(var d of days(interval)) {
  console.log(d.toISO())
}

// or construct an array out of it
var arr = Array.from(days(interval));
console.log(arr.length);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

icambron picture icambron  路  5Comments

zbraniecki picture zbraniecki  路  6Comments

emanuelegalbiati picture emanuelegalbiati  路  3Comments

farnoy picture farnoy  路  3Comments

pmcilreavy picture pmcilreavy  路  6Comments