Reputation: 293
I am new to Elixir and wanted to know if there is a way to check if a string is a date without necessarily having to write your own function that makes use of regex?
Upvotes: 2
Views: 4968
Reputation: 15045
It depends on the format of the date in the parsed string. Elixir
's standard library contains Date.from_iso8601, which can be used as the following:
def is_date?(date) do
case Date.from_iso8601(date) do
{:ok, _} -> true
_ -> false
end
end
If you're expecting a string of another format, then Timex library may be useful for you, because it allows to specify the format of the parsed string. For example:
def is_date?(date) do
case Timex.format(date, "{ISO:Extended}") do
{:ok, _} -> true
_ -> false
end
end
Upvotes: 4
Reputation: 5812
I guess the most suitable option for you might be Timex.parse, but you could provide informations of your Date's format.
If you have a bit more complex format, I'm afraid that using your own "datetime helper" will be neccesary.
Upvotes: 1