将node.js中的Date.toString()输出解析为Go中的时间。

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

Parse nodeJs Date.toString() output as time in go

问题

我有一个接收来自外部服务的 Go 服务。

数据的格式如下(json)-

{
  "firstName": "XYZ",
  "lastName": "ABC",
  "createdAtTimestamp": "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
}

请注意,createdAtTimestamp 是使用 nodeJS 的 new Date().toString() 格式输出的,没有指定任何特定的 RFC 格式。

我该如何将 createdAtTimestamp 解析为 Go 中的 time 类型?

我尝试了以下代码,但失败了-

data, _ := time.Parse(time.RFC1123, "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)")
fmt.Println(data.Format(time.RFC3339))
英文:

I have a go service which receives data from an external service.

The data looks as follows (json)-

{
  "firstName": "XYZ",
  "lastName": "ABC",
  "createdAtTimestamp": "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
}

Note that createdAtTimestamp is the output in format of nodeJS new Date().toString() which does not have any particular RFC format specified.

How do I parse createdAtTimestamp to time in go ?

I tried this but it is failing-

data, _ := time.Parse(time.RFC1123, "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)")
	fmt.Println(data.Format(time.RFC3339))

答案1

得分: 1

你在文档https://pkg.go.dev/time中看到了time.RFC1123,但实际上它并不匹配你的格式。如果你知道createdAtTimestamp的格式,首先你需要用该格式定义一个布局。

解决方案在go.dev上:https://go.dev/play/p/i9EIQjEKvog

package main

import (
	"fmt"
	"time"
)

func main() {
	// 要解析的日期和时间字符串
	createdAtTimestamp := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"

	// 日期和时间字符串的格式
	layout := "Mon Jan 02 2006 15:04:05 MST-0700 (India Standard Time)"

	// 解析日期和时间字符串
	parsedTime, err := time.Parse(layout, createdAtTimestamp)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 以RFC3339格式打印解析后的时间
	fmt.Println(parsedTime.Format(time.RFC3339))
}
英文:

You see time.RFC1123 in the docs https://pkg.go.dev/time, it does not actually match your format. https://pkg.go.dev/time#pkg-constants.

If you know the format of the createdAtTimestamp then first you have to define a layout with the format.

solution at go.dev

package main

import (
	"fmt"
	"time"
)

func main() {
	// The date and time string you want to parse
	createdAtTimestamp := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"

	// The format of the date and time string
	layout := "Mon Jan 02 2006 15:04:05 MST-0700 (India Standard Time)"

	// Parse the date and time string
	parsedTime, err := time.Parse(layout, createdAtTimestamp)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Print the parsed time in RFC3339 format
	fmt.Println(parsedTime.Format(time.RFC3339))
}

答案2

得分: 0

我认为你需要去掉(India Standard Time)(除非你知道每次都是相同的),然后可以这样做:

str := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", str[:strings.Index(str, " (")])
fmt.Println(data.Format(time.RFC3339), err)

或者,如果它总是包含(India Standard Time),你可以这样做:

str := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700 (India Standard Time)", str)
fmt.Println(data.Format(time.RFC3339), err)
英文:

I think you'll have to strip off (India Standard Time) (unless you know it will be the same each time), but you can do

https://go.dev/play/p/rWqO9W3laM2

str := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", str[:strings.Index(str, " (")])
fmt.Println(data.Format(time.RFC3339), err)

or, if it will always have (India Standard Time), you could do:

str := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700 (India Standard Time)", str)
fmt.Println(data.Format(time.RFC3339), err)

答案3

得分: 0

你可以使用以下的Layout来解析你的日期:

"Mon Jan 02 2006 15:04:05 MST-0700"

在以下代码行中:

date := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", strings.Split(date, " (")[0])
英文:

You can use below Layout to parse your date:

"Mon Jan 02 2006 15:04:05 MST-0700"

In the lines of:

date := "Mon Nov 21 2022 17:01:59 GMT+0530 (India Standard Time)"
data, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", strings.Split(date, " (")[0])

huangapple
  • 本文由 发表于 2022年12月2日 01:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/74645913.html
匿名

发表评论

匿名网友

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

确定