Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.92/5 (6 votes)
7 May 2019CPOL 3.7K   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.
JavaScript
@{
    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:

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CPS Solutions GmbH & Co. KG
Germany Germany
I'm programming in Ruby, Python and C#. Currently i'm learning Java and SQL. In business i started to learn building mobile apps for different use cases.

Comments and Discussions

 
-- There are no messages in this forum --