Get list of weeks that start on a Friday - ruby-on-rails

I want to count the number of the weeks, starting from Friday.
For more explanation: usually the counting starts on Sunday or Monday, but in this case, I want to make it start on Friday.
Some examples:
2nd Jan 2015 (Fri) ~ 8th Jan 2015 (Thu) : 1st week of 2015
...
25th Dec 2015 (Fri) ~ 31st Dec 2015 (Thu) : 52nd week of 2015
1st Jan 2016 (Fri) ~ 7th Jan 2016 (Thu) : 1st week of 2016
...
30th Dec 2016 (Fri) ~ 5th Jan 2017 (Thu) : 53rd week of 2016
6th Jan 2017 (Fri) ~ 12th Jan 2017 (Thu) : 1st week of 2017
What I need to do is
1) to get the week number from the date
ex.
input: Fri, 02 Jan 2015
output: 1
input: Sun, 27 Dec 2015
output: 52
2) to get the date range for the given week number.
I found that .strftime("%V") does almost this, but it counts weeks by every Monday.
Does anyone know nice solution to this?

Here you go:
my_array = Array.new
(Date.today..Date.today.next_year).each do |date|
if date.wday == 5
end_week = date + 6.days
my_array << "#{date.day.ordinalize} #{date.strftime('%b')} #{date.strftime('%Y')} (#{date.strftime('%a')}) ~ #{end_week.day.ordinalize} #{end_week.strftime('%b')} #{end_week.strftime('%Y')} (#{end_week.strftime('%a')}) : #{date.strftime('%U')} week of #{date.strftime('%Y')}"
end
end
# sample output of first element of array
# 30th Oct 2015 (Fri) ~ 5th Nov 2015 (Thu) : 43 week of 2015
Note: You can set any range. here I have set from Today to next year
%U - Week number of the current year, starting with the first Sunday
as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday
as the firstday of the first week (00..53)
for more info: http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime

You can do the variety of operation on the dates here...
DateAndTime
In your case, the below will work for you.
all_week(start_day = Date.beginning_of_week)

Related

How to get the date of "this Wednesday", "this Thursday" etc. in Rails

I know that Rails has an elegant way of getting dates for a certain day of the week for next week, or for last week (docs).
today = Time.zone.today
=> Fri, 24 Jan 2020
today.next_week
=> Mon, 27 Jan 2020 # Gets Monday by default
today.next_week(:thursday)
=> Thu, 30 Jan 2020 # You can get a certain day of the week by passing it in as an argument
Is there a way to do the same for this week? For example, getting the date of this Thursday, Friday, etc.? Right now, my workaround is to get the date of beginning_of_week (Monday) and calculate from there. Ideally, I wanted to do something similar to above, like Time.zone.today.this_week(:thursday).
this_monday = Time.zone.today.beginning_of_week
=> Mon, 20 Jan 2020
this_friday = this_monday.since(4.days).to_date
=> Fri, 24 Jan 2020
You could use #next_occurring
pry(main)> Time.zone.today.beginning_of_week
Mon, 20 Jan 2020
pry(main)> Time.zone.today.beginning_of_week.next_occurring(:thursday)
Thu, 23 Jan 2020

Why does adding and subtracting 2 months to a date not give back the same date?

I'm a bit confused about this outcome, taking today's date minus 2 months, and then taking that date again and adding two months, does not give me today's date when assign the dates to a variable.
Time.zone
"Eastern Time (US & Canada)"
> today = Date.today.in_time_zone
=> Thu, 31 Aug 2017 00:00:00 EDT -04:00
> a = today - 2.months # This is persisted to the db
=> Fri, 30 Jun 2017 00:00:00 EDT -04:00
> b = a + 2.months
=> Wed, 30 Aug 2017 00:00:00 EDT -04:00
If I however, just use the same object, it moves back and forth properly:
> today = Date.today.in_time_zone
=> Thu, 31 Aug 2017 00:00:00 EDT -04:00
> today - 2.months
=> Fri, 30 Jun 2017 00:00:00 EDT -04:00
> today + 2.months
=> Tue, 31 Oct 2017 00:00:00 EDT -04:00
The problem is obviously when "a" gets saved to a database, and then retrieved later on, and calculated plus 2 months..., it should match today's date.
TL;DR
A month is not a fixed duration. Adding or taking a month does not give the same "time shift" depending on which day you are.
The usual algorithm
to add or take months is the following :
try to land on the same day number (4th, 30th, 31st) as you started, just by changing the month
if you would land on an impossible date (like 31th September, 30th February, 29th February for some years) then just go the maximum allowed day number of this month
This implies that adding some months then taking out the same number of months will not necessarily give you back the same date.
Examples :
31st of some month + 1 month --> One would want to get to the 31th of next month
But if there is no 31st of next month (like for 31th of August, no 31st of September), then what to do ?
Usual interpretation would say that you want to go to the end of the month, this is 30th September (for rent or other monthly subscription, for instance)
But usually, 30th of some month - 1 month --> One would want to get to the 30th of the previous month.
That would lead to .... 30th of August. Not 31th of August.
Hence: some date + 1 month - 1 month does not necessarily give the original date !
Another example :
Start at the 30th of August.
Take a month -> 30th of July
Add a month -> You want to get to 30th of August (same number, next month) or to the end of August ?
The default algorithm will try to give the same day number -> 30th of August (which is more logical now)
Also with days...
Note that the same problem happens with days,but much less often ! When some days don't have the same number of hours, for daylight saving days, when adding and taking same number of days you might not get back to the original date and time as you started from.

how to get weak days of a particular week for a selected month and year in rails?

I want to get all the weak days of a particular week, for the selected year and month. I am getting data like year= 2015, month= 6 ,and weeknumber = 2 in controller. how to get week days for this particular data in rails?
i want a method like this which outputs the weekdays by taking week number. but the week number should be for that month like it should be less than or equal to five. below code outputs the week number for the whole year.
require 'date'
def week_dates( week_num )
year = Time.now.year
week_start = Date.commercial( year, week_num, 1 )
week_end = Date.commercial( year, week_num, 7 )
week_start.strftime( "%m/%d/%y" ) + ' - ' + week_end.strftime("%m/%d/%y" )
end
puts week_dates(22)
Because you're using rails you have the active support methods for beginning_of_week and end_of_week documented here you can do this
# first week in June
d = Date.new(2015,6, 1)
# => Mon, 01 Jun 2015
# add a week to get the second week
d += 1.week
# => Mon, 08 Jun 2015
(d.beginning_of_week..d.end_of_week).to_a
#=> [Mon, 08 Jun 2015, Tue, 09 Jun 2015, Wed, 10 Jun 2015, Thu, 11 Jun 2015, Fri, 12 Jun 2015, Sat, 13 Jun 2015, Sun, 14 Jun 2015]
If you want your week to start on sunday you can do this, passing the beginning day of the week:
(d.beginning_of_week(:sunday)..d.end_of_week(:sunday)).to_a
#=> [Sun, 31 May 2015, Mon, 01 Jun 2015, Tue, 02 Jun 2015, Wed, 03 Jun 2015, Thu, 04 Jun 2015, Fri, 05 Jun 2015, Sat, 06 Jun 2015]

get one month ago but always 15th of month

I need to get the month before today, but always to get the date with 15th.
i.e if today is March 11th 2014, I need => Feb 15th 2014.
or if the date is March 28th 2014, I need => Feb 15th 2014.
How can I achieve that?
Since you're using rails:
(DateTime.now - 1.month).beginning_of_month + 14.days
Refer to the DateTime documentation for more info.
Edit: Updated to reflect the previous month's 15th day.
(Date.today - 1.month).beginning_of_month + 14
or another way
(Date.today - 1.month).change(day: 15)
You can just create a new date object with last month's year and month numbers and 15 as the day:
today = Date.today
one_month_ago = (today - 1.month)
Date.civil(one_month_ago.year, one_month_ago.month, 15)
Do it like this:
Date.today.prev_month.beginning_of_month + 14 # => Sat, 15 Feb 2014
Use
(Date.today<<1).beginning_of_month + 14 ## => Sat, 15 Feb 2014
Or
Date.today.last_month.beginning_of_month + 14 ## => Sat, 15 Feb 2014

How to include months of next year in JQuery UI DatePicker month selector (rolling 12 next months)

is it possible to have "rolling next 12 months" (including months in next year) in JQuery UI DatePicker's month dropdown selector?
For example, at the moment (Oct 2012) I'd like to have the following months:
Oct 2012 (current year)
Nov 2012 ...
Dec 2012 (current year)
Jan 2013 (next year)
Feb 2013 ...
...
Sep 2013 (next year)
With maxDate etc I can have months from this month to end of current year. And with changeYear I can enable year selector. But is it possible to set up month selector as described?
br, Touko

Resources