如何在Golang中解析IST格式的日期?

huangapple go评论68阅读模式
英文:

How to parse Date in IST format in Golang?

问题

我可以帮你翻译这段代码。这段代码的作用是在Golang中设置一个IST格式的时间,时间为上午10点。以下是翻译好的代码:

time.Date(t.Year(), t.Month(), time.Now().Day(), 10, 0, 0, 0, time.FixedZone("IST", 5*60*60+30*60))

请注意,这里使用了time.FixedZone函数来设置时区为IST(印度标准时间),偏移量为5小时30分钟。

英文:
time.Date(t.Year(), t.Month(), time.Now().Day(), 10, 0, 0, 0, time.UTC)

I want to set dateTime of 10:00:00 AM in IST format in golang.

答案1

得分: 4

这取决于你手头的时间格式。Go语言在time包中提供了一些标准的时间格式,但如果是自定义的格式,你也可以指定自己的标准。关于时区,你可以解析或输出特定时区的时间。下面是一个例子,演示了如何将一个时间字符串解析为IST时区,并将其输出为UTC时区。根据你的问题描述不够清晰,但希望这个例子能对你有所帮助:

// 首先,我们创建一个时区位置对象的实例
loc, _ := time.LoadLocation("Asia/Kolkata")

// 这是我们自定义的时间格式。注意,格式必须与时间字符串完全匹配
format := "Jan _2 2006 3:04:05 PM"

// 这是你的时间戳
timestamp := "Jun 25 2015 10:00:00 AM"

// 现在我们解析它,考虑它是在IST时区
t, err := time.ParseInLocation(format, timestamp, loc)

// 打印它会以IST时区的形式输出,但如果你想要设置时区为UTC,也是可以的
fmt.Println(t, err)

// 示例 - 获取UTC时间戳
fmt.Println(t.UTC())

以上是代码部分的翻译结果,其他内容不做翻译。

英文:

It depends on the format of the time you have at hand. Go has some standard time formats ready as consts in the time package, but you can specify your own standard if it's custom. Regarding the time zone, you can parse or output a time in a specific time zone. Here is an example of parsing a time string in IST, and outputting it as UTC. It's not clear from your question what is your precise problem but I hope this helps:

// First, we create an instance of a timezone location object
loc, _ := time.LoadLocation("Asia/Kolkata")

// this is our custom format. Note that the format must point to this exact time
format := "Jan _2 2006 3:04:05 PM"

// this is your timestamp
timestamp := "Jun 25 2015 10:00:00 AM"

// now we parse it, considering it's in IST
t, err := time.ParseInLocation(format, timestamp,  loc)

// printing it prints it in IST, but you can set the timezone to UTC if you want
fmt.Println(t, err)

// example - getting the UTC timestamp
fmt.Println(t.UTC())

huangapple
  • 本文由 发表于 2015年6月25日 16:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/31044955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定