merge in new changes
This commit is contained in:
49
src/support/time.ts
Normal file
49
src/support/time.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { format, register } from 'timeago.js';
|
||||
|
||||
const TimeAgoConfiguration: string[][] = [
|
||||
['today', 'today'],
|
||||
['%s seconds ago', 'in %s seconds'],
|
||||
['1 minute ago', 'in 1 minute'],
|
||||
['%s minutes ago', 'in %s minutes'],
|
||||
['1 hour ago', 'in 1 hour'],
|
||||
['%s hours ago', 'in %s hours'],
|
||||
['1 day ago', 'in 1 day'],
|
||||
['%s days ago', 'in %s days'],
|
||||
['1 week ago', 'in 1 week'],
|
||||
['%s weeks ago', 'in %s weeks'],
|
||||
['1 month ago', 'in 1 month'],
|
||||
['%s months ago', 'in %s months'],
|
||||
['1 year ago', 'in 1 year'],
|
||||
['%s years ago', 'in %s years'],
|
||||
];
|
||||
|
||||
function formatDate(date: Date): string {
|
||||
const year = new Date(date).getFullYear();
|
||||
const month = String(new Date(date).getMonth() + 1).padStart(2, '0');
|
||||
const day = String(new Date(date).getDate()).padStart(2, '0');
|
||||
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
function formatDateTime(date: Date): string {
|
||||
const hours = String(new Date(date).getHours()).padStart(2, '0');
|
||||
const minutes = String(new Date(date).getMinutes()).padStart(2, '0');
|
||||
|
||||
return `${formatDate(date)} ${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
function timeago(date?: Date): string {
|
||||
if (!date) {
|
||||
return 'today';
|
||||
}
|
||||
|
||||
const localeFunc = (number: number, index: number, _?: number): [string, string] => {
|
||||
return TimeAgoConfiguration[index] as [string, string];
|
||||
};
|
||||
|
||||
register('timeago', localeFunc);
|
||||
|
||||
return format(date, 'timeago');
|
||||
}
|
||||
|
||||
export { formatDate, timeago, formatDateTime };
|
||||
Reference in New Issue
Block a user