Create Regular Time Intervals Between Two Dates
2 min read

Create Regular Time Intervals Between Two Dates

Learn to create regular time intervals between two dates in JavaScript.

I will be using luxon npm plugin to build this functionality. Luxon is a library for working with dates and times in JavaScript.

First, let's import the plugin.

const { DateTime } = require('luxon');

Let's build a function that returns time intervals.

To write such function we need three parameters

  1. Start date
  2. End date
  3. Duration (at which intervals to create)

Start date and End date should be a standard date value and Duration should be in the format supported by luxon.

Here are the supported duration format for luxon.

// days
{
	days: 2
}

// months
{
	months: 2
}

// years
{
	years: 2
}
luxon duration formats

Check out here for all duration formats: https://moment.github.io/luxon/api-docs/index.html#duration

Here is the function

function getIntervals(start, end, duration) { 
    // Step-1
    // Convert input dates to luxon DateTime object
    const startDate = DateTime.fromJSDate(new Date(start));
    const endDate = DateTime.fromJSDate(new Date(end));
    
    // Step-2
    // Initialise 
    const allRecurrences = [
        startDate,
    ];
    
    // Step-3
    // Create all next date occurence till end date
    let nextOccurrence = startDate.plus(duration);
    while (nextOccurrence.diff(endDate, 'seconds').seconds < 0) {
        allRecurrences.push(nextOccurrence);
        nextOccurrence = nextOccurrence.plus(duration);
    }

    // Step-4
    // Push the endDate
    // Note: The interval between the endDate and previous to endDate may not be the same to other. You may decide whether you want endDate in the list or not.
    if (nextOccurrence.diff(endDate, 'seconds').seconds >= 0) {
        allRecurrences.push(endDate);
    }

    // Step-5
    // Create final output
    // Each interval object includes start and end of the interval.
    const intervalsOutput = [];
    for (let i = 0; i < allRecurrences.length - 1; i += 1) {
        const currentRecurrence = allRecurrences[i];
        const nextRecurrence = allRecurrences[i + 1];
        intervalsOutput.push({
            start: currentRecurrence.toJSDate(),
            end: nextRecurrence.minus({
                seconds: 1,
            }).toJSDate(),
        });
    }

    // Step-6
    // Return
    return intervalsOutput;
}
Function getIntervals

Call the function

getIntervals('01/01/2020 00:00:00+0', '05/05/2021 00:00:00+0', {months: 2});

The function will return all the intervals as an array of objects.

Here is the sample output

[
  { start: 2020-01-01T00:00:00.000Z, end: 2020-02-29T23:59:59.000Z },
  { start: 2020-03-01T00:00:00.000Z, end: 2020-04-30T23:59:59.000Z },
  { start: 2020-05-01T00:00:00.000Z, end: 2020-06-30T23:59:59.000Z },
  { start: 2020-07-01T00:00:00.000Z, end: 2020-08-31T23:59:59.000Z },
  { start: 2020-09-01T00:00:00.000Z, end: 2020-10-31T23:59:59.000Z },
  { start: 2020-11-01T00:00:00.000Z, end: 2020-12-31T23:59:59.000Z },
  { start: 2021-01-01T00:00:00.000Z, end: 2021-02-28T23:59:59.000Z },
  { start: 2021-03-01T00:00:00.000Z, end: 2021-04-30T23:59:59.000Z },
  { start: 2021-05-01T00:00:00.000Z, end: 2021-05-04T23:59:59.000Z }
]
Output of getIntervals

Thanks for reading my post.