View Source DateTimeParser Playground

Installation

Install DateTimeParser in your project

Mix.install([:date_time_parser])

Usage

Use DateTimeParser to parse strings into DateTime, NaiveDateTime, Date, or Time structs. For example:

[
  DateTimeParser.parse_datetime("2021-11-09T10:30:00Z"),
  DateTimeParser.parse_datetime("2021-11-09T10:30:00"),
  DateTimeParser.parse_date("2021-11-09T10:30:00Z"),
  DateTimeParser.parse_time("2021-11-09T10:30:00Z"),
  DateTimeParser.parse("2021-32-32T10:30:00Z"),
  DateTimeParser.parse("2021-11-09T10:30:00Z")
]

or use the bang functions:

[
  DateTimeParser.parse_datetime!("2021-11-09T10:30:00Z"),
  DateTimeParser.parse_datetime!("2021-11-09T10:30:00"),
  DateTimeParser.parse_date!("2021-11-09T10:30:00Z"),
  DateTimeParser.parse_time!("2021-11-09T10:30:00Z"),
  DateTimeParser.parse!("2021-32-32T10:30:00Z"),
  DateTimeParser.parse!("2021-11-09T10:30:00Z")
]

Errors sometimes occur when it can't parse the input:

[
  DateTimeParser.parse("wat"),
  DateTimeParser.parse(123),
  DateTimeParser.parse([:foo])
]

Options

You can configure some convenient options as well, for example to automatically convert to UTC or to assume a time when not present.

[
  DateTimeParser.parse("12:30PM", assume_date: Date.utc_today()),
  DateTimeParser.parse("2022-01-01", assume_time: ~T[12:43:00]),
  DateTimeParser.parse("2022-01-01T15:30 EST", to_utc: true),
  DateTimeParser.parse("2022-01-01T15:30 EST", to_utc: false),
  # Excel time
  DateTimeParser.parse("30134"),
  # old Mac Excel spreadsheet time
  DateTimeParser.parse("30134.4321", use_1904_date_system: true)
]

Examples

