Ruby, Check If Date Is a Weekend

Ruby, check if date is a weekend?

require 'date'
today = Date.today
ask_price_for = (today.wday == 6) ? today - 1 : (today.wday == 0) ? today - 2 : today

or

require 'date'
today = Date.today
ask_price_for = (today.saturday?) ? today - 1 : (today.sunday?) ? today - 2 : today

ask_price_for now holds a date for which you would want to ask the price for.

Getting the actual price which is corresponding to you date depends on your Model and your ORM-Library (i.e. ActiveRecord).

Ruby find date with your day except weekend

def add_working_days date, num
num.times.inject(date) do |date|
case date.wday
when 5 then date + 3
when 6 then date + 2
else date + 1
end
end
end

main ▶ add_working_days Date.today, 3
#⇒ #<Date: 2018-03-02 ((2458180j,0s,0n),+0s,2299161j)>
main ▶ add_working_days Date.today, 5
#⇒ #<Date: 2018-03-06 ((2458184j,0s,0n),+0s,2299161j)>
main ▶ add_working_days Date.today, 7
#⇒ #<Date: 2018-03-08 ((2458186j,0s,0n),+0s,2299161j)>
main ▶ add_working_days Date.today, 10
#⇒ #<Date: 2018-03-13 ((2458191j,0s,0n),+0s,2299161j)>

How to check if variable is Date or Time or DateTime in Ruby?

Another option:

def is_datetime(d)
d.methods.include? :strftime
end

Or alternatively:

if d.respond_to?(:strftime)
# d is a Date or DateTime object
end

How can I calculate the day of the week of a date in ruby?

I have used this because I hated to go to the Date docs to look up the strftime syntax, not finding it there and having to remember it is in the Time docs.

require 'date'

class Date
def dayname
DAYNAMES[self.wday]
end

def abbr_dayname
ABBR_DAYNAMES[self.wday]
end
end

today = Date.today

puts today.dayname
puts today.abbr_dayname

How to get the date by day of the week ruby

To get the current day in Rails, you can use:

Date.today

To get the previous Saturday (since today is Monday 3rd July, that's Saturday 1st July), you can use:

Date.today.beginning_of_week(:saturday)

Or, if what you actually wanted was the previous week's Saturday (Saturday 24th June), then you can use:

Date.today.weeks_ago(1).beginning_of_week(:saturday)

Or, if you prefer:

1.week.ago.beginning_of_week(:saturday)

...However, note that the above will return an ActiveSupport::TimeWithZone object rather than a Date object - so will behave slightly differently.

Have a read through the rails documentation - in particular, the ActiveSupport extensions to ruby's Date class to see what methods are available.

If, for some reason, you needed to do this in pure ruby (i.e. without the above mentioned ActiveSupport extensions that come bundled with rails), then you could instead utilise the Date.parse method:

require 'date'

Date.parse("Saturday") - 7
# => Sat, 01 Jul 2017

In order to convert your Date (or similar) object to the string format you desire ("24-June-2017"), you can use ruby's strftime method:

(Date.parse("Saturday") - 7).strftime('%d-%B-%Y')

In rails, it is a common convention to place such custom date formats in a locale or initializer to avoid repetition. You can then reference this format by name:

# config/initializers/date_formats.rb
# (Call this whatever you want!)
Date::DATE_FORMATS[:calendar] = '%d-%B-%Y'

# Anywhere else in the rails app
Date.today.beginning_of_week(:saturday).to_s(:calendar)
# => "01-July-2017"

How to check if today is monday?

> Time.now.monday?
=> false
> 2.days.ago.monday?
=> true

How to find the date for the upcoming weekend in Ruby?

Use the Chronic gem, it is fantastic!

gem install chronic

Here is an example:

require 'chronic'
Chronic.parse('this Saturday noon')
#=> Sat Sep 24 12:00:00 PDT 2011

I edited the output to reflect the newest gem version since I am using an older version.

How to calculate next, previous business day in Rails?

As far as I understand, this is what you are looking for? (tested it)

require 'date'
def next_business_day(date)
skip_weekends(date, 1)
end

def previous_business_day(date)
skip_weekends(date, -1)
end

def skip_weekends(date, inc = 1)
date += inc
while date.wday == 0 || date.wday == 6
date += inc
end
date
end

You can test it as follows:

begin
t = Date.new(2009,9,11) #Friday, today
puts "Today: #{Date::DAYNAMES[t.wday]} #{Date::MONTHNAMES[t.mon]} #{t.day}"
nextday = next_business_day(t)
puts "Next B-day: #{Date::MONTHNAMES[nextday.mon]} #{nextday.day}"
previousday = previous_business_day(nextday)
puts "back to previous: #{Date::MONTHNAMES[previousday.mon]} #{previousday.day}"
yesterday = previous_business_day(previousday)
puts "yesterday: #{Date::MONTHNAMES[yesterday.mon]} #{yesterday.day}"
end

Skipping dates in ruby

Here's a literal translation of your description of the requirement into Ruby:

require "date"

p today = Date.today
p (1..Float::INFINITY)
.lazy
.map { |offset| today + offset }
.reject { |date| date.saturday? || date.sunday? }
.drop(5)
.next

The code creates a lazy enumeration of all days starting from tomorrow, rejects all Saturdays and Sundays, drops the next 5 valid candidates, and returns the next one.

Output:

#<Date: 2016-05-06 ((2457515j,0s,0n),+0s,2299161j)>
#<Date: 2016-05-16 ((2457525j,0s,0n),+0s,2299161j)>

A quick peek at the calendar shows that the output is correct.

Check if date is within the past seven days

You can do it like this:

require "date"
Date.today - 7 <= Date.parse("07-09-2015", "%d-%m-%Y")


Related Topics



Leave a reply



Submit