just curious - is there a shorter way to achieve this?
latest_date = date1 > date2 ? date1 : date2
thanks
You can just subtract 86400 from a Time object to get one day before. If you are using Rails, or have ActiveSupport included, you can replace 86400 with 1. days . If you're using a Date object, and not a Time object, just subtract 1 from it.
new(y, m, d) , you can create a new Date object. The values for day (d) and month (m) can be negative in which case they count backwards from the end of the year and the end of the month respectively.
The proper way is to do a Time. now. getutc. to_i to get the proper timestamp amount as simply displaying the integer need not always be same as the utc timestamp due to time zone differences.
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.
Use Enumerable#max
:
latest_date = [date1, date2].max
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