Parse and Format Unix-Timestamp from API

The goal is to create a list of Meeup events with event dates. The events are fetched from the Meetup-API and the time field is in Milliseconds. What I don’t get is how to create a formatted Date.

{{ $meetups := getJSON "https://api.meetup.com/SciFiMuc/events?status=past" }}
<ul>
{{ range $meetups }}
    <li>{{ .name }} {{ div .time 1000 }} *{{ div .time 1000 | .Time.Unix | .Time.String }}*</li>
{{ end }}
</ul>

The Rendered output is something like

<li>Science-Fiction Meetup 1.4751684e+09 **</li>

.time.Format(“some pattern”)

1 Like

@marcus Maybe looking at the dateFormat function, which will format any string that you give it and is slightly different form .Date.Format

For example—

mystringdate: 2016-12-19

{{$date := .Params.mystringdate}}
{{ dateFormat "Monday, Jan 2, 2006" $date  }}
=>
Monday, Dec 19, 2016 

Also golang docs on time:

https://golang.org/pkg/time/

@rdwatters Thanks, but I don’t see how this could work because I’ve got an Unix timestamp and dateFormat expects an instance of Go’s time.Time.

Here’s a minimal example to reproduce without Meetup API:

{{ div 1472574600000 1000 | dateFormat "Monday, Jan 2, 2006" }}

where 1472574600000 would be the value of field .time as returned by Meeup REST API. This yields the error

ERROR: 2016/12/24 00:59:36 general.go:212: Error while rendering homepage: template: 
  theme/index.html:52:40: executing "theme/index.html" at <dateFormat "Monday, ...>:
  error calling dateFormat: Unable to Cast 1472574600 to Time

I guess my problem is converting int64 value 1472574600000 to an instance of time.Time.

@bep Thanks, but how is that a solution on my question? The variable .time contains an int64 value like 1472574600000. I do know how Golangs time formatting works (as described in the documentation of Golangs time package) but cannot call it on the variable .time as generated by the rest call because it is not a instance of Golang time.Time.

So the big question is how to convert an int64 value to an instance of time.Time?

Maybe it would be better to add a step in the generation pipeline:

  1. Run a seperata script/program that queries the meetup API to create a CSV file suitable for Hugo
  2. Run Hugo

What would be the best approach?

I didn’t know that.

You could try the dateFormat – It uses cast.ToTime to convert the input string, so maybe it’s smart enough to handle epoch values. If not, my best suggestion would be to improve that library yourself.

Submitted PR upstream:

4 Likes

Thanks, great work!

I’ve tested with Hugo 0.19-dev as from 2016-12-30, which already includes your improvements to cast and the following works perfectly:

{{ div .time 1000 | int | dateFormat "Monday, Jan 2, 2006" }}

This turns, for example,

1.4725746e+12

into

Tuesday, Aug 30, 2016

Problem solved!

1 Like

any solution for this on Hugo 0.18? The solution above didn’t work.

No, the fix is not in a stable release yet. Goal is to publish the next release on Feb. 20.

1 Like

@moorereason but someone would have to pull the latest cast change into Hugo. I tried, and got some test failures, so I bailed.

1 Like