2023-04-07 15:56:02 +03:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import utc from 'dayjs/plugin/utc';
|
|
|
|
|
|
|
|
dayjs.extend(utc)
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
// https://stackoverflow.com/a/50130338
|
|
|
|
export function formatISO(date) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.format('YYYY-MM-DD')
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftMonths(date, months) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.add(months, 'months')
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftDays(date, days) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.add(days, 'days')
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatMonthYYYY(date) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.format('MMMM YYYY')
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 13:52:26 +03:00
|
|
|
export function formatYear(date) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return `Year of ${date.year()}`;
|
2022-03-31 13:52:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 15:31:44 +03:00
|
|
|
export function formatYearShort(date) {
|
|
|
|
return date.getUTCFullYear().toString().substring(2)
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export function formatDay(date) {
|
2023-04-07 15:56:02 +03:00
|
|
|
if (date.year() !== dayjs().year()) {
|
|
|
|
return date.format('ddd, DD MMM YYYY')
|
2022-03-31 10:20:13 +03:00
|
|
|
} else {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.format('ddd, DD MMM')
|
2022-03-31 10:20:13 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 15:31:44 +03:00
|
|
|
export function formatDayShort(date, includeYear = false) {
|
2023-04-07 15:56:02 +03:00
|
|
|
if (includeYear) {
|
|
|
|
return date.format('D MMM YY')
|
2022-06-06 10:41:27 +03:00
|
|
|
} else {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.format('D MMM')
|
2022-06-06 10:41:27 +03:00
|
|
|
}
|
2023-04-07 15:56:02 +03:00
|
|
|
}
|
2022-06-06 10:41:27 +03:00
|
|
|
|
2023-04-11 15:59:10 +03:00
|
|
|
export function formatDateRange(site, from, to) {
|
|
|
|
if (!from || !to) return
|
|
|
|
if (typeof from === 'string') from = parseUTCDate(from)
|
|
|
|
if (typeof to === 'string') to = parseUTCDate(to)
|
|
|
|
|
|
|
|
if (from.isSame(to)) {
|
|
|
|
return formatDay(from)
|
|
|
|
} else if (from.isSame(to, 'year')) {
|
|
|
|
const includeYear = !isThisYear(site, from)
|
|
|
|
return `${formatDayShort(from, false)} - ${formatDayShort(to, includeYear)}`
|
|
|
|
} else {
|
|
|
|
return `${formatDayShort(from, true)} - ${formatDayShort(to, true)}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:56:02 +03:00
|
|
|
export function parseUTCDate(dateString) {
|
|
|
|
return dayjs.utc(dateString)
|
2019-12-18 06:15:31 +03:00
|
|
|
}
|
|
|
|
|
2023-04-25 14:26:24 +03:00
|
|
|
export function parseNaiveDate(dateString) {
|
|
|
|
return dayjs(dateString)
|
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
export function nowForSite(site) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return dayjs.utc().utcOffset(site.offset / 60)
|
2020-09-09 11:13:55 +03:00
|
|
|
}
|
|
|
|
|
2024-05-02 14:57:10 +03:00
|
|
|
export function yesterday(site) {
|
2024-08-21 15:49:43 +03:00
|
|
|
return shiftDays(nowForSite(site), -1)
|
2024-05-02 14:57:10 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
export function lastMonth(site) {
|
|
|
|
return shiftMonths(nowForSite(site), -1)
|
|
|
|
}
|
|
|
|
|
2024-05-02 14:57:10 +03:00
|
|
|
export function isSameDate(date1, date2) {
|
|
|
|
return formatISO(date1) === formatISO(date2)
|
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
export function isSameMonth(date1, date2) {
|
|
|
|
return formatMonthYYYY(date1) === formatMonthYYYY(date2)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isToday(site, date) {
|
2024-05-02 14:57:10 +03:00
|
|
|
return isSameDate(date, nowForSite(site))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2020-12-23 11:42:22 +03:00
|
|
|
|
|
|
|
export function isThisMonth(site, date) {
|
|
|
|
return formatMonthYYYY(date) === formatMonthYYYY(nowForSite(site))
|
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
|
2022-03-31 13:52:26 +03:00
|
|
|
export function isThisYear(site, date) {
|
2023-04-07 15:56:02 +03:00
|
|
|
return date.year() === nowForSite(site).year()
|
2022-03-31 13:52:26 +03:00
|
|
|
}
|
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
export function isBefore(date1, date2, period) {
|
|
|
|
/* assumes 'day' and 'month' are the only valid periods */
|
2023-04-07 15:56:02 +03:00
|
|
|
if (date1.year() !== date2.year()) {
|
|
|
|
return date1.year() < date2.year();
|
2021-01-07 16:24:59 +03:00
|
|
|
}
|
2022-03-31 13:56:56 +03:00
|
|
|
if (period === "year") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-07 15:56:02 +03:00
|
|
|
if (date1.month() !== date2.month()) {
|
|
|
|
return date1.month() < date2.month();
|
2021-01-07 16:24:59 +03:00
|
|
|
}
|
|
|
|
if (period === "month") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-07 15:56:02 +03:00
|
|
|
return date1.date() < date2.date()
|
2021-01-07 16:24:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isAfter(date1, date2, period) {
|
|
|
|
/* assumes 'day' and 'month' are the only valid periods */
|
2023-04-07 15:56:02 +03:00
|
|
|
if (date1.year() !== date2.year()) {
|
|
|
|
return date1.year() > date2.year();
|
2021-01-07 16:24:59 +03:00
|
|
|
}
|
2022-03-31 13:56:56 +03:00
|
|
|
if (period === "year") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-07 15:56:02 +03:00
|
|
|
if (date1.month() !== date2.month()) {
|
|
|
|
return date1.month() > date2.month();
|
2021-01-07 16:24:59 +03:00
|
|
|
}
|
|
|
|
if (period === "month") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-07 15:56:02 +03:00
|
|
|
return date1.date() > date2.date()
|
2024-05-02 14:57:10 +03:00
|
|
|
}
|