2 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試以下
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)
請(qǐng)注意,您的format和timestamp應(yīng)該采用相同的時(shí)間格式

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
ParseInLocation 類似于 Parse,但在兩個(gè)重要方面有所不同。首先,在沒(méi)有時(shí)區(qū)信息的情況下,Parse 將時(shí)間解釋為 UTC;ParseInLocation 將時(shí)間解釋為給定位置。其次,當(dāng)給定區(qū)域偏移量或縮寫時(shí),Parse 會(huì)嘗試將其與本地位置進(jìn)行匹配;ParseInLocation 使用給定的位置。
**ParseInLocation 的格式是 ** func ParseInLocation(layout, value string, loc *Location) (Time, error)
你試試這個(gè)例子
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 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)