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 (

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

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)}}
=> "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

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)}}
=> "March"
{{ $monthindex := printf "%d" $time.Month }}
=> 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 (div (int .Params.dateAdd) 1000)}}
{{ $monthindex := index $.Site.Data.mois (printf "%d" $time.Month) }} 
{{$time.Day}} {{$monthindex}} {{$time.Year}}

1 Like