UNPKG

mg-js

Version:

A set of useful functions, components and hooks.

89 lines (88 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var timeAgo = function (time, language) { time = Number(time); switch (typeof time) { case "number": break; case "string": time = +new Date(time); break; default: time = +new Date(); } var time_formats; if (language == "En") { time_formats = [ [60, "seconds", 1], [120, "One minute ago", "In a minute"], [3600, "minutes", 60], [7200, "one hour ago", "In a hour"], [86400, "hours", 3600], [172800, "Yesterday", "Tomorrow"], [604800, "days", 86400], [1209600, "Week ago", "Next week"], [2419200, "weeks", 604800], [4838400, "Month ago", "Next month"], [29030400, "two month", 2419200], [58060800, "Year ago", "Next year"], [2903040000, "years", 29030400], [5806080000, "The last century", "Next century"], [58060800000, "century", 2903040000], ]; } if (language == "Heb") { time_formats = [ [60, "שניות", 1], [120, "לפני דקה 1", "בעוד דקה"], [3600, "דקות", 60], [7200, "לפני שעה אחת", "בעוד שעה"], [86400, "שעות", 3600], [172800, "אתמול", "מחר"], [604800, "ימים", 86400], [1209600, "לפני שבוע", "שבוע הבא"], [2419200, "שבועות", 604800], [4838400, "לפני חודש", "חודש הבא"], [29030400, "חודשים", 2419200], [58060800, "לפני שנה", "שנה הבאה"], [2903040000, "שנים", 29030400], [5806080000, "המאה הקודמת", "במאה הבאה"], [58060800000, "מאות", 2903040000], ]; } var seconds = (+new Date() - time) / 1000; var token; if (language == "En") { token = "Before"; } if (language == "Heb") { token = "לפני"; } var 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; } var 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; }; exports.default = timeAgo;