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\n\n")
fmt.Println("Date format using time.RFC3339 := ", sec)
fmt.Println("\nDate format using time.RFC3339Nano := ", nano)
fmt.Println("\nDate format using custom format 2006-01-02T15:04:05.000Z07:00 := ", milli)
fmt.Println("\n\nMilliseconds requires custom format when developing with go!")
}