d = Date.new(2005,4,20)
d.days_in_month #-> 30
#or
d.last_day_of_month #-> 30
Thanks,
Dan
d = Date.new(2005,4,20)
puts d.days_in_month #-> 30
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
Thanks for the simple solution.
Dan
class Date
def days_in_month
Date.civil(year, month, -1).day
end
end
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
===============================================================================
# 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
#--
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"
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:
...
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