# JavaScript 'time ago' function

DevFeed: [JavaScript 'time ago' function](<https://devfeed.tech/articles/javascript-time-ago-function-37300.md>)

Original publisher: [Read original article](<https://muffinman.io/blog/javascript-time-ago-function/>)

Author: Stanko

Published: 2018-02-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [Stanko Tadić](<https://devfeed.tech/sources/stanko-tadic.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Code](<https://devfeed.tech/topics/code.md>), [formatting](<https://devfeed.tech/topics/formatting.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [formatting](<https://devfeed.tech/tags/formatting.md>), [function](<https://devfeed.tech/tags/function.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [snippet](<https://devfeed.tech/tags/snippet.md>)

## AI overview

A reusable JavaScript time-ago function formats dates into relative or calendar-style strings, including seconds, minutes, today, yesterday, current-year dates, and older dates. The snippet is designed to remain customizable for project-specific needs.

## Source excerpt

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()