Skip to content
Parse strings into DateTime, NaiveDateTime, Date, or Time https://hexdocs.pm/date_time_parser
Elixir Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bench Fix warnings Jun 24, 2019
bin Release 0.1.3 Aug 2, 2019
config Remove BitBucket and JUnit Aug 2, 2019
lib Support Unix timestamps (#1) Aug 2, 2019
test
.credo.exs Generate Credo config Aug 2, 2019
.formatter.exs Mix new Jun 16, 2019
.gitignore Fix warnings Jun 24, 2019
.tool-versions
CHANGELOG.md Support Unix timestamps (#1) Aug 2, 2019
CODE_OF_CONDUCT.md Update documentation Jul 26, 2019
CONTRIBUTING.md Update documentation Jul 26, 2019
EXAMPLES.md Support Unix timestamps (#1) Aug 2, 2019
LICENSE.md Update documentation Jul 26, 2019
README.md
mix.exs
mix.lock

README.md

DateTimeParser

License: MIT Contributor Covenant

DateTimeParser is a tokenizer for strings that attempts to parse into a DateTime, NaiveDateTime if timezone is not determined, Date, or Time.

The biggest ambiguity between datetime formats is whether it's ymd (year month day), mdy (month day year), or dmy (day month year); this is resolved by checking if there are slashes or dashes. If slashes, then it will try dmy first. All other cases will use the international format ymd. Sometimes, if the conditions are right, it can even parse dmy with dashes if the month is a vocal month (eg, "Jan").

Documentation

Online Documentation

Examples

iex> DateTimeParser.parse_datetime("19 September 2018 08:15:22 AM")
{:ok, ~N[2018-09-19 08:15:22]}

iex> DateTimeParser.parse_datetime("2034-01-13")
{:ok, ~N[2034-01-13 00:00:00]}

iex> DateTimeParser.parse_date("2034-01-13")
{:ok, ~D[2034-01-13]}

iex> DateTimeParser.parse_date("01/01/2017")
{:ok, ~D[2017-01-01]}

iex> DateTimeParser.parse_datetime("1/1/18 3:24 PM")
{:ok, ~N[2018-01-01T15:24:00]}

iex> DateTimeParser.parse_datetime("1/1/18 3:24 PM", assume_utc: true)
{:ok, ~U[2018-01-01T15:24:00Z]}
# the ~U is a DateTime sigil introduced in Elixir 1.9.0

iex> DateTimeParser.parse_datetime(~s|"Dec 1, 2018 7:39:53 AM PST"|)
{:ok, ~U[2018-12-01T14:39:53Z]}
# Notice that the date is converted to UTC by default

iex> {:ok, datetime} = DateTimeParser.parse_datetime(~s|"Dec 1, 2018 7:39:53 AM PST"|, to_utc: false)
iex> datetime
#DateTime<2018-12-01 07:39:53-07:00 PDT PST8PDT>

iex> DateTimeParser.parse_time("10:13pm")
{:ok, ~T[22:13:00]}

iex> DateTimeParser.parse_time("10:13:34")
{:ok, ~T[10:13:34]}

iex> DateTimeParser.parse_datetime(nil)
{:error, "Could not parse nil"}

See more examples automatically generated by the tests

Installation

Add date_time_parser to your list of dependencies in mix.exs:

def deps do
  [
    {:date_time_parser, "~> 0.1.3"}
  ]
end

Changelog

View Changelog

Contributing

How to contribute

Special Thanks

You can’t perform that action at this time.