Get a list of valid time zones in Go

前端 未结 3 726
挽巷
挽巷 2020-12-31 13:16

I\'d like to write a method that will populate a Go Language array with the common timezones that are accepted by the time.Format() call, for use in an HTML tem

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 14:10

    Go's time pkg uses a timezone database.

    You can load a timezone location like this:

    loc, err := time.LoadLocation("America/Chicago")
    if err != nil {
        // handle error
    }
    
    t := time.Now().In(loc)
    

    The Format function is not related to setting the time zone, this function takes a fixed reference time that allows you to format the date how you would like. Take a look at the time pkg docs.

    For instance:

    fmt.Println(t.Format("MST")) // outputs CST
    

    Here is a running example

提交回复
热议问题