mg-js
Version:
A set of useful functions, components and hooks.
87 lines (86 loc) • 2.93 kB
JavaScript
const timeAgo = (time, language) => {
time = Number(time);
switch (typeof time) {
case "number":
break;
case "string":
time = +new Date(time);
break;
default:
time = +new Date();
}
let time_formats;
if (language == "En") {
time_formats = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
}
if (language == "Heb") {
time_formats = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
}
let seconds = (+new Date() - time) / 1000;
let token;
if (language == "En") {
token = "Before";
}
if (language == "Heb") {
token = "לפני";
}
let list_choice = 1;
if (Math.floor(seconds) === 0) {
if (language == "En")
return "At this moment";
if (language == "Heb")
return "ברגע זה";
}
if (seconds < 0) {
seconds = Math.abs(seconds);
if (language == "En")
token = "In";
;
if (language == "Heb")
token = "ב";
list_choice = 2;
}
let i = 0, format;
while ((format = time_formats[i++]))
if (seconds < format[0]) {
if (typeof format[2] == "string")
return format[list_choice];
else {
return token + " " + Math.floor(seconds / format[2]) + " " + format[1];
}
}
return time;
};
export default timeAgo;