Hey just a small improvement to N something ago example to show/hide plural.
const fromAgo = (date) => { const ms = Date.now() - date.getTime(); const seconds = Math.round(ms / 1000); const minutes = Math.round(ms / 60000); const hours = Math.round(ms / 3600000); const days = Math.round(ms / 86400000); const months = Math.round(ms / 2592000000); const years = Math.round(ms / 31104000000); let ago; if(seconds < 60) ago = `${seconds} second"`; else if (minutes < 60) ago = `${minutes} minute`; else if (hours < 24) ago = `${hours} hour`; else if (days < 30) ago = `${days} day`; else if (months < 12) ago = `${months} month`; else ago = `${years} year`; const isSingular = ago.indexOf("1 ") === 0; ago += isSingular ? " ago" : "s ago"; return ago; } const createdAt = new Date(2021, 0, 5); fromAgo(createdAt); // 14 days ago
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey just a small improvement to N something ago example to show/hide plural.