-
-
Save rodyhaddad/5896883 to your computer and use it in GitHub Desktop.
.filter("timeago", function () { | |
//time: the time | |
//local: compared to what time? default: now | |
//raw: wheter you want in a format of "5 minutes ago", or "5 minutes" | |
return function (time, local, raw) { | |
if (!time) return "never"; | |
if (!local) { | |
(local = Date.now()) | |
} | |
if (angular.isDate(time)) { | |
time = time.getTime(); | |
} else if (typeof time === "string") { | |
time = new Date(time).getTime(); | |
} | |
if (angular.isDate(local)) { | |
local = local.getTime(); | |
}else if (typeof local === "string") { | |
local = new Date(local).getTime(); | |
} | |
if (typeof time !== 'number' || typeof local !== 'number') { | |
return; | |
} | |
var | |
offset = Math.abs((local - time) / 1000), | |
span = [], | |
MINUTE = 60, | |
HOUR = 3600, | |
DAY = 86400, | |
WEEK = 604800, | |
MONTH = 2629744, | |
YEAR = 31556926, | |
DECADE = 315569260; | |
if (offset <= MINUTE) span = [ '', raw ? 'now' : 'less than a minute' ]; | |
else if (offset < (MINUTE * 60)) span = [ Math.round(Math.abs(offset / MINUTE)), 'min' ]; | |
else if (offset < (HOUR * 24)) span = [ Math.round(Math.abs(offset / HOUR)), 'hr' ]; | |
else if (offset < (DAY * 7)) span = [ Math.round(Math.abs(offset / DAY)), 'day' ]; | |
else if (offset < (WEEK * 52)) span = [ Math.round(Math.abs(offset / WEEK)), 'week' ]; | |
else if (offset < (YEAR * 10)) span = [ Math.round(Math.abs(offset / YEAR)), 'year' ]; | |
else if (offset < (DECADE * 100)) span = [ Math.round(Math.abs(offset / DECADE)), 'decade' ]; | |
else span = [ '', 'a long time' ]; | |
span[1] += (span[0] === 0 || span[0] > 1) ? 's' : ''; | |
span = span.join(' '); | |
if (raw === true) { | |
return span; | |
} | |
return (time <= local) ? span + ' ago' : 'in ' + span; | |
} | |
}) |
Hey @hrieke,
To be honest, this was posted 7 years ago, and I don't remember its context from back then.
It was probably shared as a snippet for someone else to use (since it's a public gist).
StackOverflow/the internet is also filled with similar "timeago" logic, so nothing special about this particular function.
Given that it doesn't have any explicit license mentioned, and it's a public gist, I'd say it's free for use & modification for anyone that finds it useful :)
Enjoy!
Thank you for taking the time to respond to my message.
So, just to be clear, your saying public domain?
Do you mind actually saying which public domain license you'd like to use?
Hi @hrieke,
I'll go with the MIT License since it's the most common in the community, and should allow you to use the gist as you please.
I posted the full text here too, in case you need it authored by me for legal reasons: https://gist.github.com/rodyhaddad/27bccdabfa2737fbc36b204e7ab9fff1
Let me know if that setup works for you 👍
Awesome and thank you.
Have a wonderful weekend.
Hello,
Do you have a license for your code?
Thank you,