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.
44d59c6c5a7e3435409ebe94c9aa9db5?d=identicon&s=25 Srikanth Jeeva (srikanth_j)
on 2008-12-10 05: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
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 Ara Howard (ahoward)
on 2008-12-10 06:08
(Received via mailing list)
On Dec 9, 2008, at 9:37 PM, Srikanth Jeeva wrote:

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

a @ http://codeforpeople.com/
60c6b87c4cc2716c83a737e0ba2d3bc0?d=identicon&s=25 David Rio Deiros (Guest)
on 2008-12-10 06:13
(Received via mailing list)
On Wed 10.Dec'08 at 13:37:28 +0900, Srikanth Jeeva 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
44d59c6c5a7e3435409ebe94c9aa9db5?d=identicon&s=25 Srikanth Jeeva (srikanth_j)
on 2008-12-10 06: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.