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.
125 lines
3.8 KiB
125 lines
3.8 KiB
const pad = (n:number) => n < 10 ? '0'+n : n.toString();
|
|
const weekDay = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
|
|
const monthNames = ["January","February","March","April","June","july","August","September","October","November","December"]
|
|
|
|
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 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 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())}`;
|
|
}
|
|
|
|
export function startOfMonth(now: Date): Date {
|
|
return new Date(now.getFullYear(), now.getMonth(), 1);
|
|
}
|
|
|
|
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;
|
|
}
|