Jay
Jay

Reputation: 20169

How do you parse date in the format:

It's not clear to me from the documentation how I would parse a date in this somewhat strange format. It seems like it might not be possible.

2016-07-08T08:34:24+00:00

The following does not work (go play link)

package main

import (
    "fmt"
    "time"
)

func main() {
    date := "2016-07-08T08:34:24+00:00"
    d, err := time.Parse("2006-01-02T15:04:05+07:00", date)
    if err == nil {
        fmt.Println(d)
    } else {
        fmt.Println(err)
    }
}

Obviously a regexp could first check for this format and transform the + to a -, but that implies the standard library cant parse this date.

Upvotes: 0

Views: 412

Answers (1)

user142162
user142162

Reputation:

Go's reference layout uses -7 hours as the timezone offset, but you used +7 hours:

package main

import (
    "fmt"
    "time"
)

func main() {
    date := "2016-07-08T08:34:24+00:00"
    d, err := time.Parse("2006-01-02T15:04:05-07:00", date)
    if err == nil {
        fmt.Println(d)
    } else {
        fmt.Println(err)
    }
}

https://play.golang.org/p/FNzx57R2jy

Upvotes: 3

Related Questions