Monday, February 16, 2009

Weekday or not

hi,
wanna find whether a day is week day or not??

i have tried tht here. ,


date=date.today
[0,6].include?(date.wday)
------------------------------------------------------------------------
  require 'date'

class Date
def weekend?
self.wday == 0 || self.wday == 6
end
end

d1 = "9 December 2008"
d2 = "13 December 2008"

p Date.parse(d1).weekend? # false
p Date.parse(d2).weekend? # true
------------------------------------------------------------------------

I have used the code for calculation of weekdays between 2 dates.,

require 'date'
d1 = Date.new( 2008, 11, 1 )
d2 = Date.new( 2008, 12, 31 )

WEEKDAY_NUMBERS = [1,2,3,4,5]
weekdays = (d1..d2).select{ |d| WEEKDAY_NUMBERS.include?( d.wday ) }
p weekdays.length

----------------------------------------------------

No comments: