date-fns / date-fns

⏳ Modern JavaScript date utility library ⌛️
https://date-fns.org
MIT License
34.11k stars 1.76k forks source link

Feature Request: startOfCalendarSheetMonth, endOfCalendarSheetMonth #3801

Open MoxxiManagarm opened 1 month ago

MoxxiManagarm commented 1 month ago

The request is to add 2 functions to calculate the first and the last date of a calendar sheet. Calendar sheets in common tools like google calendar, windows calendar etc. fill the rows to complete the week and additionally fill to 6 rows at the end.

With May 2024 it would result in 29th April and 9th June respectively. image

Name suggestion for the functions: startOfCalendarSheetMonth, endOfCalendarSheetMonth

To achieve the same without the suggested functions, 6 other functions of date-fns are required:

The suggested functions would make that much cleaner.

fturmel commented 1 month ago

I like it. Some thoughts/caveats:

For reference :

// process.env.TZ = 'Europe/Berlin';

import { addDays, endOfMonth, endOfWeek, startOfMonth, startOfWeek } from 'date-fns';
import { de } from 'date-fns/locale'; // use a locale or manually set the `weekStartsOn` option on the weeks functions

const today = new Date(2024, 4, 16);

const startOfCalendarSheet = startOfWeek(startOfMonth(today), { locale: de });
const endOfCalendarSheetPadLastWeek = endOfWeek(endOfMonth(today), { locale: de });
const endOfCalendarSheetFixedSixWeeks = addDays(startOfCalendarSheet, 6 * 7 - 1);

console.log(startOfCalendarSheet.toString());            // Mon Apr 29 2024 00:00:00 GMT+0200 (Central European Summer Time)
console.log(endOfCalendarSheetPadLastWeek.toString());   // Sun Jun 02 2024 23:59:59 GMT+0200 (Central European Summer Time)
console.log(endOfCalendarSheetFixedSixWeeks.toString()); // Sun Jun 09 2024 00:00:00 GMT+0200 (Central European Summer Time)