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
- Start date
- End date
- 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.
Check out here for all duration formats: https://moment.github.io/luxon/api-docs/index.html#duration
Here is the function
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
Thanks for reading my post.