Say I have a datetime object eg DateTime.now
. I want to set hours and minutes to 0 (midnight). How can I do that?
Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.
Thanks to ActiveSupport you can use:
DateTime.now.midnight DateTime.now.beginning_of_day
OR
DateTime.now.change({ hour: 0, min: 0, sec: 0 }) # More concisely DateTime.now.change({ hour: 0 })
now = DateTime.now DateTime.new(now.year, now.month, now.day, 0, 0, 0, now.zone)
OR
now = DateTime.now DateTime.parse(now.strftime("%Y-%m-%dT00:00:00%z"))
Nevermind, got it. Need to create a new DateTime:
DateTime.new(now.year, now.month, now.day, 0, 0, 0, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With