How do you find which week to month to day?

It seems I can’t plunge into my head, which can be an easy question.

Suppose I have a date.

Fri, 14 Sep 2012 18:37:50 +0200 

How to find out what week this date is for this month? This is the first, second ...? Third?

Thanks!

+5
source share
6 answers

sachin87 has a library for defining such a thing.

+8
source

Why use a library? In Ruby, it is by default:

 Week number: The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W). The days in the year before the first week are in week 0. %U - Week number of the year. The week starts with Sunday. (00..53) %W - Week number of the year. The week starts with Monday. (00..53) > Time.zone.parse("2012-01-01").strftime("%U") => "01" 

Thus, given that we can determine which week is a given date in a year, we can do some math to figure out which week of the month it occurs.

 > week_of_year_for_first_of_month = Time.zone.parse("2012-07-01").strftime("%U").to_i > week_of_target_date = Time.zone.parse("2012-07-14").strftime("%U").to_i > week_occurs_in = week_of_target_date - week_of_year_for_first_of_month + 1 > week_occurs_in # => 2 

Or method:

 def week_of_month_for_date(date) my_date = Time.zone.parse(date) week_of_target_date = my_date.strftime("%U").to_i week_of_beginning_of_month = my_date.beginning_of_month.strftime("%U").to_i week_of_target_date - week_of_beginning_of_month + 1 end > week_of_month_for_date("2012-07-14") # => 2 > week_of_month_for_date("2012-07-15") # => 3 
+10
source

Please note that this depends on how you count the weeks. Let them say that June 1 is Saturday. What week do you think June 2 is? This may be the second week, or maybe the first, if you think that the counting week should contain at least 4 days.

Or perhaps, considering that June 2 is Sunday, what is the week number for this Sunday? This is definitely the first Sunday. If this is what you mean, then it is really simple. Dates 1 to 7 are always the first [day of the week] in the month. Dates 8-14 are always second. And so on. All you have to do is create a hash and it will work for any month.

+3
source

week_of_month gem looks like it might be a bit overkill. This implementation uses many array sections and an Array.include? check Array.include? .

Instead, here is a module that you can mix with Date and Time to get the desired behavior.

 require "active_support/core_ext/date" require "active_support/core_ext/time" module WeekCalculator def week_of_year(mondays = false) # Use %U for weeks starting on Sunday # Use %W for weeks starting on Monday strftime(mondays ? "%W" : "%U").to_i + 1 end def week_of_month(mondays = false) week_of_year(mondays) - beginning_of_month.week_of_year(mondays) + 1 end end class Date include WeekCalculator end class Time include WeekCalculator end 

 Date.new(2014, 1, 1).week_of_year # => 1 Date.new(2014, 1, 1).week_of_month # => 1 Date.new(2014, 7, 1).week_of_year # => 27 Date.new(2014, 7, 1).week_of_month # => 1 Date.new(2014, 7, 27).week_of_year # => 31 Date.new(2014, 7, 27).week_of_month # => 5 Date.new(2014, 7, 27).week_of_year(:monday) # => 30 Date.new(2014, 7, 27).week_of_month(:monday) # => 4 
+2
source

Try this, find the week a_date (given that the first week is 1, and the first day of the week is Monday):

 week = (((a_date.mday + Date.new(a_date.year, a_date.month, 1).wday - 1) / 7) + 1) 
0
source

I was asked to answer "June 30, 2017 - this is nth Friday in June 2017."

I solved this by building an array of all the day names (Monday, Tuesday, etc.) dialed before the specified date and counting the number of matches.

 target_date = Date.new(2017, 6, 30) (1..target_date.day).select do |day_of_month| Date.new(target_date.year, target_date.month, day_of_month).strftime('%A') == target_date.strftime('%A') end.length 
0
source

Source: https://habr.com/ru/post/1434257/


All Articles