JavaScript 'time ago' function

Snippet for a rather popular requirement. Formatting a date in a nice way, using infamous "time ago" function. This is the basic version which I adapt to fit a specific project. To keep it easily customizable to your needs, I haven't packaged it up.

Just pass a date to it, and function will return one of the seven possible formats:

  • now - if no more than five seconds elapsed
  • about a minute ago - in no more than ninety seconds elapsed
  • 24 minutes ago - for anything in the last hour
  • Today at 11:19 - for today
  • Yesterday at 7:32 - for yesterday
  • 15. February at 17:45 - for dates in the current year
  • 23. October 2017. at 0:59 - for anything else

Feel free to play with it and add more cases if you need them.

Code

const MONTH_NAMES = [
  'January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'
];


function getFormattedDate(date, prefomattedDate = false, hideYear = false) {
  const day = date.getDate();
  const month = MONTH_NAMES[date.getMonth()];
  const year = date.getFullYear();
  const hours = date.getHours();
  let minutes = date.getMinutes();

  if (minutes < 10) {
    // Adding leading zero to minutes
    minutes = `0${ minutes }`;
  }

  if (prefomattedDate) {
    // Today at 10:20
    // Yesterday at 10:20
    return `${ prefomattedDate } at ${ hours }:${ minutes }`;
  }

  if (hideYear) {
    // 10. January at 10:20
    return `${ day }. ${ month } at ${ hours }:${ minutes }`;
  }

  // 10. January 2017. at 10:20
  return `${ day }. ${ month } ${ year }. at ${ hours }:${ minutes }`;
}


// --- Main function
function timeAgo(dateParam) {
  if (!dateParam) {
    return null;
  }

  const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam);
  const DAY_IN_MS = 86400000; // 24 * 60 * 60 * 1000
  const today = new Date();
  const yesterday = new Date(today - DAY_IN_MS);
  const seconds = Math.round((today - date) / 1000);
  const minutes = Math.round(seconds / 60);
  const isToday = today.toDateString() === date.toDateString();
  const isYesterday = yesterday.toDateString() === date.toDateString();
  const isThisYear = today.getFullYear() === date.getFullYear();


  if (seconds < 5) {
    return 'now';
  } else if (seconds < 60) {
    return `${ seconds } seconds ago`;
  } else if (seconds < 90) {
    return 'about a minute ago';
  } else if (minutes < 60) {
    return `${ minutes } minutes ago`;
  } else if (isToday) {
    return getFormattedDate(date, 'Today'); // Today at 10:20
  } else if (isYesterday) {
    return getFormattedDate(date, 'Yesterday'); // Yesterday at 10:20
  } else if (isThisYear) {
    return getFormattedDate(date, false, true); // 10. January at 10:20
  }

  return getFormattedDate(date); // 10. January 2017. at 10:20
}

Demo

Update, August 2020

Comments (8)

Achim
04. Jan 2019, 20:47

Hi, thank you. It saved me some time. I wrote a query function to automatically update the dates:

[code removed]

Burak
05. Nov 2019, 15:54

Thanks buddy. you saved my hours

McAustin
30. May 2020, 09:06

it didn't work for me saying today @ particular time frame,

Stanko
30. May 2020, 09:23

McAustin, it seems your comment got cut off. Can you please provide a test case that didn't work? And the solution if you figured it out.

Cheers!

pravynandas
04. Jul 2020, 04:48

Works Great !! Khudos.

kamaucardozo
22. Aug 2020, 08:44

thanks very much

elijah
22. Aug 2020, 08:57

i am getting an error that timeAgo is not a function

Stanko
22. Aug 2020, 09:39

Thanks guys!

elijah, I just created a demo on Codepen, I added it at the bottom of the post. Hope that helps!

Cheers!