change rounding for dates/times

This commit is contained in:
Johann150 2023-09-10 01:07:14 +02:00
parent 2c69cb4a92
commit 0cb4529ed0
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -37,20 +37,28 @@ let now = $ref(new Date());
const relative = $computed(() => {
const ago = now.getTime() - _time.getTime();
function rounded(n: number): string {
if (n % 1 >= 0.5) {
return Math.floor(n).toString() + "½"
} else {
return Math.floor(n).toString();
}
}
if (ago >= YEAR) {
return i18n.t('_ago.yearsAgo', { n: Math.round(ago / YEAR).toString() });
return i18n.t('_ago.yearsAgo', { n: rounded(ago / YEAR) });
} else if (ago >= MONTH) {
return i18n.t('_ago.monthsAgo', { n: Math.round(ago / MONTH).toString() });
return i18n.t('_ago.monthsAgo', { n: rounded(ago / MONTH) });
} else if (ago >= WEEK) {
return i18n.t('_ago.weeksAgo', { n: Math.round(ago / WEEK).toString() });
return i18n.t('_ago.weeksAgo', { n: rounded(ago / WEEK) });
} else if (ago >= DAY) {
return i18n.t('_ago.daysAgo', { n: Math.round(ago / DAY).toString() });
return i18n.t('_ago.daysAgo', { n: rounded(ago / DAY) });
}
// if the format is 'date', the relative date precision is no more than days ago
if (props.format !== 'date') {
if (ago >= HOUR) {
return i18n.t('_ago.hoursAgo', { n: Math.round(ago / HOUR).toString() });
return i18n.t('_ago.hoursAgo', { n: rounded(ago / HOUR) });
} else if (ago >= MINUTE) {
return i18n.t('_ago.minutesAgo', { n: Math.floor(ago / MINUTE).toString() });
} else if (ago >= 10 * SECOND) {