如何使用特定时区解析时间

我要从字符串中获取时间结构。我正在使用布局的函数。time.ParseTime()"2006-01-02 15:04"

当我使用任何有效的时间字符串执行该函数时,我得到一个指向该时间戳的时间结构,但它是UTC格式。

如何将其更改为其他时区?需要明确的是,我想要相同的时间戳,但具有不同的时区。我不想在时区之间转换;我只想获得相同的时间对象,但不是UTC。


白板的微信
浏览 113回答 1
1回答

繁华开满天机

使用时间。解析In位置,用于在没有给定时区时解析给定位置中的时间。 是您当地的时区,请将其作为您的位置传递。time.Localpackage mainimport (    "fmt"    "time")func main() {    // This will honor the given time zone.    // 2012-07-09 05:02:00 +0000 CEST    const formWithZone = "Jan 2, 2006 at 3:04pm (MST)"    t, _ := time.ParseInLocation(formWithZone, "Jul 9, 2012 at 5:02am (CEST)", time.Local)    fmt.Println(t)    // Lacking a time zone, it will use your local time zone.    // Mine is PDT: 2012-07-09 05:02:00 -0700 PDT    const formWithoutZone = "Jan 2, 2006 at 3:04pm"    t, _ = time.ParseInLocation(formWithoutZone, "Jul 9, 2012 at 5:02am", time.Local)    fmt.Println(t)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go