HUGO

How to find days elapsed since last post in a listing page?

Just started playing with Hugo yesterday - have zero Go experience. I am building different list pages based on taxonomy. One thing that I couldn’t figure out is how do I get the number of days elapsed since last post - essentially, I am building something like Project Hub template and I want to put a heading at the top saying “Project hasn’t moved since X days” where X is the output of {{ .Now.Sub .Date }}

I am showing the output, but it is too rich - eg: 26h29m52.828673411s - I just need this to be rounded down and show up as 1! Tried with Math module, but it takes only int arguments and not durations.

Went through Go-lang forums and I am able to do this in Go. However, I couldn’t fit any of those ideas into Go Templates.

Ideally, I should be doing this in Javascript since there could be days when nothing is posted and template code will still keep it as 1d. This is more for learning.

Thanks!
PS: Barely a day and I already am in awe of Hugo. Thanks for making such a wonderful tool.

You could maybe give .Date.Format() a try.

Couldn’t figure it out with that! However, also learned about http://flippinggodateformat.com/ :slight_smile: Anyways, I did this using moment.js using on client side javascript.

You can present the date as Unix timestamp, a value that represents the seconds since the 1st January 1970. Since both values below are integers we can easily subtract them and divide them by the number of seconds a day has (60 * 60 * 24 = 86400):

{{ div (sub .Now.Unix .Lastmod.Unix) 86400 }}

Remember that this value is static if you want to output it.

3 Likes