hi, I need help with how I would archive this … using the build-in Hugo features
I’m trying to get the current age of someone for example
john doe was born 1 January 1998
jane does was born 1 March 1899
how can i get current age using Hugo https://gohugo.io/functions/time/ and math
You can’t. But you could use unix time
(today - birthday)/ (60s60m 24h*365d) is an approximate year diff. You can probably work around the leap-years by taking the year difference divided by 4 rounded down and subtract that from the (today-birthday) calculation.
I realized I had a similar request a while back:
Hello,
this is by the way my first go at Go, so bear with me if I ask a lot of obvious seeming questions these days
I have in my old WordPress blog an widget area with a text like “He is at this location for x days now” and calculated that with PHP. Which is easy
How would the equivalent work in Go? I tried the following:
{{ start := "2005-01-08"}}
{{ $ageDays := div (sub now.Unix .start.Unix) 86400 }}
{{ with $ageDays }}{{.}}{{ end }}
which of course fails. How do I transform…
the issue is that im dealling with people born before 1970S
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970, UTC.
Read my second answer. That should solve it.
{{ $start := “2005-01-08” | time }}
{{ $diff := now.Sub $start }}
Here is another sample in pure Go: