Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add days to Ecto.DateTime?

I have a date-time which I create like this:

Ecto.DateTime.from_erl({{2015, 3, 10}, {0, 0, 0}})

It's a Phoenix app. I want to add days to it with no any additional third-party library. How?

like image 723
Alan Coromano Avatar asked Jul 05 '16 07:07

Alan Coromano


2 Answers

You can use erlang's :calendar module to manipulate dates without additional dependencies.

A standard way of adding days would be to use :calendar.date_to_gregorian_days/1 do the addition and convert back to the tuple format with :calendar.gregorian_days_to_date/1.

like image 183
michalmuskala Avatar answered Sep 20 '22 14:09

michalmuskala


As of at least Elixir 1.5.0, you can use DateTime.add/2 to add days to a date.

# add five days to the current day
DateTime.utc_now |> DateTime.add(5*24*60*60, :second)
like image 35
Ian Hunter Avatar answered Sep 18 '22 14:09

Ian Hunter