0

I know there's a method cweek, which gives me the number of the current week.

However, how do I get the count of weeks for a given year?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user984621
  • 46,344
  • 73
  • 224
  • 412
  • Get `cweek` for the Dec 31st? – Sergio Tulentsev Dec 30 '19 at 15:23
  • I was playing with this option, but this gives me the results `1`, `52` or `53`. I have this like a plan B, but probably looking for a more direct solution that I wouldn't need to parse further. – user984621 Dec 30 '19 at 15:27
  • This gives you 1 _or_ 52? Hard to believe. Show an example. – Sergio Tulentsev Dec 30 '19 at 15:30
  • I've answer with cweek but I've have some issues with current year try with strftime and %U – Horacio Dec 30 '19 at 15:32
  • 3
    `Date.new(2014,12,31).cweek` => `1`, `Date.new(2015,12,31).cweek` => `53`, `Date.new(2016,12,31).cweek` => `52` – user984621 Dec 30 '19 at 15:34
  • @user984621 try with strftime as I do in my answer – Horacio Dec 30 '19 at 15:34
  • 1
    2014-12-31 was a Wednesday. That means most days of that week were already in the first week of 2015. Therefore I think the answer is correct. Why do you think it is incorrect. What answer would you expect? – spickermann Dec 30 '19 at 15:43
  • @spickermann I am using as a point of reference this: https://www.epochconverter.com/weeks/2015 – user984621 Dec 30 '19 at 15:53
  • @user984621: thanks for the examples. You learn something new every day :) – Sergio Tulentsev Dec 30 '19 at 15:58
  • That means `cweek` returns the expected returns. Solved! – spickermann Dec 30 '19 at 16:14
  • @spickermann so if `Date.new(2014,12,31).cweek` returns `1`, how do I know the real count of the weeks? In this case, is it 52 or 53? Am I overthinking it or overlooking something? – user984621 Dec 30 '19 at 16:50
  • 2
    I am sorry I do not understand the question. Either there are always 52 weeks in a year because 36[5|6]/7 are < 52.5 and you do not care about complex calendar week calculations. Or you want to know the calendar week of a specific date and that might be 52, 53 or 1 for the last days in December. What result would you expect for 2014? – spickermann Dec 30 '19 at 17:04

1 Answers1

4

Maybe with this?

Date.new(2019 , 12, -1).strftime("%U").to_i

if you want the current year could try this

Date.new(Time.now.year , 12, -1).strftime("%U").to_i

Look at the strftime configuration

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)
Horacio
  • 2,865
  • 1
  • 14
  • 24
  • Thank you for your answer (not sure why it's downvoted either). I tried `Date.new(2015 , 12, -1).strftime("%U").to_i`, which gives me `52`. When I look here https://www.epochconverter.com/weeks/2015, it says there are 53 weeks in 2015 – user984621 Dec 30 '19 at 15:37
  • Probably someone with slow fingers. Take your +1 good man. – Sebastián Palma Dec 30 '19 at 15:38
  • cool :), if it works can mark as valid. – Horacio Dec 30 '19 at 15:38
  • @Horacio `Date.new(2015,12,31).cweek` returns `53`, `Date.new(2015 , 12, -1).strftime("%U").to_i` returns `52`... not sure where the difference comes from – user984621 Dec 30 '19 at 15:41