Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get a time ago human friendly string from a date (like dates in twitter).
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" });
}
}
@rex
Copy link

rex commented Feb 23, 2021

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

@arturcarvalho
Copy link

arturcarvalho commented Mar 1, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment