Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Days in month

131 views
Skip to first unread message

Dan Fitzpatrick

unread,
Apr 25, 2005, 1:58:30 PM4/25/05
to
Is there a method of Date or Time that gives the number of days in a
date's month or the last day of the month like:

d = Date.new(2005,4,20)
d.days_in_month #-> 30
#or
d.last_day_of_month #-> 30

Thanks,

Dan


jc

unread,
Apr 25, 2005, 2:20:45 PM4/25/05
to
class Date
def days_in_month
((Date.new(year, month, 1) >> 1)-1).day
end
end

d = Date.new(2005,4,20)
puts d.days_in_month #-> 30

David A. Black

unread,
Apr 25, 2005, 2:20:21 PM4/25/05
to
Hi --

Funny you should ask :-)

This came up on IRC a month or two ago, and the answer appears to be
no. So I wrote one:

# Date.dim -- days in month

require 'date'

class Date
def dim
d,m,y = mday,month,year
d += 1 while Date.valid_civil?(y,m,d)
d - 1
end
end

if __FILE__ == $0
puts (Date.today >> 1).dim
puts (Date.today << 1).dim
puts (Date.new(2004,2)).dim
end

__END__


David

--
David A. Black
dbl...@wobblini.net


Dan Fitzpatrick

unread,
Apr 25, 2005, 3:18:02 PM4/25/05
to
JC,

Thanks for the simple solution.

Dan

Brian Buckley

unread,
Apr 25, 2005, 3:22:50 PM4/25/05
to
!!! Yowser!

Ken Kunz

unread,
Apr 25, 2005, 3:44:01 PM4/25/05
to
Nice! Here's a slight variation that's even simpler:

class Date
def days_in_month
Date.civil(year, month, -1).day
end
end

Ara.T....@noaa.gov

unread,
Apr 27, 2005, 9:48:04 PM4/27/05
to Dan Fitzpatrick
On Tue, 26 Apr 2005, Dan Fitzpatrick wrote:

from the excellent impl in the 'days of month' thread earlier this week:

harp:~ > cat a.rb
require 'date'

class Date
def days_in_month
self::class.civil(year, month, -1).day
end
alias dim days_in_month
def weekdays
(1..dim).to_a
end
end

p Date::new(2005, 02).weekdays
p Date::new(2005, 12).weekdays

harp:~ > ruby a.rb
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

harp:~ > cal 2 2005
February 2005
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

harp:~ > cal 12 2005
December 2005
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

boy i love ruby.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| although gold dust is precious, when it gets in your eyes, it obstructs
| your vision. --hsi-tang
===============================================================================

"Peña, Botp"

unread,
Apr 28, 2005, 2:19:46 AM4/28/05
to
Ara.T....@noaa.gov [mailto:Ara.T....@noaa.gov] wrote:

# harp:~ > cal 2 2005
# February 2005
# Su Mo Tu We Th Fr Sa
# 1 2 3 4 5
# 6 7 8 9 10 11 12
# 13 14 15 16 17 18 19
# 20 21 22 23 24 25 26
# 27 28
#
# harp:~ > cal 12 2005
# December 2005
# Su Mo Tu We Th Fr Sa
# 1 2 3
# 4 5 6 7 8 9 10
# 11 12 13 14 15 16 17
# 18 19 20 21 22 23 24
# 25 26 27 28 29 30 31
#
#boy i love ruby.

i took a deep breath there.
I thought you were going to show us your cal.rb :-)

A ruby quiz perhaps?

kind regards -botp

#
#cheers.
#
#-a
#--


Logan Capaldo

unread,
Apr 28, 2005, 8:43:17 PM4/28/05
to

Well here's mine:

#!/usr/bin/env ruby
require 'date'
month = ARGV[0] ? ARGV[0].to_i : Date.today.mon
year = ARGV[1] ? ARGV[1].to_i : Date.today.year

month_names = %w{January February March April May June July August September
October November Decemeber}

dow_banner = %{Su Mo Tu We Th Fr Sa}
days_in_mon = Date.civil(year, month, -1).day

# Print title:
print " " * ((dow_banner.length -
(year.to_s.length + 1 + month_names[month - 1].length)) / 2)
print "#{month_names[month - 1]} #{year}\n"
print "#{dow_banner}\n"

dow_counter = Date.civil(year, month, 1).wday

print " " * 3 * dow_counter

(1..days_in_mon).each { |i|
if i < 10
print " #{i}"
else
print "#{i}"
end
print " "
dow_counter += 1
if dow_counter % 7 == 0
print "\n"
end
}
print "\n"

Kent Sibilev

unread,
Apr 28, 2005, 11:09:08 PM4/28/05
to
Someting like that:

require 'date'
require 'enumerator'

class Date
def days_in_month
self.class.civil(year, month, -1).day


end
alias dim days_in_month

def weekdays
(1..dim).to_a
end

def cal_print
banner = ABBR_DAYNAMES.map{|d| d[0..1]}.join(' ')

puts "#{MONTHNAMES[month]} #{year}".center(banner.size)
puts banner
wd = [' '] * wday + weekdays
wd.each_slice(7) do |slice|
puts slice.map{|d| d.to_s.rjust(2)}.join(' ')
end
end
end

month = (ARGV[0] || Date.today.mon).to_i
year = (ARGV[1] || Date.today.year).to_i

Date.civil(year, month).cal_print

Kent.

On 4/28/05, "Peña, Botp" <bo...@delmonte-phil.com> wrote:

"Peña, Botp"

unread,
Apr 29, 2005, 9:29:55 PM4/29/05
to
Kent Sibilev [mailto:ksr...@gmail.com] wrote something like...

...
Date.civil(year, month).cal_print

Logan Capaldo [mailto:loganc...@gmail.com] also wrote something similar.


Thanks Kent and Logan for the very quick solutions.
I guess it is not worth for the quiz then :-)

kind regards -botp


0 new messages