2024-02-13 23:01:07 +00:00
|
|
|
// getMonth() method ranges from 0-11 so no reason to account for it
|
2022-04-24 04:27:51 +00:00
|
|
|
const months = [
|
|
|
|
'January',
|
|
|
|
'February',
|
|
|
|
'March',
|
|
|
|
'April',
|
|
|
|
'May',
|
|
|
|
'June',
|
|
|
|
'July',
|
|
|
|
'August',
|
|
|
|
'September',
|
|
|
|
'October',
|
|
|
|
'November',
|
|
|
|
'December'
|
|
|
|
];
|
|
|
|
|
2024-10-10 05:58:33 +00:00
|
|
|
function get12HourTime(pdate: Date | string): string {
|
|
|
|
const date = (typeof pdate === 'string') ? new Date(pdate) : pdate;
|
|
|
|
if (date.getHours() > 12)
|
|
|
|
return `${date.getHours() - 12}:${date.getMinutes()} PM`;
|
|
|
|
else if (date.getHours() === 0)
|
|
|
|
return `12:${date.getMinutes()} AM`;
|
|
|
|
return `${date.getHours()}:${date.getMinutes()} AM`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
function toHumanReadableDate(date: Date | string, disable?: { year?: boolean, month?: boolean, day?: boolean }) {
|
|
|
|
const oDate = (typeof date === 'string') ? new Date(date) : date;
|
2022-10-05 03:41:59 +00:00
|
|
|
|
|
|
|
const year = oDate.getFullYear();
|
|
|
|
const month = months[oDate.getMonth()];
|
|
|
|
const day = oDate.getDate();
|
2024-02-13 23:01:07 +00:00
|
|
|
|
|
|
|
let out = !disable?.day ? `${day}${getOrdinalDaySuffix(day)}` : '';
|
2022-10-05 03:41:59 +00:00
|
|
|
out = !disable?.month ? `${out} ${month}` : out;
|
|
|
|
out = !disable?.year ? `${out} ${year}` : out;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
|
2024-10-10 05:58:33 +00:00
|
|
|
export function getOrdinalDaySuffix(day: number): string {
|
2024-02-13 23:01:07 +00:00
|
|
|
switch (day) {
|
|
|
|
case 1:
|
|
|
|
case 21:
|
|
|
|
case 31:
|
|
|
|
return 'st';
|
|
|
|
case 2:
|
|
|
|
case 22:
|
|
|
|
return 'nd';
|
|
|
|
case 3:
|
|
|
|
case 23:
|
|
|
|
return 'rd';
|
|
|
|
default:
|
|
|
|
return 'th';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 05:58:33 +00:00
|
|
|
export function toLocaleString(pdate: Date | string): string {
|
|
|
|
const date = (typeof pDate === 'string') ? new Date(pdate) : pdate;
|
|
|
|
return `${toHumanReadableDate(date)}, ${get12HourTime(date)}`;
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
export function toRelativeDate(date: Date | string): string {
|
|
|
|
const oDate = (typeof date === 'string') ? new Date(date) : date;
|
|
|
|
|
|
|
|
|
|
|
|
if (!isValid(oDate)) {
|
|
|
|
return 'Invalid Date';
|
|
|
|
}
|
2022-04-24 04:27:51 +00:00
|
|
|
|
|
|
|
const now = new Date();
|
2022-04-28 01:55:18 +00:00
|
|
|
const diff = now.getTime() - oDate.getTime();
|
2022-04-24 04:27:51 +00:00
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
let tdiff = Math.floor(diff / 1000);
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
if (tdiff < 0) {
|
|
|
|
return toHumanReadableDate(oDate);
|
|
|
|
}
|
|
|
|
|
2022-04-24 04:27:51 +00:00
|
|
|
if (tdiff < 60) {
|
|
|
|
return `${tdiff} seconds ago`;
|
|
|
|
}
|
2024-02-13 23:01:07 +00:00
|
|
|
|
|
|
|
tdiff = Math.floor(tdiff / 60);
|
2022-04-24 04:27:51 +00:00
|
|
|
if (tdiff < 60) {
|
2024-02-13 23:01:07 +00:00
|
|
|
return `${tdiff} minute${tdiff === 1 ? '' : 's'} ago`;
|
2022-04-24 04:27:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
tdiff = Math.floor(tdiff / 60);
|
2022-04-24 04:27:51 +00:00
|
|
|
if (tdiff < 24) {
|
2024-02-13 23:01:07 +00:00
|
|
|
return `${tdiff} hour${tdiff === 1 ? '' : 's'} ago`;
|
2022-04-24 04:27:51 +00:00
|
|
|
}
|
|
|
|
if (tdiff < 48) {
|
|
|
|
return `Yesterday`;
|
|
|
|
}
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
if (oDate.getFullYear() != now.getFullYear())
|
|
|
|
return toHumanReadableDate(oDate);
|
2024-02-13 23:01:07 +00:00
|
|
|
return toHumanReadableDate(oDate, { year: true });
|
2022-04-28 01:55:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:01:07 +00:00
|
|
|
export function getFullMonth(month: number) {
|
|
|
|
if (month >= 1 && month <= 12)
|
|
|
|
return months[month];
|
|
|
|
return 'Invalid Month';
|
2022-04-28 01:55:18 +00:00
|
|
|
}
|
2024-02-13 23:01:07 +00:00
|
|
|
|
|
|
|
export function isValid(date: any) {
|
|
|
|
return (new Date(date)).toString() !== 'Invalid Date';
|
|
|
|
}
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
const DateTool = {
|
2024-02-13 23:01:07 +00:00
|
|
|
toRelativeDate,
|
|
|
|
getFullMonth,
|
|
|
|
isValid,
|
|
|
|
getOrdinalDaySuffix,
|
2024-10-10 05:58:33 +00:00
|
|
|
toLocaleString,
|
2023-10-30 04:18:38 +00:00
|
|
|
};
|
2022-04-28 01:55:18 +00:00
|
|
|
|
2023-10-30 04:18:38 +00:00
|
|
|
export default DateTool;
|