|
|
@@ -1,25 +1,30 @@ |
|
|
|
import dayjs from 'dayjs'; |
|
|
|
import utc from 'dayjs/plugin/utc.js'; |
|
|
|
import {getCurrentLocale} from '../utils.js'; |
|
|
|
|
|
|
|
// Returns an array of millisecond-timestamps of start-of-week days (Sundays) |
|
|
|
dayjs.extend(utc); |
|
|
|
|
|
|
|
/** |
|
|
|
* Returns an array of millisecond-timestamps of start-of-week days (Sundays) |
|
|
|
* |
|
|
|
* @param startConfig The start date. Can take any type that `Date` accepts. |
|
|
|
* @param endConfig The end date. Can take any type that `Date` accepts. |
|
|
|
*/ |
|
|
|
export function startDaysBetween(startDate, endDate) { |
|
|
|
const start = dayjs.utc(startDate); |
|
|
|
const end = dayjs.utc(endDate); |
|
|
|
|
|
|
|
let current = start; |
|
|
|
|
|
|
|
// Ensure the start date is a Sunday |
|
|
|
while (startDate.getDay() !== 0) { |
|
|
|
startDate.setDate(startDate.getDate() + 1); |
|
|
|
while (current.day() !== 0) { |
|
|
|
current = current.add(1, 'day'); |
|
|
|
} |
|
|
|
|
|
|
|
const start = dayjs(startDate); |
|
|
|
const end = dayjs(endDate); |
|
|
|
const startDays = []; |
|
|
|
|
|
|
|
let current = start; |
|
|
|
while (current.isBefore(end)) { |
|
|
|
startDays.push(current.valueOf()); |
|
|
|
// we are adding 7 * 24 hours instead of 1 week because we don't want |
|
|
|
// date library to use local time zone to calculate 1 week from now. |
|
|
|
// local time zone is problematic because of daylight saving time (dst) |
|
|
|
// used on some countries |
|
|
|
current = current.add(7 * 24, 'hour'); |
|
|
|
current = current.add(1, 'week'); |
|
|
|
} |
|
|
|
|
|
|
|
return startDays; |
|
|
@@ -29,10 +34,10 @@ export function firstStartDateAfterDate(inputDate) { |
|
|
|
if (!(inputDate instanceof Date)) { |
|
|
|
throw new Error('Invalid date'); |
|
|
|
} |
|
|
|
const dayOfWeek = inputDate.getDay(); |
|
|
|
const dayOfWeek = inputDate.getUTCDay(); |
|
|
|
const daysUntilSunday = 7 - dayOfWeek; |
|
|
|
const resultDate = new Date(inputDate.getTime()); |
|
|
|
resultDate.setDate(resultDate.getDate() + daysUntilSunday); |
|
|
|
resultDate.setUTCDate(resultDate.getUTCDate() + daysUntilSunday); |
|
|
|
return resultDate.valueOf(); |
|
|
|
} |
|
|
|
|