Format Reading Time with JavaScript

Updated: July 4, 2020

Reading time is a pretty common functionality in blogs.

The formatReadingTime() function below displays cups of coffee as indicators of the minutes it takes to read the post.

function formatReadingTime(timeToRead) {
const minutes = `${timeToRead > 1 ? `minutes` : `minute`}`;
const cups = Math.round(timeToRead / 5);

return `${new Array(cups || 1)
.fill("☕️")
.join("")} ${timeToRead} ${minutes} read`;
}

formatReadingTime(1); // "☕️ 1 minute read"
formatReadingTime(9); // "☕️☕️ 9 minutes read"
formatReadingTime(30); // "☕️☕️☕️☕️☕️☕️ 30 minutes read"