简体   繁体   中英

Convert CST DateTime to Unix timestamp in Golang

I have a datetime format 0000-00-00 00:00:00 in China Standard Time, I want to convert it to unix timestamp in Golang.

I am not sure what default timestamp we need, if we call time.Unix( ) directly.

just use time.Location

func main() {
    now := "2022-08-11 11:40:00"
    location, err := time.LoadLocation("Asia/Shanghai")
    if err != nil {
        log.Fatalln(err)
    }
    t, err := time.ParseInLocation("2006-01-02 15:04:05", now, location)
    if err != nil {
        log.Fatalln(err)
    }
    //2022-08-11 11:40:00 +0800 CST
    fmt.Println(t)
    //1660189200
    fmt.Println(t.Unix())
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM