-
-
Save 3rdp/c1e4eec0ab4226b4893f213cec430d66 to your computer and use it in GitHub Desktop.
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
function timeSince(timeStamp) { | |
var now = new Date(), | |
secondsPast = (now.getTime() - timeStamp.getTime()) / 1000; | |
if(secondsPast < 60){ | |
return [-parseInt(secondsPast), 'seconds'] | |
} | |
if(secondsPast < 3600){ | |
return [-parseInt(secondsPast/60), 'minutes'] | |
} | |
if(secondsPast <= 86400){ | |
return [-parseInt(secondsPast/3600), 'hours'] | |
} | |
if(secondsPast <= 86400 * 7) { | |
return [-parseInt(secondsPast/86400), 'day'] | |
} | |
return [-parseInt(secondsPast/(86400 * 7)), 'week'] | |
} | |
function formatRelativeTime(date, locale) { | |
return new Intl.RelativeTimeFormat(locale).format(...timeSince(date)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment