I am trying to use DateTime to go through a list of dates, and find the days of week (Monday, Tuesday, Wednesdat, &c.) that are most often associated with each date in the list.
I am trying this:
contents = CSV.open 'event_attendees.csv', headers: true, header_converters: :symbol
contents.each do |row|
times = contents.map { |row| row[:regdate] }
target_days = Hash[times.group_by { |t| DateTime.strptime(t, '%m/%d/%Y %H:%M').wday }.map{|k,v| [k, v.count]}.sort_by{ |k,v| v }.reverse]
puts target_days
I get:
{1=>6, 2=>5, 5=>4, 6=>2, 0=>1}
From what I understand wday will represent each day as 0(sunday), 1(monday), &c. I am stumped on how to convert this into the actual name of the day? Or how can this be converted to the abbreviated name (Sun, Mon, Tue, &c.)?
Also, I'm not positive the above is returning the correct days. Looking at my list, there are six dates for 11/12/2008. November 12, 2008 was a Wednesday — but it looks like it is showing that the most common day, with a count of 6, is Monday. So, I'm not sure that this is really counting the correct day of the week.
Can someone please explain why what I am doing doesn't seem to be counting the correct day of the week, also — how to convert this to the name of the day and abbreviated name?
Thank you!
You can convert the wday integers to full names using Date::DAYNAMES and abbreviated names using Date::ABBR_DAYNAMES:
Date::DAYNAMES[3]
#=> "Wednesday"
Date::ABBR_DAYNAMES[3]
#=> "Wed"
Update
As far as your algorithm goes, it looks right to me:
require "date"
times = [
"4/25/2014 00:00", # Friday
"4/21/2014 00:00", # Monday
"4/22/2014 00:00", # Tuesday
"4/20/2014 00:00", # Sunday
"4/22/2014 00:00", # Tuesday
"4/21/2014 00:00", # Monday
"4/21/2014 00:00", # Monday
"4/19/2014 00:00"] # Saturday
target_days = Hash[times.group_by do |time|
DateTime.strptime(time, "%m/%d/%Y %H:%M").wday
end.map do |key, value|
[Date::ABBR_DAYNAMES[key], value.count]
end.sort_by do |key, value|
value
end.reverse]
puts target_days
#=> {"Mon"=>3, "Tue"=>2, "Sat"=>1, "Sun"=>1, "Fri"=>1}
I would double check the contents of the file, and then step through the algorithm to see what's going wrong.
time = Time.new(2000)
# The full weekday name
puts time.strftime("%A")
# Saturday
# The abbreviated name
puts time.strftime("%a")
# Sat
See Time.strftime for more details. DateTime has the same implementation.
Related
Im working on a birthday widget, that shows the name of the person who is the closest to have his birthday.
I currently have the following code;
<%
def closest_birthdate(birthdates)
sorted_dates = birthdates.sort_by do |_, value|
(DateTime.parse(value) - DateTime.now).abs
end
name, birthday = sorted_dates.first
"#{name} has his birthday on #{Date.parse(birthday).strftime('%m-%d')}"
end
%>
<% hash = { 'Bart' => '2017-12-06',
'Thomas' => '2017-10-06',
'William' => '2018-09-05',} %>
<%= closest_birthdate(hash) %>
It returns the following:
Thomas has his birthday on 10-06
Now, after his birthday, I have to change the year from 2017 to 2018.
The dates with names are currently stored as strings.
How can I change the strings to dates?
How can I use their actual birth dates so I dont have to change the year from 2017 to 2018 everytime they had their birthday?
Storing dates
A Date instance can be created via Date::new:
require 'date'
birthdate = Date.new(1978, 12, 6)
#=> #<Date: 1978-12-06 ...>
Calculating the next birthday
The next or upcoming birthday for the above birthdate is 2017-12-06, so we have to combine today's year and the birthdate's month and day:
today = Date.today
#=> #<Date: 2017-10-06 ...>
next_birthday = Date.new(today.year, birthdate.month, birthdate.day)
#=> #<Date: 2017-12-06 ...>
What happens if the birthday already occurred this year?
birthdate = Date.new(1985, 9, 5)
#=> #<Date: 1985-12-06 ...>
next_birthday = Date.new(today.year, birthdate.month, birthdate.day)
#=> #<Date: 2017-09-05 ...>
To actually get the next birthday, we have to add a year in that case: (i.e. if next_birthday happens to be before today)
next_birthday = next_birthday.next_year if next_birthday < today
#=> #<Date: 2018-09-05 ...>
Finding the closest birthday
Let's move the above calculation into a method:
def next_birthday(birthdate, today = Date.today)
date = Date.new(today.year, birthdate.month, birthdate.day)
date < today ? date.next_year : date
end
To find the closest (upcoming) birthday, we can either use sort_by and first:
hash = {
'Bart' => Date.new(1978, 12, 6),
'Thomas' => Date.new(1981, 10, 6),
'William' => Date.new(1985, 9, 5)
}
hash.sort_by { |_name, birthdate| next_birthday(birthdate) }
#=> [
# ["Thomas", #<Date: 1981-10-06 ...>],
# ["Bart", #<Date: 1978-12-06 ...>],
# ["William", #<Date: 1985-09-05 ...>]
# ]
hash.sort_by { |_name, birthdate| next_birthday(birthdate) }.first
#=> ["Thomas", #<Date: 1981-10-06 ...>]
or min_by:
hash.min_by { |_name, birthdate| next_birthday(birthdate) }
#=> ["Thomas", #<Date: 1981-10-06 ...>]
Generating output
name, birthdate = hash.min_by { |_name, birthdate| next_birthday(birthdate) }
puts "#{name}'s birthday is on #{birthdate.strftime('%m-%d')}"
# Thomas's birthday is on 10-06
We could also add the age:
age = next_birthday(birthdate).year - birthdate.year
#=> 36
puts "#{name} is turning #{age} on #{birthdate.strftime('%m-%d')}"
# Thomas is turning 36 on 10-06
Edge case
February 29 only occurs on leap years:
Date.new(1984, 2, 29)
#=> #<Date: 1984-02-29 ...>
Attempting to create a February 29 on a non-leap year results in an error:
Date.new(2017, 2, 29)
#=> ArgumentError: invalid date
You could remove those dates or adjust them unless today's year is leap? (to Feb 28 or Mar 1).
There is no standard object as a (birth)day without specification of the year. So you should just save an array of month and day of month. And it is much better to save them as integers rather than strings.
hash = {
'Bart' => [12, 6],
'Thomas' => [10, 6],
'William' => [9, 5],
}
To convert them to a date with a year specification (let's say year = 2018), do like this:
require "date"
date = Date.new(year, *hash['Bart']) rescue nil
Implementing it fully would be like this:
require "date"
def closest_birthdate(birthdates)
year, month, day = Date.today.year, Date.today.month, Date.today.day
name, (m, d) =
birthdates
.sort_by(&:last)
.bsearch{|_, (m, d)| month <= m and day < d}
return unless Date.new(year, m, d) rescue nil
"#{name} has his birthday on #{year}-#{m}-#{d}"
end
closest_birthdate(hash)
Try this if you don't want to change the structure:
<%
def closest_birthdate(birthdates)
sorted_dates = birthdates.sort_by do |_, value|
array = value.split("-") # you got something like this [2017, 12, 06]
(DateTime.parse("#{DateTime.now.year}-#{array[1]}-#{array[2]}") - DateTime.now).abs
end
name, birthday = sorted_dates.first
"#{name} has his birthday on #{Date.parse(birthday).strftime('%m-%d')}"
end
%>
<% hash = { 'Bart' => '1978-12-06',
'Thomas' => '1972-10-06',
'William' => '1992-09-05',} %>
<%= closest_birthdate(hash) %>
Hope this helps.
How can i retrieve Retrieve first and last day of the month with Ruby (DateTime)?
I want to create invoices that start the first day and finishes the last day of the month.
Given a year and a month:
year = 2017
month = 5
You can pass these to Date.new along with a day value of 1 and -1 to get the first and last day respectively:
require 'date'
Date.new(year, month, 1) #=> #<Date: 2017-05-01 ...>
Date.new(year, month, -1) #=> #<Date: 2017-05-31 ...>
Use the beginning_of_month and end_of_month methods
irb(main):004:0> n = DateTime.now
=> Wed, 10 May 2017 14:48:01 +0300
irb(main):005:0> n.to_date.beginning_of_month
=> Mon, 01 May 2017
irb(main):006:0> n.to_date.end_of_month
=> Wed, 31 May 2017
Let's say I had a Ruby Array of Dates like:
2011-01-20
2011-01-23
2011-02-01
2011-02-15
2011-03-21
What would be an easy and Ruby-esque way of creating a Hash that groups the Date elements by year and then month, like:
{
2011 => {
1 => [2011-01-20, 2011-01-23],
2 => [2011-02-01, 2011-02-15],
3 => [2011-03-21],
}
}
I can do this by iterating over everything and extracting years, months and so on, then comining them.
Ruby offers so many methods and blocks for Arrays and Hashes, there must be an easier way?
require 'date'
dates = [
'2011-01-20',
'2011-01-23',
'2011-02-01',
'2011-02-15',
'2011-03-21'
].map{|sd| Date.parse(sd)}
Hash[
dates.group_by(&:year).map{|y, items|
[y, items.group_by{|d| d.strftime('%B')}]
}
]
#=> {2011=>{"January"=>[#<Date: 2011-01-20 (4911163/2,0,2299161)>, #<Date: 2011-01-23 (4911169/2,0,2299161)>], "February"=>[#<Date: 2011-02-01 (4911187/2,0,2299161)>, #<Date: 2011-02-15 (4911215/2,0,2299161)>], "March"=>[#<Date: 2011-03-21 (4911283/2,0,2299161)>]}}
I noticed you have changed month names into numbers, so you may want to replace d.strftime('%B') above with d.month or whatever else.
Here's a step-by-step explanation:
You essentially want two-level grouping: first level by year, second by month. Ruby has very useful method group_by, which groups elements by given expression (a block). So: first part is grouping original array by year:
hash_by_year = dates.group_by(&:year)
# => {2011=>[#<Date: 2011-01-20 (4911163/2,0,2299161)>, #<Date: 2011-01-23 (4911169/2,0,2299161)>, #<Date: 2011-02-01 (4911187/2,0,2299161)>, #<Date: 2011-02-15 (4911215/2,0,2299161)>, #<Date: 2011-03-21 (4911283/2,0,2299161)>]}
That gives us first level: keys are years, values arrays of dates with given year. But we still need to group the second level: that's why we map by-year hash - to group its values by month. Let's for start forget strftime and say that we're grouping by d.month:
hash_by_year.map{|year, dates_in_year|
[year, dates_in_year.group_by(&:month)]
}
# => [[2011, {1=>[#<Date: 2011-01-20 (4911163/2,0,2299161)>, #<Date: 2011-01-23 (4911169/2,0,2299161)>], 2=>[#<Date: 2011-02-01 (4911187/2,0,2299161)>, #<Date: 2011-02-15 (4911215/2,0,2299161)>], 3=>[#<Date: 2011-03-21 (4911283/2,0,2299161)>]}]]
That way we got our second level grouping. Instead of array of all dates in a year, now we have hash whose keys are months, and values arrays of dates for a given month.
The only problem we have is that map returns an array and not a hash. Thats why we "surround" whole expression by Hash[], which makes a hash out of array of pairs, in our case pairs [year, hash_of_dates_by_month].
Sorry if the explanation sounds confusing, I found harder to explain functional expressions than imperative, because of the nesting. :(
This gets you pretty close, you just need to change the numerical month number into a textual month name:
dates = %w(
2011-01-20
2011-01-23
2011-02-01
2011-02-15
2011-03-21
)
grouped = dates.inject({}) do |ret, date|
y,m,d = date.split('-')
ret[y] ||= {}
# Change 'm' into month name here
ret[y][m] ||= []
ret[y][m] << date
ret
end
puts grouped.inspect
dates = %w(
2011-01-20
2011-01-23
2011-02-01
2011-02-15
2011-03-21
)
hash = {}
dates.each do |date|
year, month = date.strftime('%Y,%B').split(',')
hash[year] ||= {}
hash[year][month] = hash[year][month].to_a << date
end
How can I calculate the day of the week of a date in Ruby? For example, October 28 of 2010 is = Thursday
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
Take a look at the Date class reference. Once you have a date object, you can simply do dateObj.strftime('%A') for the full day, or dateObj.strftime('%a') for the abbreviated day. You can also use dateObj.wday for the integer value of the day of the week, and use it as you see fit.
time = Time.at(time) # Convert number of seconds into Time object.
puts time.wday # => 0: Day of week: 0 is Sunday
Date.today.strftime("%A")
=> "Wednesday"
Date.today.strftime("%A").downcase
=> "wednesday"
Quick, dirty and localization-friendly:
days = {0 => "Sunday",
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday"}
puts "It's #{days[Time.now.wday]}"
Works out of the box with ruby without requiring:
Time.now.strftime("%A").downcase #=> "thursday"
As #mway said, you can use date.strftime("%A") on any Date object to get the day of the week.
If you're lucky Date.parse might get you from String to day of the week in one go:
def weekday(date_string)
Date.parse(date_string).strftime("%A")
end
This works for your test case:
weekday("October 28 of 2010") #=> "Thursday"
Say i have date = Time.now.to_date
then date.strftime("%A") will print name for the day of the week and to have just the number for the day of the week write date.wday.
In your time object, use the property .wday to get the number that corresponds with the day of the week, e.g. If .wday returns 0, then your date is Sunday, 1 Monday, etc.
basically the same answer as Andreas
days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
today_is = days[Time.now.wday]
if today_is == 'tuesday'
## ...
end
In C#, There is a method AddDays([number of days]) in DateTime class.
Is there any kind of method like this in ruby?
The Date class provides a + operator that does just that.
>> d = Date.today
=> #<Date: 4910149/2,0,2299161>
>> d.to_s
=> "2009-08-31"
>> (d+3).to_s
=> "2009-09-03"
>>
In Rails there are very useful methods of Fixnum class for this (here n is Fixnum. For example: 1,2,3.... ):
Date.today + n.seconds # you can use 1.second
Date.today + n.minutes # you can use 1.minute
Date.today + n.hours # you can use 1.hour
Date.today + n.days # you can use 1.day
Date.today + n.weeks # you can use 1.week
Date.today + n.months # you can use 1.month
Date.today + n.years # you can use 1.year
These are convenient for Time class too.
PS: require Active Support Core Extensions to use these in Ruby
require 'active_support/core_ext'
I think next_day is more readable than + version.
require 'date'
DateTime.new(2016,5,17)
# => #<DateTime: 2016-05-17T00:00:00+00:00 ((2457526j,0s,0n),+0s,2299161j)>
DateTime.new(2016,5,17).next_day(10)
# => #<DateTime: 2016-05-27T00:00:00+00:00 ((2457536j,0s,0n),+0s,2299161j)>
Date.new(2016,5,17)
# => #<Date: 2016-05-17 ((2457526j,0s,0n),+0s,2299161j)>
Date.new(2016,5,17).next_day(10)
# => #<Date: 2016-05-27 ((2457536j,0s,0n),+0s,2299161j)>
http://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/Date.html#method-i-next_day.
From the Date class:
+(n)
Return a new Date object that is n days later than the current one.
n may be a negative value, in which case the new Date is earlier than the current one; however, #-() might be more intuitive.
If n is not a Numeric, a TypeError will be thrown. In particular, two Dates cannot be added to each other.
Date.new(2001,9,01).next_day(30) # 30 - numbers of day
# => #<Date: 2001-10-01 ...
You can also use the advance (https://apidock.com/rails/DateTime/advance) method. I think it's more legible.
date = Date.today
# => Fri, 25 Oct 2019
date.advance(days: 10)
# => Mon, 04 Nov 2019
time = DateTime.now
# => Fri, 25 Oct 2019 14:32:53 +0200
time.advance(months:1, weeks: 2, days: 2, hours: 6, minutes: 6, seconds: 34)
# => Wed, 11 Dec 2019 20:39:27 +0200