Warning : DateTime.now.midnight and DateTime.now.beginning_of_day return the same value (this is the zero hour of the current day - midnight does not return at 24:00:00, as one would expect from his name).
So, I am adding this as additional information for those who can use the accepted answer to calculate the midnight x days in the future.
For example, a 14-day free trial, which should expire at midnight on the 14th day:
DateTime.now.midnight + 14.days
- This is the morning of the 14th day, which is equivalent to the 13-day test (x is the part of the day remaining until the end - if it is noon, then this is a 13.5-day trial version).
You really need to do this:
DateTime.now.midnight + 15.days
to get midnight on the 14th day.
For this reason, I always prefer to use beginning_of_day , since it is 00:00:00. Using midnight can be misleading / misunderstood.
rmcsharry Apr 13 '16 at 11:05 2016-04-13 11:05
source share