2019-11-19 07:30:42 +03:00
|
|
|
// https://stackoverflow.com/a/50130338
|
|
|
|
export function formatISO(date) {
|
|
|
|
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
|
|
|
|
.toISOString()
|
|
|
|
.split("T")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftMonths(date, months) {
|
|
|
|
const newDate = new Date(date.getTime())
|
2021-03-31 15:13:58 +03:00
|
|
|
const d = newDate.getDate();
|
|
|
|
newDate.setMonth(newDate.getMonth() + +months);
|
|
|
|
if (newDate.getDate() != d) {
|
|
|
|
newDate.setDate(0);
|
|
|
|
}
|
|
|
|
return newDate;
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftDays(date, days) {
|
|
|
|
const newDate = new Date(date.getTime())
|
|
|
|
newDate.setDate(newDate.getDate() + days)
|
|
|
|
return newDate
|
|
|
|
}
|
|
|
|
|
|
|
|
const MONTHS = [
|
|
|
|
"January", "February", "March",
|
|
|
|
"April", "May", "June", "July",
|
|
|
|
"August", "September", "October",
|
|
|
|
"November", "December"
|
|
|
|
]
|
|
|
|
|
2022-03-31 10:20:13 +03:00
|
|
|
const DAYS_ABBREV = [
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
]
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export function formatMonthYYYY(date) {
|
|
|
|
return `${MONTHS[date.getMonth()]} ${date.getFullYear()}`;
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:52:26 +03:00
|
|
|
export function formatYear(date) {
|
|
|
|
return `Year of ${date.getFullYear()}`;
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export function formatDay(date) {
|
2022-03-31 16:47:19 +03:00
|
|
|
var weekday = DAYS_ABBREV[date.getDay()];
|
2022-03-31 10:20:13 +03:00
|
|
|
if (date.getFullYear() !== (new Date()).getFullYear()) {
|
|
|
|
return `${weekday}, ${date.getDate()} ${formatMonthShort(date)} ${date.getFullYear()}`;
|
|
|
|
} else {
|
|
|
|
return `${weekday}, ${date.getDate()} ${formatMonthShort(date)}`;
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
export function formatDayShort(date) {
|
2022-03-31 10:20:13 +03:00
|
|
|
return `${date.getDate()} ${formatMonthShort(date)}`;
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-12-18 06:15:31 +03:00
|
|
|
export function parseUTCDate(dateString) {
|
|
|
|
var date = new Date(dateString);
|
|
|
|
return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
// https://stackoverflow.com/a/11124448
|
2020-09-09 11:13:55 +03:00
|
|
|
export function nowForSite(site) {
|
2019-11-21 11:59:06 +03:00
|
|
|
const browserOffset = (new Date()).getTimezoneOffset() * 60
|
2020-09-09 11:13:55 +03:00
|
|
|
return new Date(new Date().getTime() + (site.offset * 1000) + (browserOffset * 1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lastMonth(site) {
|
|
|
|
return shiftMonths(nowForSite(site), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSameMonth(date1, date2) {
|
|
|
|
return formatMonthYYYY(date1) === formatMonthYYYY(date2)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isToday(site, date) {
|
2020-09-09 11:13:55 +03:00
|
|
|
return formatISO(date) === formatISO(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) {
|
|
|
|
return date.getFullYear() === nowForSite(site).getFullYear()
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
export function isBefore(date1, date2, period) {
|
|
|
|
/* assumes 'day' and 'month' are the only valid periods */
|
|
|
|
if (date1.getFullYear() !== date2.getFullYear()) {
|
|
|
|
return date1.getFullYear() < date2.getFullYear();
|
|
|
|
}
|
2022-03-31 13:56:56 +03:00
|
|
|
if (period === "year") {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
if (date1.getMonth() !== date2.getMonth()) {
|
|
|
|
return date1.getMonth() < date2.getMonth();
|
|
|
|
}
|
|
|
|
if (period === "month") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return date1.getDate() < date2.getDate()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isAfter(date1, date2, period) {
|
|
|
|
/* assumes 'day' and 'month' are the only valid periods */
|
|
|
|
if (date1.getFullYear() !== date2.getFullYear()) {
|
|
|
|
return date1.getFullYear() > date2.getFullYear();
|
|
|
|
}
|
2022-03-31 13:56:56 +03:00
|
|
|
if (period === "year") {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
if (date1.getMonth() !== date2.getMonth()) {
|
|
|
|
return date1.getMonth() > date2.getMonth();
|
|
|
|
}
|
|
|
|
if (period === "month") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return date1.getDate() > date2.getDate()
|
|
|
|
}
|
2022-03-31 10:20:13 +03:00
|
|
|
|
|
|
|
function formatMonthShort(date) {
|
|
|
|
return `${MONTHS[date.getMonth()].substring(0, 3)}`;
|
|
|
|
}
|