Last active
May 10, 2024 13:43
-
-
Save felixhaeberle/fc08f7ca9ced2ed1e9f497e3967c2625 to your computer and use it in GitHub Desktop.
TypeScript: Get time distance as human readable string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// takes either a unix time number, string or a Date object and returns time string | |
export const timeAgo = (time: number | string | Date) => { | |
switch (typeof time) { | |
case "number": | |
break; | |
case "string": | |
time = +new Date(time); | |
break; | |
case "object": | |
if (time.constructor === Date) time = time.getTime(); | |
break; | |
default: | |
time = +new Date(); | |
} | |
const time_formats = [ | |
[60, "seconds", 1], // 60 | |
[120, "1 minute ago", "1 minute from now"], // 60*2 | |
[3600, "minutes", 60], // 60*60, 60 | |
[7200, "1 hour ago", "1 hour from now"], // 60*60*2 | |
[86400, "hours", 3600], // 60*60*24, 60*60 | |
[172800, "yesterday", "tomorrow"], // 60*60*24*2 | |
[604800, "days", 86400], // 60*60*24*7, 60*60*24 | |
[1209600, "last week", "next week"], // 60*60*24*7*4*2 | |
[2419200, "weeks", 604800], // 60*60*24*7*4, 60*60*24*7 | |
[4838400, "last month", "next month"], // 60*60*24*7*4*2 | |
[29030400, "months", 2419200], // 60*60*24*7*4*12, 60*60*24*7*4 | |
[58060800, "last year", "next year"], // 60*60*24*7*4*12*2 | |
[2903040000, "years", 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12 | |
[5806080000, "last century", "next century"], // 60*60*24*7*4*12*100*2 | |
[58060800000, "centuries", 2903040000], // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100 | |
]; | |
/* @ts-ignore */ | |
let seconds = (+new Date() - time) / 1000, | |
token = "ago", | |
list_choice = 1; | |
if (seconds == 0) { | |
return "Just now"; | |
} | |
if (seconds < 0) { | |
seconds = Math.abs(seconds); | |
token = "from now"; | |
list_choice = 2; | |
} | |
let i = 0, | |
format; | |
/* eslint-disable no-cond-assign */ | |
while ((format = time_formats[i++])) | |
if (seconds < format[0]) { | |
if (typeof format[2] == "string") return format[list_choice]; | |
else | |
return Math.floor(seconds / format[2]) + " " + format[1] + " " + token; | |
} | |
return time; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used on zirkular.dev. Zirkular connects contributors and maintainers in a community-first workspace with goal-oriented communication, efficient workflows and smart automations.