我有一個時間戳2022-11-20 21:00:00 0900,現在我需要將其轉換為 IST。所以我試過了
loc, _ := time.LoadLocation("Asia/Calcutta")
format := "Jan _2 2006 3:04:05 PM"
timestamp := "2022-11-20 21:00:00 0900"
ISTformat, err := time.ParseInLocation(format, timestamp, loc)
fmt.Println(ISTformat, err)
但它沒有作業并給出錯誤cannot parse
我需要使用哪種型別的 golang 時間格式來執行此操作?
uj5u.com熱心網友回復:
嘗試以下
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應該采用相同的時間格式
uj5u.com熱心網友回復:
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
}
有關更多資訊,您可以閱讀 GO lang time 包中的時間檔案這是鏈接https://pkg.go.dev/time#Date
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/535742.html
標籤:去时间数据转换
