Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a string of format "YYYYMMDD" to a Timex.Parse.DateTime type?

I want to use Paul Schoenfelder's awesome Timex library to parse a simple string of format "YYYYMMDD" into a Timex.Parse.DateTime.t type.

Right now, I'm doing something like this:

{:ok, dt} = Timex.parse "20161111", "{YYYYDDMM}"
** (MatchError) no match of right hand side value: {:error, {:format, "Expected at least one parser to succeed at line 1, column 0."}}

This code above, doesn't work. But when I do this:

{:ok, dt} = Timex.parse "2016", "{YYYY}"        
{:ok, #<DateTime(2016-01-01T00:00:00Z)>}

I get dt assigned as a DateTime variable.

What am I doing wrong?

Thanks in advance :D

like image 290
cesarvargas Avatar asked Jan 07 '23 08:01

cesarvargas


1 Answers

  • It looks like the DD and MM are not valid parsers. You're probably looking for 0D and 0M (more date/month parsers available here).
  • You need to wrap each component in their own {}

Running on the latest git version of Timex:

iex> Timex.parse "20161111", "{YYYY}{0D}{0M}"
{:ok, #<DateTime(2016-11-11T00:00:00Z)>}
like image 187
Dogbert Avatar answered Jan 16 '23 04:01

Dogbert