Heya folks, I have a conceptual task I’m not grokking. Basically, I have a bunch of data with created_at timestamps. I’d like to turn that into “3 years ago”, or similar.
I was looking for a function that might do the thing I wanted, but now I think I need to do some math.
One approach I’ve thought: turn values into Unix epoch seconds, find difference, convert answer back to human readable time statement.
I’d appreciate alternatives or prior art. Is there a programming pattern that could assist me?
git clone --single-branch -b hugo-forum-topic-37496 https://github.com/jmooring/hugo-testing hugo-forum-topic-37496
cd hugo-forum-topic-37496
npm install
hugo server
It would be convenient if we could do:
{{ .Date | time.Format ":fuzzy" }}
to produce a locale-aware string, but only if you intend to frequently rebuild the site. If it says “1 hour ago” and you last updated the site a year ago…
Thanks for putting the example repo together, I am so used to working on projects alone I forget to mention I was looking for a go template solution, in case there was a much easier way to do it.
If you set cutoff to 604800 (the number of seconds in a week), a fuzzy date will appear during the first week following the specified date. After one week, the actual date will appear.
A Hugo implementation of fuzzy dates would be much more useful if we had a cutoff value, but invert its meaning. For example, if the specified date is less than a month ago, display the actual date, otherwise display a fuzzy date. This would provide more accurate results for sites that are built once per week, month, etc.
I’m not sure how to parameterize a cutoff value with this:
{{ .Date | time.Format ":fuzzy" }}
Perhaps another site configuration setting. Food for thought…
I am using js from this example to show age of the tour guides on a client website. the only thing I set in Hugo is the date of birth and let the js calculate the time ago.
I suspect the pragmatic approach with :fuzzy in a static context would be to have a fairly long default cutoff. A config option may be nice-to-have, but the main use case is to get a distinct marker for “old content”, I suspect …