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

王林
Release: 2024-02-08 21:18:32
forward
1047 people have browsed it

将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)"
}
Copy after login

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))
Copy after login

Workaround

You can use the following layout to parse your date:

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

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])
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!