Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

How To Convert Date To RFC3339 Extended Date String With Milliseconds In Go?

How do I convert a date string in the format "2005-06-13 04:40:51" to RFC3339 date string UTC, e.g. "2005-06-13T04:40:51.000Z"?

Also have asked this on Go forums, since have received helpful and constructive feedback previously.

Nearest I can get is by:

createdOn, err := time.Parse("2006-01-02 15:04:05", p.CreatedOn)
self.CreatedOn = createdOn.Format(time.RFC3339)

For example, this will give a date string in the format:

1970-02-13T10:31:13Z

How do I get the date to be in the format 1970-02-13T10:31:13.000Z other than generating a string using time.RFC3339 and then using a regular expression to parse the result and tag .000 before the Z character!?

It looks like time.RFC3339 includes seconds only and time.RFC3339Nano includes nanoseconds. However, there is nothing for time.RFC333Milli, i.e. milliseconds. RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"

erm..... will I have to define my own format when using go programming language to perform such a task?

At first I thought I was doing a basic typo or something, based on the 2 close link feedback postings received from the go stack overflow community for this question.

However, it looks as though that in this case a custom format is indeed required when developing in go! Another developer raised an issue at golang repository while encountering a similar issue. The issue can be found on the official golang repository, here.

Based on question feedback (read the close link below question) I have added reproducible example on go playground, here and included code below:

package main

import (
    "fmt"
    "time"
)

func main() {

    createdOn, err := time.Parse("2006-01-02 15:04:05", "2016-07-23 12:43:01")

    if err != nil {
        fmt.Printf("Error parsing date := %v", err)
    }

    sec := createdOn.Format(time.RFC3339)
    nano := createdOn.Format(time.RFC3339Nano)
    milli := createdOn.Format("2006-01-02T15:04:05.000Z07:00")

    fmt.Println("Original date/time := ", "2016-07-23 12:43:01

")
    fmt.Println("Date format using time.RFC3339 := ", sec)
    fmt.Println("
Date format using time.RFC3339Nano := ", nano)
    fmt.Println("
Date format using custom format 2006-01-02T15:04:05.000Z07:00 := ", milli)

    fmt.Println("

Milliseconds requires custom format when developing with go!")
}
question from:https://stackoverflow.com/questions/65907011/how-to-convert-date-to-rfc3339-extended-date-string-with-milliseconds-in-go

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Go, you can define your custom format based on constant values provided by the "time" package. You can see more in this thread: Parsing date/time strings which are not 'standard' formats

For your problem, here is a snippet of code that will work:

package main

import (
    "fmt"
    "time"
)

func main() {
    fm := "2006-01-02 03:04:05"
    t, _ := time.Parse(fm, "2005-06-13 04:40:51")
    fmt.Println(t.Format(time.RFC3339))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...