I have the following custom function for converting hours into unit of time:
def time_expiry_text(time_expiry) # unit of time is hours
time_expiry_text = ''
if ( (time_expiry / 24) > 30 ) then # if more than 30 days use months as unit
months = ( (time_expiry / 24) / 30 )
time_expiry_text = months.to_s
time_expiry_text += months == 1 ? ' month' : ' months'
elsif time_expiry >= 24 then # if greater than or equal to 1 day or 24 hours use days as unit
days = time_expiry / 24
time_expiry_text = days.to_s
time_expiry_text += days == 1 ? ' day' : ' days'
else
hours = time_expiry
time_expiry_text = hours.to_s
time_expiry_text += hours == 1 ? ' hour' : ' hours'
end
return time_expiry_text
end
I am having two issues:
-
Sometimes I don’t always get the result I want. For ex, I got a return time of -2700 hours.
-
When the unit of time is months, I also want to return the days left as well. For example, 2 months and 13 days.
Assuming you’re using a broad sense of the definition of a month (which isn’t always 30 days, but it seems like your logic here isn’t date-related)