Created
February 14, 2020 17:22
-
-
Save pomber/6195066a9258d1fb93bb59c206345b38 to your computer and use it in GitHub Desktop.
Get a time ago human friendly string from a date (like dates in twitter).
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
const MINUTE = 60, | |
HOUR = MINUTE * 60, | |
DAY = HOUR * 24, | |
YEAR = DAY * 365; | |
function getTimeAgo(date) { | |
const secondsAgo = Math.round((+new Date() - date) / 1000); | |
if (secondsAgo < MINUTE) { | |
return secondsAgo + "s"; | |
} else if (secondsAgo < HOUR) { | |
return Math.floor(secondsAgo / MINUTE) + "m"; | |
} else if (secondsAgo < DAY) { | |
return Math.floor(secondsAgo / HOUR) + "h"; | |
} else if (secondsAgo < YEAR) { | |
return date.toLocaleString("default", { day: "numeric", month: "short" }); | |
} else { | |
return date.toLocaleString("default", { year: "numeric", month: "short" }); | |
} | |
} |
Minor update if you're in need of a slightly more verbose version of this awesome gist that also treats all the time scales equally:
const MINUTE = 60, HOUR = MINUTE * 60, DAY = HOUR * 24, WEEK = DAY * 7, MONTH = DAY * 30, YEAR = DAY * 365 function getTimeAgo(date) { const secondsAgo = Math.round((+new Date() - date) / 1000) let divisor = null let unit = null if (secondsAgo < MINUTE) { return secondsAgo + " seconds ago" } else if (secondsAgo < HOUR) { [divisor, unit] = [MINUTE, 'minute'] } else if (secondsAgo < DAY) { [divisor, unit] = [HOUR, 'hour'] } else if (secondsAgo < WEEK) { [divisor, unit] = [DAY, 'day'] } else if (secondsAgo < MONTH) { [divisor, unit] = [WEEK, 'week'] } else if (secondsAgo < YEAR) { [divisor, unit] = [MONTH, 'month'] } else if (secondsAgo > YEAR) { [divisor, unit] = [YEAR, 'year'] } count = Math.floor(secondsAgo / divisor) return `${count} ${unit}${(count > 1)?'s':''} ago` }Edit: Updated to include singular/plural for all time units
Just what I needed @rex, thanks! Tiny fix: You are missing const on the count.
Problem is it'll return "1 seconds ago". Here's my modified version:
const MINUTE = 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const WEEK = DAY * 7;
const MONTH = DAY * 30;
const YEAR = DAY * 365;
function getTimeAgo(date) {
const secondsAgo = Math.round((Date.now() - Number(date)) / 1000);
if (secondsAgo < MINUTE) {
return secondsAgo + ` second${secondsAgo !== 1 ? "s" : ""} ago`;
}
let divisor;
let unit = "";
if (secondsAgo < HOUR) {
[divisor, unit] = [MINUTE, "minute"];
} else if (secondsAgo < DAY) {
[divisor, unit] = [HOUR, "hour"];
} else if (secondsAgo < WEEK) {
[divisor, unit] = [DAY, "day"];
} else if (secondsAgo < MONTH) {
[divisor, unit] = [WEEK, "week"];
} else if (secondsAgo < YEAR) {
[divisor, unit] = [MONTH, "month"];
} else {
[divisor, unit] = [YEAR, "year"];
}
const count = Math.floor(secondsAgo / divisor);
return `${count} ${unit}${count > 1 ? "s" : ""} ago`;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minor update if you're in need of a slightly more verbose version of this awesome gist that also treats all the time scales equally:
Edit: Updated to include singular/plural for all time units