how to use the eachdayofinterval function from date-fns in javascript

To use the eachDayOfInterval function from date-fns library in JavaScript:

  1. Install the date-fns package using npm command:
npm install date-fns
21 chars
2 lines
  1. Import the eachDayOfInterval function in your JavaScript file:
import { eachDayOfInterval } from 'date-fns';
46 chars
2 lines
  1. Call the eachDayOfInterval function with two arguments, a start and end date for the interval:
const startDate = new Date('2021-01-01');
const endDate = new Date('2021-01-07');
const interval = { start: startDate, end: endDate };
const dates = eachDayOfInterval(interval);
console.log(dates);
198 chars
6 lines

In this example, the eachDayOfInterval function will return an array of Date objects for each day in the week of January 1st to 7th, 2021:

[
  Fri Jan 01 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Sat Jan 02 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Sun Jan 03 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Mon Jan 04 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Tue Jan 05 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Wed Jan 06 2021 00:00:00 GMT-0800 (Pacific Standard Time),
  Thu Jan 07 2021 00:00:00 GMT-0800 (Pacific Standard Time)
]
430 chars
10 lines

gistlibby LogSnag