Reputation: 31
I have to convert a string date with this format:
Thu, 01 Nov 2018 14:20:34 +0700
.. into this format:
2018-11-01 14:20:34
I tried it with the following code:
dt_pub, err := time.Parse("RFC1123", Thu, 01 Nov 2018 14:20:34 +0700)
dt_pub.Format("2006-01-02 15:04:05")
But unfortunately, the result looks like this:
0001-01-01 00:00:00 +0000 UTC
Upvotes: 1
Views: 437
Reputation: 166549
See Go package time.
0001-01-01 00:00:00 +0000 UTC
is the zero value for time.Time
. The zero value is returned when a parsing error has occurred.
Check for errors. Use the time.RFC1123Z
(RFC1123 with numeric zone) layout for parsing to match your input. For example,
package main
import (
"fmt"
"time"
)
func main() {
dt_pub, err := time.Parse(time.RFC1123Z, "Thu, 01 Nov 2018 14:20:34 +0700")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(dt_pub)
fmt.Println(dt_pub.Format("2006-01-02 15:04:05"))
}
Playground: https://play.golang.org/p/rIoRVWArhfx
Output:
2018-11-01 14:20:34 +0700 +0700
2018-11-01 14:20:34
Upvotes: 5