1Go Date Parser
2---------------------------
3
4Parse many date strings without knowing format in advance. Uses a scanner to read bytes and use a state machine to find format. Much faster than shotgun based parse methods. See [bench_test.go](https://github.com/araddon/dateparse/blob/master/bench_test.go) for performance comparison.
5
6
7[](https://codecov.io/gh/araddon/dateparse)
8[](http://godoc.org/github.com/araddon/dateparse)
9[](https://travis-ci.org/araddon/dateparse)
10[](https://goreportcard.com/report/araddon/dateparse)
11
12**MM/DD/YYYY VS DD/MM/YYYY** Right now this uses mm/dd/yyyy WHEN ambiguous if this is not desired behavior, use `ParseStrict` which will fail on ambiguous date strings.
13
14**Timezones** The location your server is configured affects the results! See example or https://play.golang.org/p/IDHRalIyXh and last paragraph here https://golang.org/pkg/time/#Parse.
15
16
17```go
18
19// Normal parse. Equivalent Timezone rules as time.Parse()
20t, err := dateparse.ParseAny("3/1/2014")
21
22// Parse Strict, error on ambigous mm/dd vs dd/mm dates
23t, err := dateparse.ParseStrict("3/1/2014")
24> returns error
25
26// Return a string that represents the layout to parse the given date-time.
27layout, err := dateparse.ParseFormat("May 8, 2009 5:57:51 PM")
28> "Jan 2, 2006 3:04:05 PM"
29
30```
31
32cli tool for testing dateformats
33----------------------------------
34
35[Date Parse CLI](https://github.com/araddon/dateparse/blob/master/dateparse)
36
37
38Extended example
39-------------------
40
41https://github.com/araddon/dateparse/blob/master/example/main.go
42
43```go
44package main
45
46import (
47 "flag"
48 "fmt"
49 "time"
50
51 "github.com/apcera/termtables"
52 "github.com/araddon/dateparse"
53)
54
55var examples = []string{
56 "May 8, 2009 5:57:51 PM",
57 "oct 7, 1970",
58 "oct 7, '70",
59 "oct. 7, 1970",
60 "oct. 7, 70",
61 "Mon Jan 2 15:04:05 2006",
62 "Mon Jan 2 15:04:05 MST 2006",
63 "Mon Jan 02 15:04:05 -0700 2006",
64 "Monday, 02-Jan-06 15:04:05 MST",
65 "Mon, 02 Jan 2006 15:04:05 MST",
66 "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
67 "Mon, 02 Jan 2006 15:04:05 -0700",
68 "Thu, 4 Jan 2018 17:53:36 +0000",
69 "Mon Aug 10 15:44:11 UTC+0100 2015",
70 "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
71 "September 17, 2012 10:09am",
72 "September 17, 2012 at 10:09am PST-08",
73 "September 17, 2012, 10:10:09",
74 "October 7, 1970",
75 "October 7th, 1970",
76 "12 Feb 2006, 19:17",
77 "12 Feb 2006 19:17",
78 "7 oct 70",
79 "7 oct 1970",
80 "03 February 2013",
81 "1 July 2013",
82 "2013-Feb-03",
83 // mm/dd/yy
84 "3/31/2014",
85 "03/31/2014",
86 "08/21/71",
87 "8/1/71",
88 "4/8/2014 22:05",
89 "04/08/2014 22:05",
90 "4/8/14 22:05",
91 "04/2/2014 03:00:51",
92 "8/8/1965 12:00:00 AM",
93 "8/8/1965 01:00:01 PM",
94 "8/8/1965 01:00 PM",
95 "8/8/1965 1:00 PM",
96 "8/8/1965 12:00 AM",
97 "4/02/2014 03:00:51",
98 "03/19/2012 10:11:59",
99 "03/19/2012 10:11:59.3186369",
100 // yyyy/mm/dd
101 "2014/3/31",
102 "2014/03/31",
103 "2014/4/8 22:05",
104 "2014/04/08 22:05",
105 "2014/04/2 03:00:51",
106 "2014/4/02 03:00:51",
107 "2012/03/19 10:11:59",
108 "2012/03/19 10:11:59.3186369",
109 // Chinese
110 "2014年04月08日",
111 // yyyy-mm-ddThh
112 "2006-01-02T15:04:05+0000",
113 "2009-08-12T22:15:09-07:00",
114 "2009-08-12T22:15:09",
115 "2009-08-12T22:15:09Z",
116 // yyyy-mm-dd hh:mm:ss
117 "2014-04-26 17:24:37.3186369",
118 "2012-08-03 18:31:59.257000000",
119 "2014-04-26 17:24:37.123",
120 "2013-04-01 22:43",
121 "2013-04-01 22:43:22",
122 "2014-12-16 06:20:00 UTC",
123 "2014-12-16 06:20:00 GMT",
124 "2014-04-26 05:24:37 PM",
125 "2014-04-26 13:13:43 +0800",
126 "2014-04-26 13:13:43 +0800 +08",
127 "2014-04-26 13:13:44 +09:00",
128 "2012-08-03 18:31:59.257000000 +0000 UTC",
129 "2015-09-30 18:48:56.35272715 +0000 UTC",
130 "2015-02-18 00:12:00 +0000 GMT",
131 "2015-02-18 00:12:00 +0000 UTC",
132 "2015-02-08 03:02:00 +0300 MSK m=+0.000000001",
133 "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001",
134 "2017-07-19 03:21:51+00:00",
135 "2014-04-26",
136 "2014-04",
137 "2014",
138 "2014-05-11 08:20:13,787",
139 // mm.dd.yy
140 "3.31.2014",
141 "03.31.2014",
142 "08.21.71",
143 "2014.03",
144 "2014.03.30",
145 // yyyymmdd and similar
146 "20140601",
147 "20140722105203",
148 // unix seconds, ms, micro, nano
149 "1332151919",
150 "1384216367189",
151 "1384216367111222",
152 "1384216367111222333",
153}
154
155var (
156 timezone = ""
157)
158
159func main() {
160 flag.StringVar(&timezone, "timezone", "UTC", "Timezone aka `America/Los_Angeles` formatted time-zone")
161 flag.Parse()
162
163 if timezone != "" {
164 // NOTE: This is very, very important to understand
165 // time-parsing in go
166 loc, err := time.LoadLocation(timezone)
167 if err != nil {
168 panic(err.Error())
169 }
170 time.Local = loc
171 }
172
173 table := termtables.CreateTable()
174
175 table.AddHeaders("Input", "Parsed, and Output as %v")
176 for _, dateExample := range examples {
177 t, err := dateparse.ParseLocal(dateExample)
178 if err != nil {
179 panic(err.Error())
180 }
181 table.AddRow(dateExample, fmt.Sprintf("%v", t))
182 }
183 fmt.Println(table.Render())
184}
185
186/*
187+-------------------------------------------------------+-----------------------------------------+
188| Input | Parsed, and Output as %v |
189+-------------------------------------------------------+-----------------------------------------+
190| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
191| oct 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
192| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC |
193| oct. 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
194| oct. 7, 70 | 1970-10-07 00:00:00 +0000 UTC |
195| Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC |
196| Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST |
197| Mon Jan 02 15:04:05 -0700 2006 | 2006-01-02 15:04:05 -0700 -0700 |
198| Monday, 02-Jan-06 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
199| Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
200| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 |
201| Mon, 02 Jan 2006 15:04:05 -0700 | 2006-01-02 15:04:05 -0700 -0700 |
202| Thu, 4 Jan 2018 17:53:36 +0000 | 2018-01-04 17:53:36 +0000 UTC |
203| Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC |
204| Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT |
205| September 17, 2012 10:09am | 2012-09-17 10:09:00 +0000 UTC |
206| September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST |
207| September 17, 2012, 10:10:09 | 2012-09-17 10:10:09 +0000 UTC |
208| October 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
209| October 7th, 1970 | 1970-10-07 00:00:00 +0000 UTC |
210| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
211| 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC |
212| 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC |
213| 7 oct 1970 | 1970-10-07 00:00:00 +0000 UTC |
214| 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC |
215| 1 July 2013 | 2013-07-01 00:00:00 +0000 UTC |
216| 2013-Feb-03 | 2013-02-03 00:00:00 +0000 UTC |
217| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
218| 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
219| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |
220| 8/1/71 | 1971-08-01 00:00:00 +0000 UTC |
221| 4/8/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
222| 04/08/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
223| 4/8/14 22:05 | 2014-04-08 22:05:00 +0000 UTC |
224| 04/2/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
225| 8/8/1965 12:00:00 AM | 1965-08-08 00:00:00 +0000 UTC |
226| 8/8/1965 01:00:01 PM | 1965-08-08 13:00:01 +0000 UTC |
227| 8/8/1965 01:00 PM | 1965-08-08 13:00:00 +0000 UTC |
228| 8/8/1965 1:00 PM | 1965-08-08 13:00:00 +0000 UTC |
229| 8/8/1965 12:00 AM | 1965-08-08 00:00:00 +0000 UTC |
230| 4/02/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
231| 03/19/2012 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
232| 03/19/2012 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
233| 2014/3/31 | 2014-03-31 00:00:00 +0000 UTC |
234| 2014/03/31 | 2014-03-31 00:00:00 +0000 UTC |
235| 2014/4/8 22:05 | 2014-04-08 22:05:00 +0000 UTC |
236| 2014/04/08 22:05 | 2014-04-08 22:05:00 +0000 UTC |
237| 2014/04/2 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
238| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
239| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
240| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
241| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
242| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
243| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
244| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
245| 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC |
246| 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC |
247| 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC |
248| 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC |
249| 2013-04-01 22:43 | 2013-04-01 22:43:00 +0000 UTC |
250| 2013-04-01 22:43:22 | 2013-04-01 22:43:22 +0000 UTC |
251| 2014-12-16 06:20:00 UTC | 2014-12-16 06:20:00 +0000 UTC |
252| 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC |
253| 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC |
254| 2014-04-26 13:13:43 +0800 | 2014-04-26 13:13:43 +0800 +0800 |
255| 2014-04-26 13:13:43 +0800 +08 | 2014-04-26 13:13:43 +0800 +0800 |
256| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0900 +0900 |
257| 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 UTC |
258| 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 UTC |
259| 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 UTC |
260| 2015-02-18 00:12:00 +0000 UTC | 2015-02-18 00:12:00 +0000 UTC |
261| 2015-02-08 03:02:00 +0300 MSK m=+0.000000001 | 2015-02-08 03:02:00 +0300 +0300 |
262| 2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001 | 2015-02-08 03:02:00.001 +0300 +0300 |
263| 2017-07-19 03:21:51+00:00 | 2017-07-19 03:21:51 +0000 UTC |
264| 2014-04-26 | 2014-04-26 00:00:00 +0000 UTC |
265| 2014-04 | 2014-04-01 00:00:00 +0000 UTC |
266| 2014 | 2014-01-01 00:00:00 +0000 UTC |
267| 2014-05-11 08:20:13,787 | 2014-05-11 08:20:13.787 +0000 UTC |
268| 3.31.2014 | 2014-03-31 00:00:00 +0000 UTC |
269| 03.31.2014 | 2014-03-31 00:00:00 +0000 UTC |
270| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
271| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
272| 2014.03.30 | 2014-03-30 00:00:00 +0000 UTC |
273| 20140601 | 2014-06-01 00:00:00 +0000 UTC |
274| 20140722105203 | 2014-07-22 10:52:03 +0000 UTC |
275| 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
276| 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC |
277| 1384216367111222 | 2013-11-12 00:32:47.111222 +0000 UTC |
278| 1384216367111222333 | 2013-11-12 00:32:47.111222333 +0000 UTC |
279+-------------------------------------------------------+-----------------------------------------+
280*/
281
282```