fixed desktop version
This commit is contained in:
27
src/utils/relativeTime.js
Normal file
27
src/utils/relativeTime.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const UNITS = [
|
||||
["year", 365 * 24 * 3600],
|
||||
["month", 30 * 24 * 3600],
|
||||
["week", 7 * 24 * 3600],
|
||||
["day", 24 * 3600],
|
||||
["hour", 3600],
|
||||
["minute", 60],
|
||||
];
|
||||
|
||||
/**
|
||||
* «5 минут назад», «вчера», «2 недели назад» — по языку интерфейса.
|
||||
* Возвращает null для пустой/некорректной даты.
|
||||
*/
|
||||
export function formatRelativeTime(value, language = "ru", now = Date.now()) {
|
||||
if (!value) return null;
|
||||
const time = new Date(value).getTime();
|
||||
if (!Number.isFinite(time)) return null;
|
||||
|
||||
const diffSec = Math.round((time - now) / 1000);
|
||||
const abs = Math.abs(diffSec);
|
||||
const rtf = new Intl.RelativeTimeFormat(language, { numeric: "auto" });
|
||||
|
||||
for (const [unit, sec] of UNITS) {
|
||||
if (abs >= sec) return rtf.format(Math.round(diffSec / sec), unit);
|
||||
}
|
||||
return rtf.format(0, "minute");
|
||||
}
|
||||
Reference in New Issue
Block a user