简体   繁体   中英

Elixir Ecto Phoenix - Comparing string date format “YYYY-MM-DD” with #DateTime<2017-12-01 13:31:14.987000Z>

I'm trying to implement a date range in my Ecto query where the date ranges are string dates of format "YYYY-MM-DD" , and the corresponding created_at date in the database is in #DateTime<2017-12-01 13:31:14.987000Z> format.

I have my range comparison query set up, my only problem is in figuring out how to cast my date strings into the same #DateTime format.

You can use DateTime.from_iso8601/1

date = "2015-01-23"
case DateTime.from_iso8601("#{date}T00:00:00Z") do
    {:error, error} -> error
    {:ok, dt} -> 
                 # .... do stuff 
end

If you have the need to manipulate Dates & Times, format, parse & etc, you might look into Timex as it covers a lot of usual things related to Dates and Time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM