benonymus
How to accept date and datetime into the same field
Hey I would need to save these 2 types of dates into one field
"start_date": "2019-10-18",
"start_date": "2019-10-18T20:00:00+02"
as you can see one is a date and the other is a datetime.
what’s the best way to go about this?
maybe convert them into something else?
and then transform them back when i want to return them?
Most Liked
josevalim
Creator of Elixir
As others said, it is likely a bad idea, but assuming you have to handle such input for reasons beyond your control, you can always trying parsing as one and then fallback to the other. I am not sure which order is best but here is an example:
case Date.from_iso8601(maybe_datetime) do
{:ok, date} -> convert_date_to_datetime(...)
{:error, _} ->
case NaiveDateTime.from_iso(maybe_datetime) do
{:ok, naive} -> convert_naive_to_datetime(...)
{:error, _} -> ...
end
end
3
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
can someone please explain to me how Enum.reduce works with maps
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New







