Parsing a date string in the Golang

I tried to parse the date string "2014-09-12T11: 45: 26.371Z" in the Golang.

The code

layout := "2014-09-12T11:45:26.371Z" str := "2014-11-12T11:45:26.371Z" t, err := time.Parse(layout , str) 

analysis time "2014-11-12T11: 47: 39.489Z": month out of range

I got this error.

How to parse this date string?

+88
go
Sep 15 '14 at 9:47
source share
6 answers

Use the exact layout numbers described here and a nice blog post here .

So:

 layout := "2006-01-02T15:04:05.000Z" str := "2014-11-12T11:45:26.371Z" t, err := time.Parse(layout, str) if err != nil { fmt.Println(err) } fmt.Println(t) 

gives:

 >> 2014-11-12 11:45:26.371 +0000 UTC 

I know. The skill is overwhelming. Also caught me for the first time. Go just does not use the abstract syntax for the datetime components ( YYYY-MM-DD ), but these are exact numbers ( I think the time it was first go Nope, according to this . Does anyone know?).

+115
Sep 15 '14 at 9:55
source share
β€” -

The layout used is really " 2006-01-02T15:04:05.000Z " described in the RickyA answer . <br> This is not the "time of the first transition", but rather a mnemonic way to remember this layout.
See pkg / time :

Estimated time used in layouts:

 Mon Jan 2 15:04:05 MST 2006 

which is Unix time 1136239445 .
Since MST is GMT-0700, the start of the clock can be considered as

  01/02 03:04:05PM '06 -0700 

(1,2,3,4,5,6,7, if you remember that 1 per month, and 2 per day, which is not so easy for a European, like me, the date format was used for the day month)

As shown in " time.parse: why does the golan not correctly analyze time ? , this layout (using 1,2,3,4,5,6, 7) must be followed exactly.

+64
Sep 15 '14 at 10:22
source share

As an answer, but to save the input "2006-01-02T15:04:05.000Z" for the layout, you can use the RFC3339 package constant .

 str := "2014-11-12T11:45:26.371Z" t, err := time.Parse(time.RFC3339, str) if err != nil { fmt.Println(err) } fmt.Println(t) 

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

+35
May 28 '17 at 23:40
source share

It's pretty late for the party, and actually nothing is said about what has not yet been said in one form or another, mainly from the links above, but I wanted to give a TL summary; DR for those with less attention:

The date and time of the go format string is very important. It's like Go knows which field is what. They are usually 1-9 from left to right as follows:

  • January / January / January / January / 01 / _1 (etc.) per month
  • 02 / _2 for the day of the month
  • 15/03 / _3 / PM / P / pm / p for the hour and meridian (3 pm)
  • 04 / _4 minutes
  • 05 / _5 per second
  • 2006/06 year
  • -0700/07: 00 / MST for the time zone
  • .999999999 / .000000000, etc. In incomplete seconds (I think the difference is that trailing zeros are removed)
  • Monday / Monday is the day of the week (which actually was 01-02-2006),

Therefore, do not write β€œ01-05-15” as the date format if you do not want β€œMonth-Second-Hour”

(... again, this was basically the summary above.)

+9
Aug 19 '18 at 8:19
source share

I suggest using the constant time.RFC3339 from the time package. You can check other constants from the time package. https://golang.org/pkg/time/#pkg-constants

 package main import ( "fmt" "time" ) func main() { fmt.Println("Time parsing"); dateString := "2014-11-12T11:45:26.371Z" time1, err := time.Parse(time.RFC3339,dateString); if err!=nil { fmt.Println("Error while parsing date :", err); } fmt.Println(time1); } 
+8
Feb 18 '18 at 15:52
source share

It may be too late, but it is for people who may stumble upon this problem and may want to use an external package to parse a date string.

I tried to find the libraries and I found this:

https://github.com/araddon/dateparse

Example from README:

 package main import ( "flag" "fmt" "time" "github.com/apcera/termtables" "github.com/araddon/dateparse" ) var examples = []string{ "May 8, 2009 5:57:51 PM", "Mon Jan 2 15:04:05 2006", "Mon Jan 2 15:04:05 MST 2006", "Mon Jan 02 15:04:05 -0700 2006", "Monday, 02-Jan-06 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST", "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)", "Mon, 02 Jan 2006 15:04:05 -0700", "Thu, 4 Jan 2018 17:53:36 +0000", "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "12 Feb 2006, 19:17", "12 Feb 2006 19:17", "03 February 2013", "2013-Feb-03", // mm/dd/yy "3/31/2014", "03/31/2014", "08/21/71", "8/1/71", "4/8/2014 22:05", "04/08/2014 22:05", "4/8/14 22:05", "04/2/2014 03:00:51", "8/8/1965 12:00:00 AM", "8/8/1965 01:00:01 PM", "8/8/1965 01:00 PM", "8/8/1965 1:00 PM", "8/8/1965 12:00 AM", "4/02/2014 03:00:51", "03/19/2012 10:11:59", "03/19/2012 10:11:59.3186369", // yyyy/mm/dd "2014/3/31", "2014/03/31", "2014/4/8 22:05", "2014/04/08 22:05", "2014/04/2 03:00:51", "2014/4/02 03:00:51", "2012/03/19 10:11:59", "2012/03/19 10:11:59.3186369", // Chinese "2014εΉ΄04月08ζ—₯", // yyyy-mm-ddThh "2006-01-02T15:04:05+0000", "2009-08-12T22:15:09-07:00", "2009-08-12T22:15:09", "2009-08-12T22:15:09Z", // yyyy-mm-dd hh:mm:ss "2014-04-26 17:24:37.3186369", "2012-08-03 18:31:59.257000000", "2014-04-26 17:24:37.123", "2013-04-01 22:43", "2013-04-01 22:43:22", "2014-12-16 06:20:00 UTC", "2014-12-16 06:20:00 GMT", "2014-04-26 05:24:37 PM", "2014-04-26 13:13:43 +0800", "2014-04-26 13:13:44 +09:00", "2012-08-03 18:31:59.257000000 +0000 UTC", "2015-09-30 18:48:56.35272715 +0000 UTC", "2015-02-18 00:12:00 +0000 GMT", "2015-02-18 00:12:00 +0000 UTC", "2017-07-19 03:21:51+00:00", "2014-04-26", "2014-04", "2014", "2014-05-11 08:20:13,787", // mm.dd.yy "3.31.2014", "03.31.2014", "08.21.71", // yyyymmdd and similar "20140601", // unix seconds, ms "1332151919", "1384216367189", } var ( timezone = "" ) func main() { flag.StringVar(&timezone, "timezone", "UTC", "Timezone aka 'America/Los_Angeles' formatted time-zone") flag.Parse() if timezone != "" { // NOTE: This is very, very important to understand // time-parsing in go loc, err := time.LoadLocation(timezone) if err != nil { panic(err.Error()) } time.Local = loc } table := termtables.CreateTable() table.AddHeaders("Input", "Parsed, and Output as %v") for _, dateExample := range examples { t, err := dateparse.ParseLocal(dateExample) if err != nil { panic(err.Error()) } table.AddRow(dateExample, fmt.Sprintf("%v", t)) } fmt.Println(table.Render()) } 
+4
Aug 11 '17 at 3:40 on
source share



All Articles