教程 date-fns tutorial

eachYearOfInterval

返回指定时间间隔内的所有年份。

javascript
import { eachYearOfInterval } from "date-fns";

// 示例时间间隔
const startDate = new Date(2022, 0, 1); // 2022年1月1日
const endDate = new Date(2024, 0, 1); // 2024年1月1日

// 返回指定时间间隔内的所有年份
const years = eachYearOfInterval({ start: startDate, end: endDate });

console.log("指定时间间隔内的所有年份:", years);
// 指定时间间隔内的所有年份:[
//   Sat Jan 01 2022 00:00:00 GMT+0800 (China Standard Time),
//   Sun Jan 01 2023 00:00:00 GMT+0800 (China Standard Time),
//   Mon Jan 01 2024 00:00:00 GMT+0800 (China Standard Time)
// ]