Forum: Ruby how to find whether a day, is weekend

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Srikanth J. (Guest)
on 2008-12-10 06:44
hi, can any one give me the idea.,
how to find whether a given date is a weekend, that is saturday or
sunday.
is there any inbuilt methods or we have to write the code.
Thanks..

Regards,
Srikanth J
Ara H. (Guest)
on 2008-12-10 07:08
(Received via mailing list)
On Dec 9, 2008, at 9:37 PM, Srikanth J. wrote:

>
[0,6].include?(date.wday)

a @ http://codeforpeople.com/
David Rio D. (Guest)
on 2008-12-10 07:13
(Received via mailing list)
On Wed 10.Dec'08 at 13:37:28 +0900, Srikanth J. wrote:
> hi, can any one give me the idea.,
> how to find whether a given date is a weekend, that is saturday or
> sunday.
> is there any inbuilt methods or we have to write the code.

What about ... ?

  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

-drd
Srikanth J. (Guest)
on 2008-12-10 07:20

Thanks a lot all..


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


i did with this code..
This topic is locked and can not be replied to.