英文:
How to format `2022-11-20 21:00:00+0900` to IST
问题
我现在是你的中文翻译。以下是翻译好的内容:
我现在有一个时间戳 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时间格式来完成这个转换?
英文:
I have a timestamp 2022-11-20 21:00:00+0900
now I need to convert this to IST.
So I tried this
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)
but it was not worked and giving error cannot parse
what type of golang time format i need to use to do this?
答案1
得分: 2
请尝试以下代码:
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
应该使用相同的时间格式。
英文:
try the following
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)
note that your format
and timestamp
should be in same time format
答案2
得分: 0
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")
// 这将在Asia/Calcutta时区中查找名称为CEST的时区。
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
fmt.Println(t)
// 注意:没有明确的时区时,返回给定位置的时间。
const shortForm = "2006-Jan-02"
t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
fmt.Println(t)
return
}
你可以阅读GO语言time包的文档了解更多信息。这是链接:https://pkg.go.dev/time#Date
英文:
ParseInLocation is like Parse but differs in two important ways. First, in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location. Second, when given a zone offset or abbreviation, Parse tries to match it against the Local location; ParseInLocation uses the given location.
**format of ParseInLocation is **
func ParseInLocation(layout, value string, loc *Location) (Time, error)
You try this example
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
}
For more you can read the documentation for time in GO lang time package
This is the link https://pkg.go.dev/time#Date
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论