# Leap year wacked out months\_ago

**URL:** <https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120>\
**Category:** Rails\
**Created:** [March 6, 2008, 7:20am UTC](https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120 "2008-03-06T07:20:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [March 6, 2008, 7:20am UTC](https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120/1 "2008-03-06T07:20:03Z")

</div>

> > ```
> > 0.upto(11) do |n|
> > 
> > ```

?\> puts Time.now.beginning\_of\_month.ago(n.months)

> > ```
> > end
> > 
> > ```

Sat Mar 01 00:00:00 -0600 2008  
Thu Jan 31 00:00:00 -0600 2008  
Tue Jan 01 00:00:00 -0600 2008  
Sun Dec 02 00:00:00 -0600 2007  
Fri Nov 02 00:00:00 -0500 2007  
Wed Oct 03 00:00:00 -0500 2007  
Mon Sep 03 00:00:00 -0500 2007  
Sat Aug 04 00:00:00 -0500 2007  
Thu Jul 05 00:00:00 -0500 2007  
Tue Jun 05 00:00:00 -0500 2007  
Sun May 06 00:00:00 -0500 2007  
Fri Apr 06 00:00:00 -0500 2007

Uhhh…where is Feb in this list…

---

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [March 6, 2008, 7:22am UTC](https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120/2 "2008-03-06T07:22:24Z")

</div>

Here it is, could of sworn I was doing it this way already, sorry  
about that internet…

> > ```
> > 0.upto(11) do |n|
> > 
> > ```

?\> puts Time.now.beginning\_of\_month.months\_ago(n)

> > ```
> > end
> > 
> > ```

Sat Mar 01 00:00:00 -0600 2008  
Fri Feb 01 00:00:00 -0600 2008  
Tue Jan 01 00:00:00 -0600 2008  
Sat Dec 01 00:00:00 -0600 2007  
Thu Nov 01 00:00:00 -0500 2007  
Mon Oct 01 00:00:00 -0500 2007  
Sat Sep 01 00:00:00 -0500 2007  
Wed Aug 01 00:00:00 -0500 2007  
Sun Jul 01 00:00:00 -0500 2007  
Fri Jun 01 00:00:00 -0500 2007  
Tue May 01 00:00:00 -0500 2007  
Sun Apr 01 00:00:00 -0500 2007

---

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [March 6, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120/3 "2008-03-06T15:01:03Z")

</div>

On 6 Mar 2008, at 06:19, uncle wrote:

> > > ```
> > > 0.upto(11) do |n|
> > > 
> > > ```
> 
> ?\> puts Time.now.beginning\_of\_month.ago(n.months)
> 
> > > ```
> > > end
> > > 
> > > ```

Sounds like you’re using rails 1.x. n.months (or n.days etc…) used  
to be dumb: it just converted an integer into a corresponding number  
of seconds, an approach with obvious limitations.  
In rails 2, it’s smarter and leverages Date to di it’s computations,  
so when I run your code I get

Sat Mar 01 00:00:00 UTC 2008  
Fri Feb 01 00:00:00 UTC 2008  
Tue Jan 01 00:00:00 UTC 2008  
Sat Dec 01 00:00:00 UTC 2007  
Thu Nov 01 00:00:00 UTC 2007  
Mon Oct 01 00:00:00 UTC 2007  
Sat Sep 01 00:00:00 UTC 2007  
Wed Aug 01 00:00:00 UTC 2007  
Sun Jul 01 00:00:00 UTC 2007  
Fri Jun 01 00:00:00 UTC 2007  
Tue May 01 00:00:00 UTC 2007  
Sun Apr 01 00:00:00 UTC 2007

(Note also how your code drifts, because it’s effectively just  
subtracting a fixed number of days and not all months have the same  
length).

If you’re stuck with 1.x this probably does the trick too

0.upto(11) do |n|  
puts Time.now.months\_ago(n).beginning\_of\_month  
end

Fred

---

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [March 6, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/leap-year-wacked-out-months-ago/131120/4 "2008-03-06T15:12:15Z")

</div>

uncle wrote:

> Here it is, could of sworn I was doing it this way already, sorry  
> about that internet…

On behalf of the internet, I forgive you!
