# Is there a decent canonical way to get total days in a month from say Time class?

**URL:** <https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996>\
**Category:** Ruby\
**Created:** [June 11, 2009, 3:04am UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996 "2009-06-11T03:04:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [June 11, 2009, 3:04am UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996/1 "2009-06-11T03:04:35Z")

</div>

I suppose I could flip through local until I generate an exception, but  
somehow  
I would think there would be a set of class methods to get:

daysofmonth = Time.daysofmonth(y,m)  
daysofyear = Time.daysofyear(y)  
leapday = Time.leapday?(y)  
leapseconds = Time.leapseconds?(y)

??  
xc

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [June 11, 2009, 3:20am UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996/2 "2009-06-11T03:20:22Z")

</div>

Xeno C. wrote:

> I suppose I could flip through local until I generate an exception, but  
> somehow I would think there would be a set of class methods to get:

Here’s a fairly simple sequence I found on the net using Date  
(attributions to  
kelyar, jgwong, and timmorgan, whoever they are):

#!/usr/bin/ruby

# 

require ‘date’

d0 = Date.new(2009,6,1)  
puts “trace d0: #{d0}”

d1 = d0 \>\> 1  
puts “trace d1: #{d1}”

d2 = d1 - 1  
puts “trace d2: #{d2}”

d3 = d2.day  
puts “trace d3: #{d3}”  
—snip—  
the above are my own illustration of what the authors show in their blog  
posts.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [June 11, 2009, 4:13am UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996/3 "2009-06-11T04:13:04Z")

</div>

On Thu, Jun 11, 2009 at 9:19 AM, Xeno  
Campanoli[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘date’
> 
> d0 = Date.new(2009,6,1)  
> puts “trace d0: #{d0}”

you could jump to end by using negative day

> d0 = Date.new(2009,12,-1)  
> =\> #\<Date: 2009-12-31 (4910393/2,0,2299161)\>  
> d0.day  
> =\> 31

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [June 11, 2009, 5:03am UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996/4 "2009-06-11T05:03:19Z")

</div>

botp wrote:

> > require ‘date’

That is nice. Thanks.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [June 11, 2009, 12:53pm UTC](https://www.ruby-forum.com/t/is-there-a-decent-canonical-way-to-get-total-days-in-a-month-from-say-time-class/169996/5 "2009-06-11T12:53:19Z")

</div>

On Thu, Jun 11, 2009 at 3:02 AM, Xeno  
Campanoli[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I suppose I could flip through local until I generate an exception, but  
> somehow I would think there would be a set of class methods to get:
> 
> ```
> daysofmonth = Time.daysofmonth(y,m)
> 
> ```

Do you want to get the maximum number of days for a given year/month  
combination? Here you go:

def days\_of\_month(year=Time.now.year, m=1)  
(Date.new(year,12,31)\<\<12-month).day  
end

days\_of\_month(2004, 2) # =\> 29  
days\_of\_month(2003, 2) # =\> 28

Michael
