2 回答
TA贡献1951条经验 获得超3个赞
尝试以下
loc, _ := time.LoadLocation("Asia/Calcutta")
format := "2006-01-02 15:04:05-0700"
timestamp := "2022-11-20 21:00:00+0900"
// ISTformat, _ := time.ParseInLocation(format, timestamp, loc)
// fmt.Println(ISTformat)
parsed_time, _ := time.Parse(format, timestamp)
IST_time := parsed_time.In(loc)
fmt.Println("Time in IST", IST_time)
请注意,您的format和timestamp应该采用相同的时间格式
TA贡献1824条经验 获得超5个赞
ParseInLocation 类似于 Parse,但在两个重要方面有所不同。首先,在没有时区信息的情况下,Parse 将时间解释为 UTC;ParseInLocation 将时间解释为给定位置。其次,当给定区域偏移量或缩写时,Parse 会尝试将其与本地位置进行匹配;ParseInLocation 使用给定的位置。
**ParseInLocation 的格式是 ** func ParseInLocation(layout, value string, loc *Location) (Time, error)
你试试这个例子
package main
import (
"fmt"
"time"
)
func main() {
loc, _ := time.LoadLocation("Asia/Calcutta")
// This will look for the name CEST in the Asia/Calcutta time zone.
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
fmt.Println(t)
// Note: without explicit zone, returns time in given location.
const shortForm = "2006-Jan-02"
t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
fmt.Println(t)
return
}
- 2 回答
- 0 关注
- 102 浏览
添加回答
举报