Cast a int from a timestamp unix - Time

Hello everybody,

I have some difficulties with the Time function. I can’t get an int from a timestamp Unix

i’ve tried

{{time ((div (int .Params.dateAdd) 1000)) }}

{{$test := (time 1496333001).Unix }} {{ $test }}

{{(time (div (int .Params.dateAdd) 1000)).Month }}

or with go it's ok 

package main

import (
“fmt”
“time”
)

func main() {
fmt.Printf("%d", (int((time.Unix(1496332344, 0)).Month())));
}


https://play.golang.org/p/w_ub-bDlP8

Thx :p

There is no Time function that I know of.

In Hugo templates you can do:

{{ now }}
{{ now.Unix }}
{{ now.UnixNano }}
{{ .Date.UnixNano }}

Etc. Unix and UnixNano are ints.

In my issue I would like to have the index of the month In order to translate it

Should I assume your front matter field for dateAdd is a Unix time stamp in quotes/string? time converts the timestamp string into a time.Time. So, if the front matter key-value is a Unix time stamp, first format it through time then access .Month, which, by the way, returns the string (e.g., June) and not the month number (from what I can tell testing locally).

Maybe this will help get you what you want?

Let’s say you have addDate: "1489276800" in your front matter:

{{$time := time (int .Params.addDate)}}
<span>{{$time.Month}}</span>
=> "March"

Or are you looking for month number since you mention the “index of the month?”

Hi @rdwatters thank you for your time, indeed i’m looking for a month number. I have a /data/month.yaml for translating in regard to https://gohugo.io/tutorials/create-a-multilingual-site/#customize-dates

Gotcha. This took a bit of fiddling, but hopefully this works for you.

I’ll include my full example above as well:

{{$time := time (int .Params.addDate)}}
<span>{{$time.Month}}</span>
=> "March"
{{ $monthindex := printf "%d" $time.Month }}
<span>{{$monthindex}}</span>
=> 3
1 Like

Thanks @rdwatters i will try it.

1 Like

Yes it’s good ! I finally took the time to improve my ui

<time>

{{$time := time (div (int .Params.dateAdd) 1000)}}
{{ $monthindex := index $.Site.Data.mois (printf "%d" $time.Month) }} 
{{$time.Day}} {{$monthindex}} {{$time.Year}}

</time>
1 Like