Parse date difference in template

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 :wink:

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 :wink:

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 string value in start to a unix value so that the calculation works?

I know that I could just use the integer unixtime of 8 in the morning of Jan 8th 2005, but I’d like to learn playing with time in (Hu)Go along the way.

Thanks
Patrick

{{ $start := “2005-01-08” | time }}

Also:

{{ $diff := now.Sub $start }}

// $diff should now be a Duration object: time package - time - Go Packages
Hours: {{ $diff.Hours }}

3 Likes

Thank you, that did the job :slight_smile:

1 Like