Skip to content

Instantly share code, notes, and snippets.

@takeshy
Created January 29, 2014 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshy/8685742 to your computer and use it in GitHub Desktop.
Save takeshy/8685742 to your computer and use it in GitHub Desktop.
def calc_month(t,n)
y = t.year
m = t.month + n
if m < 1
y += ((m-1)/12).floor
m = 12 + (m % -12)
elsif m > 12
y += (m/12).floor
m = ((m-1) % 12) + 1
end
d = t.day
while Time.new(y,m,d).month != m
d -= 1
end
Time.new(y,m,d)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment