You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.7 KiB
198 lines
5.7 KiB
const weekDay = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
|
|
const monthNames = ["January","February","March","April","May","June","july","August","September","October","November","December"]
|
|
|
|
export function formatFormTime(time: Date | string): string {
|
|
if (!time) {
|
|
return "";
|
|
}
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
if (Number.isNaN(time.getTime()) || time.getTime() < 86400000) {
|
|
return "";
|
|
}
|
|
|
|
return `${time.getFullYear()}-${pad(time.getMonth()+1)}-${pad(time.getDate())}T${pad(time.getHours())}:${pad(time.getMinutes())}`;
|
|
}
|
|
|
|
function pad(n:number) {
|
|
return n < 10 ? '0'+n : n.toString()
|
|
};
|
|
|
|
export function formatTime(time: Date | string): string {
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
|
|
return `${pad(time.getHours())}:${pad(time.getMinutes())}`;
|
|
}
|
|
|
|
export function formatDate(time: Date | string | number): string {
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
|
|
return `${time.getFullYear()}-${pad(time.getMonth()+1)}-${pad(time.getDate())}`;
|
|
}
|
|
|
|
export function formatDateTime(time: Date | string | number): string {
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
|
|
return `${formatDate(time)} ${formatTime(time)}`
|
|
}
|
|
|
|
export function formatWeekdayDate(time: Date | string | number): string {
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
|
|
return `${time.getFullYear()}-${pad(time.getMonth()+1)}-${pad(time.getDate())} (${weekDay[time.getDay()]})`;
|
|
}
|
|
|
|
export function formatWeekdayTitle(now: Date, time: Date | string | number): string {
|
|
if (!(time instanceof Date)) {
|
|
time = new Date(time);
|
|
}
|
|
|
|
const diff = Math.floor((time.getTime() - now.getTime()) / (86400000));
|
|
console.log(diff);
|
|
if (diff === 0) {
|
|
return "Today"
|
|
} else if (diff === -1) {
|
|
return "Yesterday"
|
|
} else if (diff >= -6) {
|
|
return `${weekDay[time.getDay()]}`;
|
|
} else {
|
|
return formatWeekdayDate(time);
|
|
}
|
|
}
|
|
|
|
export function startOfMonth(now: Date, offset = 0): Date {
|
|
return new Date(now.getFullYear(), now.getMonth() + offset);
|
|
}
|
|
|
|
export function startOfYear(now: Date): Date {
|
|
return new Date(now.getFullYear(), 0, 1);
|
|
}
|
|
|
|
export function endOfYear(now: Date): Date {
|
|
return new Date(new Date(now.getFullYear() + 1, 0, 1).getTime() - 60000);
|
|
}
|
|
|
|
export function endOfMonth(now: Date): Date {
|
|
return new Date(nextMonth(now).getTime() - 60000)
|
|
}
|
|
|
|
export function nextYear(now: Date, n?: number): Date {
|
|
return new Date(now.getFullYear() + (n || 1), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
|
|
}
|
|
|
|
export function lastYear(now: Date): Date {
|
|
return new Date(now.getFullYear() - 1, now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
|
|
}
|
|
|
|
export function nextMonth(now: Date): Date {
|
|
let result: Date;
|
|
if (now.getMonth() == 11) {
|
|
result = new Date(now.getFullYear() + 1, 0, 1);
|
|
} else {
|
|
result = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
|
}
|
|
|
|
return new Date(result.getTime());
|
|
}
|
|
|
|
export function lastMonth(now: Date): Date {
|
|
let result: Date;
|
|
if (now.getMonth() == 0) {
|
|
result = new Date(now.getFullYear() - 1, 11, 1);
|
|
} else {
|
|
result = new Date(now.getFullYear(), now.getMonth() - 1, 1);
|
|
}
|
|
|
|
return new Date(result.getTime());
|
|
}
|
|
|
|
export function startOfWeek(now: Date): Date {
|
|
return new Date(now.getTime() - ((now.getTimezoneOffset() * -60000) + ((now.getTime() + (86400000 * 3)) % (86400000 * 7))));
|
|
}
|
|
|
|
export function endOfWeek(now: Date): Date {
|
|
return new Date(startOfWeek(now).getTime() + (86400000 * 6) + (3600000 * 23) + (60000 * 59))
|
|
}
|
|
|
|
export function startOfDay(now: Date): Date {
|
|
return new Date(Math.floor(now.getTime() / 86400000) * 86400000 - (now.getTimezoneOffset() * -60000));
|
|
}
|
|
|
|
export function endOfDay(now: Date): Date {
|
|
return new Date(startOfDay(now).getTime() + (3600000 * 23) + (60000 * 59))
|
|
}
|
|
|
|
export function monthName(date: Date): string {
|
|
return monthNames[date.getMonth()]
|
|
}
|
|
|
|
export function addDays(date: Date, days: number): Date {
|
|
const newDate = new Date(date.getTime());
|
|
newDate.setDate(newDate.getDate() + days);
|
|
return newDate;
|
|
}
|
|
|
|
export function addMonths(date: Date, months: number): Date {
|
|
const newDate = new Date(date.getTime());
|
|
newDate.setMonth(newDate.getMonth() + months);
|
|
return newDate;
|
|
}
|
|
|
|
export function addYears(date: Date, years: number): Date {
|
|
const newDate = new Date(date.getTime());
|
|
newDate.setFullYear(newDate.getFullYear() + years);
|
|
return newDate;
|
|
}
|
|
|
|
export function formatDays(date: Date, now: Date) {
|
|
const d = startOfDay(date);
|
|
const n = startOfDay(now);
|
|
const diff = Math.round((d.getTime() - n.getTime()) / (86400000));
|
|
|
|
if (diff === 0) {
|
|
return "today";
|
|
} else if (diff < 7) {
|
|
return `${diff}d`;
|
|
} else {
|
|
return `${Math.floor(diff / 7)}w${Math.floor(diff % 7)}d`;
|
|
}
|
|
}
|
|
|
|
export function formatPrettyTime(date: Date, now: Date) {
|
|
const d = startOfDay(date);
|
|
const n = startOfDay(now);
|
|
|
|
const diff = Math.round((d.getTime() - n.getTime()) / (86400000));
|
|
const hhmm = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
|
|
|
const dayOfMonth = date.getDate();
|
|
var ordinalDay = dayOfMonth + "th";
|
|
if (dayOfMonth == 1) {
|
|
ordinalDay = dayOfMonth + "st";
|
|
} else if (dayOfMonth == 2) {
|
|
ordinalDay = dayOfMonth + "nd"
|
|
} else if (dayOfMonth == 3) {
|
|
ordinalDay = dayOfMonth + "rd"
|
|
}
|
|
|
|
if (diff === 0) {
|
|
return `today at ${hhmm}`;
|
|
} else if (diff === 1) {
|
|
return `tomorrow at ${hhmm}`;
|
|
} else if (diff === -1) {
|
|
return `yesterday at ${hhmm}`;
|
|
} else if (diff > -7 && diff < 7) {
|
|
return `${weekDay[date.getDay()]}, the ${ordinalDay} at ${hhmm}`;
|
|
} else {
|
|
return `${ordinalDay} of ${monthName(date)} at ${hhmm}`;
|
|
}
|
|
}
|