Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
7 May 2019 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here