temporal-extra
Version:
Locale-aware date utilities for Temporal: week numbers, date adjusters, polyfill support and more
139 lines (138 loc) • 4.04 kB
JavaScript
import "./week-info/polyfill.js";
/**
* Returns the previous business day dependent on locale.
*/
export const previousBusinessDay = (date, locale) => {
const { weekend } = (typeof locale === "string" ? new Intl.Locale(locale) : locale).getWeekInfo();
let diff = 1;
while (weekend.includes(((date.dayOfWeek - diff - 1 + 7) % 7) + 1)) {
diff++;
}
return date.subtract({ days: diff });
};
/**
* Returns the next business day dependent on locale.
*/
export const nextBusinessDay = (date, locale) => {
const { weekend } = (typeof locale === "string" ? new Intl.Locale(locale) : locale).getWeekInfo();
let diff = 1;
while (weekend.includes(((date.dayOfWeek + diff - 1 + 7) % 7) + 1)) {
diff++;
}
return date.add({ days: diff });
};
/**
* Returns the most recent day matching a given day of week.
*/
export const previousDayOfWeek = (date, dayOfWeek) => {
const diff = dayOfWeek - date.dayOfWeek;
return date.subtract({ days: diff >= 0 ? 7 - diff : -diff });
};
/**
* Returns the most recent or same day matching a given day of week.
*/
export const previousOrSameDayOfWeek = (date, dayOfWeek) => {
if (date.dayOfWeek === dayOfWeek) {
return date;
}
const diff = dayOfWeek - date.dayOfWeek;
return date.subtract({ days: diff >= 0 ? 7 - diff : -diff });
};
/**
* Returns the closest next day matching a given day of week.
*/
export const nextDayOfWeek = (date, dayOfWeek) => {
const diff = date.dayOfWeek - dayOfWeek;
return date.add({ days: diff >= 0 ? 7 - diff : -diff });
};
/**
* Returns the closest next or same day matching a given day of week.
*/
export const nextOrSameDayOfWeek = (date, dayOfWeek) => {
if (date.dayOfWeek === dayOfWeek) {
return date;
}
const diff = date.dayOfWeek - dayOfWeek;
return date.add({ days: diff >= 0 ? 7 - diff : -diff });
};
/**
* Returns the first day of the week dependent on locale.
*/
export const firstDayOfWeek = (date, locale) => {
const { firstDay } = (typeof locale === "string" ? new Intl.Locale(locale) : locale).getWeekInfo();
const diff = date.dayOfWeek - firstDay;
return date.subtract({ days: diff >= 0 ? diff : 7 + diff });
};
/**
* Returns the last day of the week dependent on locale.
*/
export const lastDayOfWeek = (date, locale) => {
const { firstDay } = (typeof locale === "string" ? new Intl.Locale(locale) : locale).getWeekInfo();
const lastDay = ((firstDay + 5) % 7) + 1;
const diff = lastDay - date.dayOfWeek;
return date.add({ days: diff >= 0 ? diff : 7 + diff });
};
/**
* Returns the first day of the month.
*/
export const firstDayOfMonth = (date) => {
return date.with({ day: 1 });
};
/**
* Returns the last day of the month.
*/
export const lastDayOfMonth = (date) => {
return date.with({ day: date.daysInMonth });
};
/**
* Returns the first day of the year.
*/
export const firstDayOfYear = (date) => {
return date.with({ month: 1, day: 1 });
};
/**
* Returns the last day of the year.
*/
export const lastDayOfYear = (date) => {
return date.with({ month: 12, day: 31 });
};
/**
* Returns the first day of the next month.
*/
export const firstDayOfNextMonth = (date) => {
return date.month === 12
? date.with({ year: date.year + 1, month: 1, day: 1 })
: date.with({ month: date.month + 1, day: 1 });
};
/**
* Returns the first day of the next year.
*/
export const firstDayOfNextYear = (date) => {
return date.with({ year: date.year + 1, month: 1, day: 1 });
};
/**
* Returns the start of the day.
*/
export const startOfDay = (dateTime) => {
return dateTime.with({
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0,
});
};
/**
* Returns the end of the day.
*/
export const endOfDay = (dateTime) => {
return dateTime.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999,
});
};