ReadingTime Define


I have added the readingtime in the post with the following way:

title = "Introduction!"
date = 2018-07-19T6:23:49+02:00
description = "How are you."
draft = true
toc = false
readingtime = 55
categories = ["General"]
tags = ["Fallout4"]

Although, it seems it, didn’t work.

May I know what did I do wrong?

What you did was defining a page-sepcific parameter. For display purposes you need to use .Params.readingtime in your template (assuming that your post is the current content otherwise you need to assign the post to a variable depending on your needs). The official .ReadingTime variable is provided by hugo for each post and it’s value it automatically calculated so you cannot overwrite it.

Hope that helps


I didn’t know it’s actually automated, thank you sir!

Here’s the partial I use:

{{ if gt .ReadingTime 1 }}
    {{ .Scratch.Set "readingTime" "minutes" }}
{{ else }}
    {{ .Scratch.Set "readingTime" "minute" }}
{{ end }}

<p>Reading time: {{ .ReadingTime }} {{ .Scratch.Get "readingTime" }}</p>

{{/*<!-- NOTE: Alternative writing:
    {{ $readTime := cond (gt .ReadingTime 1) "minutes" "minute" }}
    <p>Reading time: {{ .ReadingTime }} {{ $readTime }}.</p>

Hope this helps.


By the way, you can also calculate the reading time yourself if you want. That way it’s possible to use a custom words per minutes to estimate reading time. Or use seconds in the output (like the readingtime front matter variable from the first post seems to do).

This Hugo template code outputs the reading time in minutes and seconds, assuming 220 words per minute:

{{ $readTime := mul (div (countwords .Content) 220.0) 60 }}

{{ $minutes := math.Floor (div $readTime 60) }}
{{ $seconds := mod $readTime 60 }}

<p>Reading time: {{ $minutes }} {{ cond (eq $minutes 1) "minute" "minutes" }} and
    {{ $seconds }} {{ cond (eq $seconds 1) "second" "seconds" }}.</p>

The output is something like (depending on your content’s length):

Reading time: 1 minute and 35 seconds.

Reading time: 6 minutes and 45 seconds.

Reading time: 0 minutes and 22 seconds.

Reading time: 33 minutes and 0 seconds.

I wrote a blog post about this approach yesterday. You can find it here: calculate reading time of a Hugo string.


Jura, you can also calculate the wordcount by using .WordCount instead of (countwords .Content).

1 Like

Thanks, that’s of course correct. I unfortunately can’t update the post above anymore, but with .CountWords the first line of the above example becomes:

{{ $readTime := mul (div .CountWords 220.0) 60 }}
1 Like

If you need to count on CJK languages, you can use countrunes.

That’s true. I wrote an in-depth article about that as well: how to get the length of a Hugo string.

As a conclusion:

  • Use countwords to measure the words in a string, provided there are no Chinese, Japanese, or Korean-like language characters.
  • For those languages:
    • Use countrunes to measure the length of a string, excluding whitespace and HTML code.
    • With strings.RuneCount we can measure all the runes* in a string.
  • len returns the length of a string in characters. But this function is only reliable when that string only contains ASCII characters.
    • Else len returns an incorrect value and you’re better off using strings.RuneCount or countrunes to measure the characters in a string.

*: a rune is a single Unicode character. Characters like a , 8 , * , ç , ή , and , are all 1 rune.