5

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!")
}
Braiam
  • 1
  • 11
  • 47
  • 78
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62

2 Answers2

4

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:

Solution: Formats 2006-01-02 15:04:05 as 2016-07-23T12:43:01.000Z

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!")
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62
2

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))
}
Kuriboh
  • 21
  • 2
  • Thanks @Kuriboh, have tried that and get ```Successfully inserted 100 records into the database CreatedOn: 1986-12-03 22:01:35 parsing time "1986-12-03 22:01:35": hour out of range ``` My code is ```fmt.Println("CreatedOn: ", p.CreatedOn) createdOn, err := time.Parse("2006-01-02 03:04:05", p.CreatedOn) fmt.Println(err.Error()) fmt.Println(err.Error()) self.CreatedOn = createdOn.Format(time.RFC3339)``` Apologies on formatting! Have updated question with error message it gives – anon_dcs3spp Jan 26 '21 at 18:46