Home  >  Article  >  Backend Development  >  Parse nodeJs Date.toString() output into time in go

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

王林
王林forward
2024-02-08 21:18:321028browse

将nodeJs Date.toString()输出解析为go中的时间

php editor Strawberry will introduce to you how to parse the Date.toString() output of nodeJs into time in go. During the development process, we often encounter data format conversion problems between different programming languages, especially when dealing with dates and times. Node.js and Go are two commonly used programming languages ​​that have slightly different time formats. This article will explain in detail how to parse a date string in Node.js into a time object in Go to help you solve this problem.

Question content

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

The data is as follows (json)-

{
  "firstname": "xyz",
  "lastname": "abc",
  "createdattimestamp": "mon nov 21 2022 17:01:59 gmt+0530 (india standard time)"
}

Please note that createdattimestamp is the output of nodejs new date().tostring() format, which does not specify any specific rfc format.

How to parse createdattimestamp into time in go?

I tried, but failed -

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

Workaround

You can use the following layout to parse your date:

"mon jan 02 2006 15:04:05 mst-0700"

as follows:

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])

The above is the detailed content of Parse nodeJs Date.toString() output into time in go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete