Skip to content

Instantly share code, notes, and snippets.

@hartct
Created December 27, 2010 16:12
Show Gist options
  • Save hartct/756253 to your computer and use it in GitHub Desktop.
Save hartct/756253 to your computer and use it in GitHub Desktop.
A couple methods to prorate prices by calculating the remaining days of the month
require 'date'
def days_remaining_in_month
day_of_month = Time.now.day
(days_in_month(Time.now.year, Time.now.month) - day_of_month)
end
def prorated_price_today(price)
day_of_month = Time.now.day
percent_of_month_remaining = BigDecimal(days_remaining_in_month.to_s) / BigDecimal(days_in_month(Time.now.year, Time.now.month).to_s)
(BigDecimal(price.to_s) * BigDecimal(percent_of_month_remaining.to_s)).round(2)
end
def days_in_month(year, month)
(Date.new(year, 12, 31) << (12-month)).day
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment