11import dayjs from 'dayjs' ;
2+ import utc from 'dayjs/plugin/utc.js' ;
23import { getCurrentLocale } from '../utils.js' ;
34
4- // Returns an array of millisecond-timestamps of start-of-week days (Sundays)
5+ dayjs . extend ( utc ) ;
6+
7+ /**
8+ * Returns an array of millisecond-timestamps of start-of-week days (Sundays)
9+ *
10+ * @param startConfig The start date. Can take any type that `Date` accepts.
11+ * @param endConfig The end date. Can take any type that `Date` accepts.
12+ */
513export function startDaysBetween ( startDate , endDate ) {
14+ const start = dayjs . utc ( startDate ) ;
15+ const end = dayjs . utc ( endDate ) ;
16+
17+ let current = start ;
18+
619 // Ensure the start date is a Sunday
7- while ( startDate . getDay ( ) !== 0 ) {
8- startDate . setDate ( startDate . getDate ( ) + 1 ) ;
20+ while ( current . day ( ) !== 0 ) {
21+ current = current . add ( 1 , 'day' ) ;
922 }
1023
11- const start = dayjs ( startDate ) ;
12- const end = dayjs ( endDate ) ;
1324 const startDays = [ ] ;
14-
15- let current = start ;
1625 while ( current . isBefore ( end ) ) {
1726 startDays . push ( current . valueOf ( ) ) ;
18- // we are adding 7 * 24 hours instead of 1 week because we don't want
19- // date library to use local time zone to calculate 1 week from now.
20- // local time zone is problematic because of daylight saving time (dst)
21- // used on some countries
22- current = current . add ( 7 * 24 , 'hour' ) ;
27+ current = current . add ( 1 , 'week' ) ;
2328 }
2429
2530 return startDays ;
@@ -29,10 +34,10 @@ export function firstStartDateAfterDate(inputDate) {
2934 if ( ! ( inputDate instanceof Date ) ) {
3035 throw new Error ( 'Invalid date' ) ;
3136 }
32- const dayOfWeek = inputDate . getDay ( ) ;
37+ const dayOfWeek = inputDate . getUTCDay ( ) ;
3338 const daysUntilSunday = 7 - dayOfWeek ;
3439 const resultDate = new Date ( inputDate . getTime ( ) ) ;
35- resultDate . setDate ( resultDate . getDate ( ) + daysUntilSunday ) ;
40+ resultDate . setUTCDate ( resultDate . getUTCDate ( ) + daysUntilSunday ) ;
3641 return resultDate . valueOf ( ) ;
3742}
3843
0 commit comments