How do I get all Sundays between two dates in Ruby?
fun one! :D
start_date = Date.today # your start
end_date = Date.today + 1.year # your end
my_days = [1,2,3] # day of the week in 0-6. Sunday is day-of-week 0; Saturday is day-of-week 6.
result = (start_date..end_date).to_a.select {|k| my_days.include?(k.wday)}
using the data above you'll get an array of all Mon/Tue/Weds between now and next year.
how many times specific days occur in a date range in ruby?
This question is very similar to How do I get all Sundays between two dates in Ruby?.
Get all days between this dates and select the days, which weekday number is 1
or 2
. Sunday is the first weekday (= 0
).
require 'date'
start_date = DateTime.parse('2013-06-01')
end_date = DateTime.parse('2013-12-01')
result = (start_date..end_date).to_a.select { |d| [1,2].include?(d.wday) }
result.count
The result is 52
times.
List Array of Days Between Two Dates
I don't know which day you meant, thus I have shown all the ways.
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
(date_from..date_to).map(&:to_s)
Is there any way to find dates for all wednesday in next 6 months
2.1.1 :041 > date = Date.today + 1.day
Wed, 21 May 2014
2.1.1 :042 > array = []
[]
2.1.1 :043 > 24.times { array.push(date); date += 7.days; }
24
2.1.1 :044 > array
[
[ 0] Wed, 21 May 2014,
[ 1] Wed, 28 May 2014,
[ 2] Wed, 04 Jun 2014,
[ 3] Wed, 11 Jun 2014,
[ 4] Wed, 18 Jun 2014,
[ 5] Wed, 25 Jun 2014,
[ 6] Wed, 02 Jul 2014,
[ 7] Wed, 09 Jul 2014,
[ 8] Wed, 16 Jul 2014,
[ 9] Wed, 23 Jul 2014,
[10] Wed, 30 Jul 2014,
[11] Wed, 06 Aug 2014,
[12] Wed, 13 Aug 2014,
[13] Wed, 20 Aug 2014,
[14] Wed, 27 Aug 2014,
[15] Wed, 03 Sep 2014,
[16] Wed, 10 Sep 2014,
[17] Wed, 17 Sep 2014,
[18] Wed, 24 Sep 2014,
[19] Wed, 01 Oct 2014,
[20] Wed, 08 Oct 2014,
[21] Wed, 15 Oct 2014,
[22] Wed, 22 Oct 2014,
[23] Wed, 29 Oct 2014
]
Count number of days between two dates
With the Date (and DateTime) classes you can do (end_date - start_date).to_i
to get the number of days difference.
Ruby Year-Month-Date of last Sunday
In rails 4:
most_recent_sunday = Time.now.sunday.to_s
last_sunday = Time.now.last_week.sunday.to_s
To get it to the format you are after:
DateTime.parse(most_recent_sunday).strftime("%Y-%m-%d")
http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/sunday
http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/last_week
Calculate number of business days between two days
Take a look at business_time. It can be used for both the things you're asking.
Calculating business days between two dates:
wednesday = Date.parse("October 17, 2018")
monday = Date.parse("October 22, 2018")
wednesday.business_days_until(monday) # => 3
Adding business days to a given date:
4.business_days.from_now
8.business_days.after(some_date)
Historical answer
When this question was originally asked, business_time
didn't provide the business_days_until
method so the method below was provided to answer the first part of the question.
This could still be useful to someone who didn't need any of the other functionality from business_time
and wanted to avoid adding an additional dependency.
def business_days_between(date1, date2)
business_days = 0
date = date2
while date > date1
business_days = business_days + 1 unless date.saturday? or date.sunday?
date = date - 1.day
end
business_days
end
This can also be fine tuned to handle the cases that Tipx mentions in the way that you would like.
Get array of next 7 days name from today in ruby
Here's a nice little one liner to do what you want.
(0..6).map{ |n| (Date.today+n).strftime("%A")}
Assuming today is Saturday, that will produce:
["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
Quick explanation of each part:(0..6)
creates an array of numbers: [0, 1, 2, 3, 4, 5, 6]
.
.map { |n| ... }
is a function called on the above array that takes each element in one at a time as n
.
(Date.today+n)
is an object that represents today's day (based on your system clock). It lets you add a number to it to offset the date, which creates a new object.
And finally .strftime("%A")}
is called on the offset date object to produce a string from the date object. The "%A"
is a format directive for string day of the week.
Related Topics
Ruby: How to Generate CSV Files That Has Excel-Friendly Encoding
Clarification on the Ruby << Operator
How to Do Advanced String Comparison in Ruby
Using Calendar_Date_Select with Rails 3
Changing Active Model Serializers Default Adapter
How to Recursively Require All Files in a Directory in Ruby
Rails: Why Images Are Not Showing in My Rails Basic App
Does C1 Code Coverage Analysis Exist for Ruby
Rspec and Shoulda - Complementary or Alternatives
In Ruby, Should I Use ||= or If Defined? for Memoization
Deleting While Iterating in Ruby
Why Does Openuri Treat Files Under 10Kb in Size as Stringio