65.9K
CodeProject is changing. Read more.
Home

Adding a Estimated Time to Read to a .NET Core Blog with Razor

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.92/5 (6 votes)

May 7, 2019

CPOL
viewsIcon

3799

How to add the estimated time to read to a .NET Core Blog

Introduction

This week, I implemented a feature that shows you how much time you have to spend to read a blog article.

Background

In the first step, I used my model BlogStory to get the content.

Using the Code

  1. It counts the spaces between the words and adds 1. So now, we know how many words we have in that article.
  2. Most people can read 200 to 250 in one minute. So we need to divide the counted words by 250. Then we know how many minutes it will take to read.
  3. Then we combine a modulo with a divide to get the seconds.
@{
    var word_count = @Model.Body;
    var counts = word_count.Count(ch => ch == ' ') + 1;    
    var minutes = counts / 250;
    var seconds = counts % 250 / (250 / 60);
    var str_minutes = (minutes == 1) ? "Minute " : "Minutes ";
    var str_seconds = (seconds == 1) ? "Second " : "Seconds ";    
}

Now, we place the code for displaying the estimated time to read:

<i class="fas fa-clock"></i> @minutes @str_minutes  @seconds @str_seconds

On the place where this snippet is, will be the estimated time to read.

History

  • 7th May, 2019: Initial version