# Parse and Format Unix-Timestamp from API

**URL:** <https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849>\
**Category:** support\
**Created:** [December 17, 2016, 10:06pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849 "2016-12-17T22:06:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcus/32/2377_2.png) [@marcus](https://discourse.gohugo.io/u/marcus)\
**Post date:** [December 17, 2016, 10:06pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/1 "2016-12-17T22:06:38Z")

</div>

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>
```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 17, 2016, 10:40pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/2 "2016-12-17T22:40:53Z")

</div>

.time.Format(“some pattern”)

---

<div class="post-metadata">

**Author:** ![rdwatters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rdwatters/32/2848_2.png) [@rdwatters](https://discourse.gohugo.io/u/rdwatters)\
**Post date:** [December 19, 2016, 6:07pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/3 "2016-12-19T18:07:23Z")

</div>

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

> **[Functions Quick Reference](https://gohugo.io/functions/)**
>
> Comprehensive list of Hugo templating functions, including basic and advanced usage examples.

For example—

```auto
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/](https://golang.org/pkg/time/)

---

<div class="post-metadata">

**Author:** ![marcus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcus/32/2377_2.png) [@marcus](https://discourse.gohugo.io/u/marcus)\
**Post date:** [December 24, 2016, 12:04am UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/4 "2016-12-24T00:04:26Z")

</div>

@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`.

---

<div class="post-metadata">

**Author:** ![marcus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcus/32/2377_2.png) [@marcus](https://discourse.gohugo.io/u/marcus)\
**Post date:** [December 24, 2016, 12:21am UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/5 "2016-12-24T00:21:19Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 24, 2016, 12:30am UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/6 "2016-12-24T00:30:37Z")

</div>

> [@marcus](#):
>
> @bep Thanks, but how is that a solution on my question? The variable .time contains an int64 value like 1472574600000.

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.

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [December 24, 2016, 5:27pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/7 "2016-12-24T17:27:15Z")

</div>

Submitted PR upstream:

> <https://github.com/spf13/cast/pull/35>

---

<div class="post-metadata">

**Author:** ![marcus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcus/32/2377_2.png) [@marcus](https://discourse.gohugo.io/u/marcus)\
**Post date:** [December 30, 2016, 10:33pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/8 "2016-12-30T22:33:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kenold](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kenold/32/2463_2.png) [@kenold](https://discourse.gohugo.io/u/kenold)\
**Post date:** [February 8, 2017, 9:42pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/9 "2017-02-08T21:42:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [February 8, 2017, 11:17pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/10 "2017-02-08T23:17:50Z")

</div>

No, the fix is not in a stable release yet. Goal is to publish [the next release](https://github.com/spf13/hugo/milestone/12) on Feb. 20.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [February 10, 2017, 3:58pm UTC](https://discourse.gohugo.io/t/parse-and-format-unix-timestamp-from-api/4849/11 "2017-02-10T15:58:00Z")

</div>

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