Created
December 6, 2024 19:55
-
-
Save karenpayneoregon/1a90e045098baacc4331e887729e3cf0 to your computer and use it in GitHub Desktop.
JavaScript now-now
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
/* | |
* var pastDate = new Date('2014-10-01T02:30'); | |
* var message = fromNow(pastDate); | |
* //=> '2 days ago' | |
* | |
* @param {Date} Native JavaScript Date object | |
* @return {string} | |
*/ | |
function fromNow(date) { | |
var seconds = Math.floor((new Date() - date) / 1000); | |
var years = Math.floor(seconds / 31536000); | |
var months = Math.floor(seconds / 2592000); | |
var days = Math.floor(seconds / 86400); | |
if (days > 548) { | |
return years + ' years ago'; | |
} | |
if (days >= 320 && days <= 547) { | |
return 'a year ago'; | |
} | |
if (days >= 45 && days <= 319) { | |
return months + ' months ago'; | |
} | |
if (days >= 26 && days <= 45) { | |
return 'a month ago'; | |
} | |
var hours = Math.floor(seconds / 3600); | |
if (hours >= 36 && days <= 25) { | |
return days + ' days ago'; | |
} | |
if (hours >= 22 && hours <= 35) { | |
return 'a day ago'; | |
} | |
var minutes = Math.floor(seconds / 60); | |
if (minutes >= 90 && hours <= 21) { | |
return hours + ' hours ago'; | |
} | |
if (minutes >= 45 && minutes <= 89) { | |
return 'an hour ago'; | |
} | |
if (seconds >= 90 && minutes <= 44) { | |
return minutes + ' minutes ago'; | |
} | |
if (seconds >= 45 && seconds <= 89) { | |
return 'a minute ago'; | |
} | |
if (seconds >= 0 && seconds <= 45) { | |
return 'a few seconds ago'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment