Display date in relative time format?

Is it possible to express a date in a relative time format? e.g. 1 hour ago, 3 days ago, or 4 months ago etc…

I’m new to Hugo (started yesterday) and couldn’t find anything on this forum or via Google.

Thanks.

Loving Hugo BTW :slightly_smiling:

Check out AddDate:

https://hugodocs.info/functions/adddate

Thanks for the suggestion.

Excuse my ignorance but I’m not sure how I can use that to express a point in time in a relative format.

e.g. Express the date ‘2017-05-01T00:42:28+01:00’ (01 May 2017) as ‘3 days ago’, or ‘2016-05-03T00:54:21+01:00’ as ‘1 year ago’.

@colin Ah, gotcha. Check out Unix:

https://hugodocs.info/functions/unix/

See the second example but please note the admonition I wrote at the bottom re:static generation.

Something like “3 days ago” will only be accurate if you are building your site once a day. If not, I’d recommend a client side solution like moment.js.

HTH

@rdwatters Thank you.

I think I must have been tired last night and had my dynamic webdev head on!

I’ll probably go with your suggestion to use moment.js.

Thanks again for you help. :+1:

1 Like