73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
import dayjs from 'dayjs';
|
|
import relativeTime from 'dayjs/esm/plugin/relativeTime';
|
|
import localizedFormat from 'dayjs/plugin/localizedFormat';
|
|
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
import isToday from 'dayjs/plugin/isToday';
|
|
import utc from 'dayjs/plugin/utc';
|
|
import timezone from 'dayjs/plugin/timezone';
|
|
|
|
dayjs.extend(updateLocale);
|
|
dayjs.extend(relativeTime);
|
|
dayjs.extend(localizedFormat);
|
|
dayjs.extend(isToday);
|
|
dayjs.extend(utc);
|
|
dayjs.extend(timezone);
|
|
|
|
dayjs.shortFormating = (s, ago = false) => {
|
|
if (s === 'now' || s === 'now ago') {
|
|
return 'now';
|
|
}
|
|
|
|
const prefix = s.split(' ')[0];
|
|
const posfix = s.split(' ')[1];
|
|
const isPast = s.includes('ago');
|
|
let newPostfix = '';
|
|
switch (posfix) {
|
|
case 'minute':
|
|
newPostfix = 'm';
|
|
break;
|
|
case 'minutes':
|
|
newPostfix = 'm';
|
|
break;
|
|
case 'hour':
|
|
newPostfix = 'h';
|
|
break;
|
|
case 'hours':
|
|
newPostfix = 'h';
|
|
break;
|
|
case 'day':
|
|
newPostfix = 'd';
|
|
break;
|
|
case 'days':
|
|
newPostfix = 'd';
|
|
break;
|
|
case 'month':
|
|
newPostfix = 'M';
|
|
break;
|
|
case 'months':
|
|
newPostfix = 'M';
|
|
break;
|
|
case 'year':
|
|
newPostfix = 'Y';
|
|
break;
|
|
case 'years':
|
|
newPostfix = 'Y';
|
|
break;
|
|
}
|
|
return `${['a', 'an'].includes(prefix) ? '1' : prefix} ${newPostfix}${
|
|
isPast ? (ago ? ' ago' : '') : ''
|
|
}`;
|
|
};
|
|
|
|
export function dayjsLocal(dateTimeString) {
|
|
let localTimezone = dayjs.tz.guess();
|
|
// dates are stored in Asia/Calcutta timezone on the server
|
|
return dayjs.tz(dateTimeString, 'Asia/Calcutta').tz(localTimezone);
|
|
}
|
|
|
|
export function dayjsIST(dateTimeString) {
|
|
return dayjs(dateTimeString).tz('Asia/Calcutta');
|
|
}
|
|
|
|
export default dayjs;
|