InputOutput (ISO 8601)MethodOptions
01 Feb 20132013-02-01parse
03 Jan 2013 10:15:26 -08002013-01-03T18:15:26Zparse[to_utc: true]
10/1/2018 :: AM2018-10-01parse
11 Feb 20132013-02-11parse
11 Jan 2013 13:26:55 -08002013-01-11T21:26:55Zparse[to_utc: true]
12/26/20162016-12-26parse
24 Sep 20132013-09-24parse
""=""9/5/2018"""2018-09-05parse_date
""=""9/5/2018"""2018-09-05T00:00:00parse_datetime[assume_time: true]
"=""10/1/2018"""2018-10-01parse
"=""9/5/2018"""2018-09-05parse
"Apr 1, 2016 12:02:53 AM PDT"2016-04-01T07:02:53Zparse[to_utc: true]
"Apr 1, 2017 2:21:25 AM PDT"2017-04-01T09:21:25Zparse[to_utc: true]
"Dec 1, 2018 7:39:53 AM PST"2018-12-01T15:39:53Zparse[to_utc: true]
"Jan 1, 2013 06:34:31 PM PST"2013-01-02T02:34:31Zparse[to_utc: true]
"Jan 1, 2014 6:44:47 AM PST"2014-01-01T14:44:47Zparse[to_utc: true]
"Mar 28, 2014 6:44:47 AM PDT"2014-03-28T13:44:47Zparse[to_utc: true]
"Nov 16, 2017 9:41:28 PM PST"2017-11-17T05:41:28Zparse[to_utc: true]
"Nov 20, 2016 22:09:23 PM"2016-11-20T22:09:23parse
"Sat, 29 Sep 2018 21:36:28 -0400"2018-09-30T01:36:28Zparse[to_utc: true]
"September 28, 2016"2016-09-28parse
"Tuesday, November 29, 2016"2016-11-29parse
-00000000011969-12-31parse_date
-00000000011969-12-31T23:59:59Zparse_datetime
-0000000001.000000000123:59:58.000000parse_time
-0000000001.00000000011969-12-31T23:59:58.000000Zparse_datetime
-0000000001.00000123:59:58.999999parse_time
-0000000001.0000123:59:58.99999parse_time
-0000000001.000123:59:58.9999parse_time
-0000000001.0011969-12-31parse_date
-0000000001.00123:59:58.999parse_time
-0000000001.0123:59:58.99parse_time
-0000000001.123:59:58.9parse_time
-0000000001.1111111969-12-31parse_date
-03863808001957-10-04T00:00:00Zparse_datetime
-3631899-01-01parse_date
-363.01899-01-01T00:00:00parse_datetime
-45103.14543981481776-07-04parse_date
-45103.145439814820:30:34parse_time
-45103.14543981481776-07-04T20:30:34parse_datetime
-99999999991653-02-10T06:13:21Zparse_datetime
-9999999999.0091653-02-10parse_date
-9999999999.906:13:20.1parse_time
-9999999999.9906:13:20.01parse_time
-9999999999.9991653-02-10parse_date
-9999999999.99906:13:20.001parse_time
-9999999999.999906:13:20.0001parse_time
-9999999999.9999906:13:20.00001parse_time
-9999999999.9999991653-02-10parse_date
-9999999999.99999906:13:20.000001parse_time
-9999999999.99999999991653-02-10parse_date
-9999999999.999999999906:13:20.000001parse_time
-9999999999.99999999991653-02-10T06:13:20.000001Zparse_datetime
-99999999999-1199-02-15T14:13:21Zparse_datetime
00000000001970-01-01parse_date
000000000000:00:00parse_time
00000000001970-01-01T00:00:00Zparse_datetime
00:00.000:00:00parse_time
01-01-20182018-01-01parse
01-Feb-182018-02-01parse
01-Jul2019-07-01parse[assume_date: ~D[2019-01-05]]
01-JulCould not parse "01-Jul"parse
01-JulCould not parse "01-Jul"parse_datetime
01-Jul-182018-07-01parse
01.09.20182018-09-01parse
01.11.20182018-11-01parse
01/01/172017-01-01parse
01/01/20172017-01-01parse
01/01/2018 - 17:062018-01-01T17:06:00parse
01/01/2018 01:21PM2018-01-01T13:21:00parse
01/01/2018 14:442018-01-01T14:44:00parse
01/01/2018 6:222018-01-01T06:22:00parse
01/02/162016-01-02parse
01/02/18 01:02 AM2018-01-02T01:02:00parse
01/02/20152015-01-02parse
01/09/20342034-01-09parse_date
01/09/20342034-01-09T00:00:00parse_datetime[assume_time: true]
01/Jun./20182018-06-01parse
02-05-20182018-05-02parse
02-Oct-172017-10-02parse
02/01/172017-02-01parse
02/01/20182018-02-01parse
02/06/20192019-02-06parse_date
02/06/20192019-02-06T00:00:00parse_datetime[assume_time: true]
02/21/2018 9:37:42 AM2018-02-21T09:37:42parse
03/5/20182018-03-05parse
05/01/2018 0:002018-05-01T00:00:00parse
06/14/2018 09:42:08 PM-05002018-06-15T02:42:08Zparse[to_utc: true]
06/28/18 1:252018-06-28T01:25:00parse
07:09.307:09:00parse_time
08:53.008:53:00parse_time
1-Apr2019-04-01parse[assume_date: ~D[2019-01-13]]
1-AprCould not parse "1-Apr"parse
1//1/172017-01-01parse
1/1/01170117-01-01parse
1/1/17 19:122017-01-01T19:12:00parse
1/1/18 00:012018-01-01T00:01:00parse
1/1/18 3:24 PM2018-01-01T15:24:00parse
1/1/19 10:39 AM2019-01-01T10:39:00parse
1/1/20132013-01-01parse
1/10/2018 8:38pM2018-01-10T20:38:00parse
1/13/192019-01-13parse_date
1/13/192019-01-13T00:00:00parse_datetime[assume_time: true]
1/13/20192019-01-13parse_date
1/13/20192019-01-13T00:00:00parse_datetime[assume_time: true]
1/15/2019 3:062019-01-15parse_date
1/15/2019 3:062019-01-15T03:06:00parse_datetime
1/17/2018 0:00:002018-01-17T00:00:00parse
1/2/2018 18:06:262018-01-02T18:06:26parse
1/3/2019 12:00:00 AM2019-01-03T00:00:00parse
1/31/2018 0:00:00 UTC2018-01-31T00:00:00Zparse
1/9/20342034-01-09parse_date
1/9/20342034-01-09T00:00:00parse_datetime[assume_time: true]
1/9/342034-01-09parse_date
1/9/342034-01-09T00:00:00parse_datetime[assume_time: true]
10/2/2017 - 23:142017-10-02T23:14:00parse
10/5/2017 23:522017-10-05T23:52:00parse
10:13.710:13:00parse_time
11 July 2017 1:43:46 PM2017-07-11parse_date
11 July 2017 1:43:46 PM2017-07-11T13:43:46parse_datetime
12/52021-12-05parse_date[assume_date: ~D[2021-01-01]]
12:30PM12:30:00parse_time[assume_date: ~D[2020-01-01]]
12:30PM2020-01-01T12:30:00parse[assume_date: ~D[2020-01-01]]
13/52021-05-13parse_date[assume_date: ~D[2021-01-01]]
18-07-2018 20:38:34 +00:002018-07-18T20:38:34Zparse
18-12-292018-12-29parse
19 September 18 2:33:08 PM2018-09-19parse_date
19 September 18 2:33:08 PM2018-09-19T14:33:08parse_datetime
19 September 2018 08:15:22 AM2018-09-19parse_date
19 September 2018 08:15:22 AM2018-09-19T08:15:22parse_datetime
19-Dec-192019-12-19parse
21900-01-01parse_date
2.01900-01-01T00:00:00parse_datetime
2/52021-02-05parse_date[assume_date: ~D[2021-01-01]]
2010/01/012010-01-01parse
2011-01-01 04:19:20 -0:002011-01-01T04:19:20Zparse
2012-10-30 09:52:002012-10-30T09:52:00parse
2012-11-23T22:42:25-05:002012-11-24T03:42:25Zparse[to_utc: true]
2013-04-26 11:25:03 UTC2013-04-26T11:25:03Zparse
2013-09-10 22:14:56.7172013-09-10T22:14:56.717parse
2013-12-31T22:18:50+00:002013-12-31T22:18:50Zparse
2015-09-28 10:57:11 -07002015-09-28T17:57:11Zparse[to_utc: true]
2015/12/1 1:162015-12-01T01:16:00parse
2016-02-292016-02-29parse_date
2016-02-29 00:00:00 UTC2016-02-29T00:00:00Zparse_datetime
2016-04-302016-04-30parse
2016-05-02T01:10:06+00:002016-05-02T01:10:06Zparse
2016-06-11 15:50:432016-06-11T15:50:43parse
2016-06-16 06:06:062016-06-16T06:06:06parse
2016-07-01 01:51:34+002016-07-01T01:51:34Zparse
2016-07-31 18:42:46-07:002016-08-01T01:42:46Zparse[to_utc: true]
2016-08-04T07:00:25Z2016-08-04T07:00:25Zparse
2016-08-19 09:34:51.02016-08-19T09:34:51.0parse
2016-11-17 10:36:34.812016-11-17T10:36:34.81parse
2016-11-23T16:25:33.9718972016-11-23T16:25:33.971897parse
2016/1/92016-01-09parse
2017-02-29Could not parse "2017-02-29"parse_date
2017-02-29 00:00:00 UTCCould not parse "2017-02-29 00:00:00 UTC"parse_datetime
2017-03-04 15:20:47 EDT2017-03-04T20:20:47Zparse_datetime[to_utc: true]
2017-03-04 15:20:47 EST2017-03-04T20:20:47Zparse_datetime[to_utc: true]
2017-04-31Could not parse "2017-04-31"parse_date
2017-04-31 00:00:00 UTCCould not parse "2017-04-31 00:00:00 UTC"parse_datetime
2017-06-31Could not parse "2017-06-31"parse_date
2017-06-31 00:00:00 UTCCould not parse "2017-06-31 00:00:00 UTC"parse_datetime
2017-09-29+00:002017-09-29T00:00:00parse
2017-09-31Could not parse "2017-09-31"parse_date
2017-09-31 00:00:00 UTCCould not parse "2017-09-31 00:00:00 UTC"parse_datetime
2017-10-06+03:45:162017-10-06T03:45:16parse
2017-10-24 04:00:10 PDT2017-10-24T11:00:10Zparse[to_utc: true]
2017-11-04 15:20:47 EDT2017-11-04parse_date
2017-11-04 15:20:47 EDT2017-11-04T19:20:47Zparse_datetime[to_utc: true]
2017-11-04 15:20:47 EST2017-11-04parse_date
2017-11-04 15:20:47 EST2017-11-04T19:20:47Zparse_datetime[to_utc: true]
2017-11-04 15:20:47 UTC2017-11-04parse_date
2017-11-04 15:20:47 UTC2017-11-04T15:20:47Zparse_datetime
2017-11-04 15:20:47+00002017-11-04parse_date
2017-11-04 15:20:47+00002017-11-04T15:20:47Zparse_datetime
2017-11-04 15:20:47+05002017-11-04parse_date
2017-11-04 15:20:47+05002017-11-04T10:20:47Zparse_datetime[to_utc: true]
2017-11-04 15:20:47-05002017-11-04parse_date
2017-11-04 15:20:47-05002017-11-04T20:20:47Zparse_datetime[to_utc: true]
2017-11-31Could not parse "2017-11-31"parse_date
2017-11-31 00:00:00 UTCCould not parse "2017-11-31 00:00:00 UTC"parse_datetime
2017-12-01 03:522017-12-01T03:52:00parse
2017/08/082017-08-08parse
2019-05-16+04:002019-05-16parse_date
2019-05-16+04:002019-05-16T04:00:00parse_datetime[assume_time: true]
2019-05-20 10:00:00PST2019-05-20parse_date
2019-05-20 10:00:00PST2019-05-20T17:00:00Zparse_datetime[to_utc: true]
2019/01/31 0:012019-01-31T00:01:00parse
2021-03-27 00:00 am2021-03-27T00:00:00parse_datetime
2021-03-27 00:00 pm2021-03-27T00:00:00parse_datetime
2021-03-27 12:00 am2021-03-27T00:00:00parse_datetime
2021-03-27 12:00 pm2021-03-27T12:00:00parse_datetime
2034-01-132034-01-13parse_date
2034-01-132034-01-13T00:00:00parse_datetime[assume_time: true]
2034-1-92034-01-09parse_date
2034-1-92034-01-09T00:00:00parse_datetime[assume_time: true]
203401092034-01-09T00:00:00parse_datetime[assume_time: true]
23-05-2019 @ 10:012019-05-23parse_date
23-05-2019 @ 10:012019-05-23T10:01:00parse_datetime[assume_time: true]
24:00Could not parse "24:00"parse_time
29/Aug./20182018-08-29parse
29/Sep./20182018-09-29parse
301341982-07-02parse
30134.01982-07-02T00:00:00parse
34-1-132034-01-13parse_date
34-1-132034-01-13T00:00:00parse_datetime[assume_time: true]
4/24/2019 0:00:002019-04-24parse_date
4/24/2019 0:00:002019-04-24T00:00:00parse_datetime
41261.60138888892012-12-18parse_date
41261.601388888914:26:00parse_time
41261.60138888892012-12-18T14:26:00parse_datetime
5/12/2019 12:21:58 PM2019-05-12T12:21:58parse
5/2/2019 0:00:002019-05-02parse_date
5/2/2019 0:00:002019-05-02T00:00:00parse_datetime
5/2/2019 12:00:00 AM2019-05-02parse_date
5/2/2019 12:00:00 AM2019-05-02T00:00:00parse_datetime
5/31/2019 12:00:00 AM2019-05-31parse_date
5/31/2019 12:00:00 AM2019-05-31T00:00:00parse_datetime
621900-03-02parse_date
621900-03-02T00:00:00parse[assume_time: true]
621904-03-03T00:00:00parse[assume_time: true, use_1904_date_system: true]
62.01900-03-02T00:00:00parse
62.01900-03-02T00:00:00parse_datetime
62.01904-03-03T00:00:00parse[use_1904_date_system: true]
62.01904-03-03T00:00:00parse_datetime[use_1904_date_system: true]
9-2-322032-02-09parse_date
9-2-322032-02-09T00:00:00parse_datetime[assume_time: true]
9-Feb-182018-02-09parse_date
9-Feb-182018-02-09T00:00:00parse_datetime[assume_time: true]
9/1/2018 10:262018-09-01parse_date
9/1/2018 10:262018-09-01T10:26:00parse_datetime[assume_time: true]
9/10/2018 11:08:13 AM2018-09-10T11:08:13parse
9/19/2018 20:382018-09-19T20:38:00parse
9/20/2017 18:57:24 UTC2017-09-20T18:57:24Zparse
9/4/2018 0:002018-09-04parse_date
9/4/2018 0:002018-09-04T00:00:00parse_datetime
99999999992286-11-20parse_date
999999999917:46:39parse_time
99999999992286-11-20T17:46:39Zparse_datetime
9999999999.00000000092286-11-20T17:46:39.000000Zparse_datetime
9999999999.00000090002286-11-20T17:46:39.000000Zparse_datetime
9999999999.00000117:46:39.000001parse_time
9999999999.00001017:46:39.000010parse_time
9999999999.00010017:46:39.000100parse_time
9999999999.00100017:46:39.001000parse_time
9999999999.0092286-11-20parse_date
9999999999.00917:46:39.009parse_time
9999999999.0092286-11-20T17:46:39.009Zparse_datetime
9999999999.01000017:46:39.010000parse_time
9999999999.0902286-11-20T17:46:39.090Zparse_datetime
9999999999.10000017:46:39.100000parse_time
9999999999.90017:46:39.900parse_time
9999999999.9002286-11-20T17:46:39.900Zparse_datetime
9999999999.9992286-11-20parse_date
9999999999.99917:46:39.999parse_time
9999999999.9992286-11-20T17:46:39.999Zparse_datetime
9999999999.9999992286-11-20parse_date
9999999999.99999917:46:39.999999parse_time
9999999999.9999992286-11-20T17:46:39.999999Zparse_datetime
9999999999.99999999992286-11-20parse_date
9999999999.999999999917:46:39.999999parse_time
9999999999.99999999992286-11-20T17:46:39.999999Zparse_datetime
999999999995138-11-16parse_date
9999999999909:46:39parse_time
999999999995138-11-16T09:46:39Zparse_datetime
Fri Mar 2 09:01:57 20182018-03-02T09:01:57parse
Fri Mar 31 2017 21:41:40 GMT+0000 (UTC)2017-03-31T21:41:40Zparse
Friday 02 February 2018 10:42:21 AM2018-02-02T10:42:21parse
Jan 20202020-01-01parse_date[assume_date: ~D[0001-12-01]]
Jan-01-192019-01-01parse
Jan-01-192019-01-01T00:00:00parse[assume_time: true]
Jan-01-192019-01-01T10:13:15parse[assume_time: ~T[10:13:15]]
Jan-01-20182018-01-01parse
May 14421442-05-01parse_date[assume_date: ~D[0001-12-01]]
May 30, 2019 4:31:09 AM PDT2019-05-30parse_date
May 30, 2019 4:31:09 AM PDT2019-05-30T11:31:09Zparse_datetime[to_utc: true]
Monday 01 October 2018 06:34:19 AM2018-10-01T06:34:19parse
Monday 02 October 2017 9:04:49 AM2017-10-02T09:04:49parse
November 29, 20162016-11-29parse_date
November 29, 20162016-11-29T00:00:00parse_datetime[assume_time: true]
Oct 5, 2018 6:16:56 PM PDT2018-10-05parse_date
Oct 5, 2018 6:16:56 PM PDT2018-10-06T01:16:56Zparse_datetime[to_utc: true]
October 19951995-10-01parse_date[assume_date: ~D[0001-12-01]]
Sep-19-162016-09-19parse_date
Sep-19-162016-09-19T00:00:00parse_datetime[assume_time: true]
Sun 01 January 2017 10:11:02 PM2017-01-01parse_date
Sun 01 January 2017 10:11:02 PM2017-01-01T22:11:02parse_datetime
Sun Jan 08 2017 04:28:42 GMT+0000 (UTC)2017-01-08T04:28:42Zparse
Sun Jul 1 00:31:18 20182018-07-01T00:31:18parse
Sun, 01 January 2017 10:11:02 PM2017-01-01parse_date
Sun, 01 January 2017 10:11:02 PM2017-01-01T22:11:02parse_datetime
Sunday 01 January 2017 09:22:46 AM2017-01-01T09:22:46parse
Sunday 01 January 2017 10:11:02 PM2017-01-01parse_date
Sunday 01 January 2017 10:11:02 PM2017-01-01T22:11:02parse_datetime
Sunday 01 January 2017 10:11:02 PM2017-01-01T22:11:02parse
Sunday, 01 January 2017 10:11:02 PM2017-01-01parse_date
Sunday, 01 January 2017 10:11:02 PM2017-01-01T22:11:02parse_datetime
Thu Aug 09 2018 17:13:43 GMT+0000 (UTC)2018-08-09T17:13:43Zparse
Thu Feb 08 00:24:33 20182018-02-08T00:24:33parse
Thu Jul 5 12:19:56 20182018-07-05T12:19:56parse
Thursday 30 August 2018 11:31:18 AM2018-08-30T11:31:18parse
Tue Jul 31 06:44:39 20182018-07-31T06:44:39parse
Tuesday 11 July 2017 1:43:46 PM2017-07-11T13:43:46parse
jul-10-182018-07-10parse