week_field gives wrong week after serialization and deserialization - ruby-on-rails

I'm using the week_field helper to generate a week picker in a form. When I choose a week and submit, my controller gets the correct serialized week (e.g. '2014-W03') which I can turn into a date object. That all works, but when the date is serialized again it is always decremented by 1 (e.g. it'll be '2014-W02'). I looked at the source code for the week_field helper and it serializes as
def format_date(value)
value.try(:strftime, "%Y-W%W")
end
but this doesn't seem to be the encoding when the date is parsed. Furthermore, parsing and then serializing a date yeilded this wonky result:
irb > Date.parse('2014-W03').strftime('%Y-W%W')
=> "2014-W02"
Any ideas as to what's going on here or how I can do this in a way that makes sense? I'd hate to have an extra +1 on the week number or change the week_field format_date definition if there's a cleaner route.

This is from http://apidock.com/ruby/DateTime/strftime :
%W - Week number of the year. The week starts with Monday. (00..53)
It seems you have the good old - start with 0 or start with 1 - problem. Strftime will start counting weeks with 0.
But maybe %V is the right thing for you:
ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
%G - The week-based year
%g - The last 2 digits of the week-based year (00..99)
%V - Week number of the week-based year (01..53)

Related

count how many specific days are in a time period

I want to count say how many Mondays we have from 2022-02-01 - 2022-03-01. I found smth like this:
=SUMPRODUCT(WEEKDAY(B4:C4)=2) - B4 and C4 are the dates
But it returns 0. I assume it only checks if specific date is the specific day. Any ideas how I can do this but for a date range? So how count how many Mondays there are in February
I also found this
=NETWORKDAYS.INTL(B4;C4;"1000000")
but this returns 25
You can take advantage of the NETWORKDAYS.INTL function by using the string method to make all the days as weekend except for Monday.
The String method states:
weekends can be specified using seven 0’s and 1’s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
In this case since you only want to know the Mondays, the string would be "0111111" and the function would look like:
=NETWORKDAYS.INTL(StartDate,EndDate,"0111111")
I think this is right. It's counting inclusively so you would get five Mondays starting on Monday 7th Feb 2022 and ended on Monday 7th March 2022 for example.
=floor((B2-(A2+7-weekday(A2,12)))/7)+1
where A2 and B2 contain the start date end end date.
Obvs nul points for me again but for the record this could be generalised if you put the day number in C2 (e.g. 1 if you want to find Sundays, 2 for Mondays):
=floor((B2-(A2+7-weekday(A2,10+C2)))/7)+1

How to get N days ago in Dart's DateTime?

There's the subtract() method but the documentation says it's not aware of daylight savings which makes it pretty much useless in this case, or in any other case except where the programmer doesn't know how many milliseconds there are in 24 hours.
I'm thinking of two ways:
get the day of the month, and then subtract N from it and if it's less than 1 then subtract the month and the year if appropriate and set the day for the last day of whichever month it turns out to be
OR
subtract N days from the noon of the current day and then get start of the day for the resulting day
Is there some easier/better way to do this?
You should probably try to convert both DateTimes to UTC (standardize), then call difference(). That converts it to a nice, easy Duration, which you can convert as necessary to hours, days, months, or whatever else.
DateTime one = somedatetime.toUtc();
DateTime two = someotherdatetime.toUtc();
Duration diff = one.difference(two);
//Then just convert...
return diff.inDays;

How to get first day of previous month Datetime with Dartlang

I want to get first day of previous month in Dart as DateTime.
Below code is working correctly even for x = 1 (passing 0 as month)
print(new DateTime(2016,x-1,1));
but is it by design or I should not relay on it ?
It is by design.
The DateTime constructor you are using allows overflow and underflow on both days and months. A month value less than one means a month prior to January, which is a month in a previous year. Likewise a day value of less than one is a day in a previous month.

Get date from week number in Google Sheets

If I have week 7 in 2017 what week date is the Monday in that week in Google Sheets?
=DATE(B9,1,1)-WEEKDAY(DATE(B9,1,1),3)+7*(WEEKDAY(DATE(B9,1,1),3)>3)+7*(A9-1)
is the least complicated formula I know which works for week numbers in Sweden (i.e. Monday first day of week, ISO rules for what is week 1).
Short answer (A1==Week, B1==Year):
=DATE(B1;1;1)+((A1-1)*7)-WEEKDAY(DATE(B1;1;1);3)
Long answer:
DATE(<year>;1;1) // days since 1970 until the frist day of the year
plus
((<week number>-1)*7) // how many days into the year is this week
minus
WEEKDAY(DATE(<year>;1;1);3) // how many extra days from previous year in first week
PS:
This assumes monday as the first day of week you have to change the arguments for WEEKDAY to change it to sunday
Because of this definition (https://en.wikipedia.org/wiki/Week) the 4th of January must be used instead the 1st. The 4th of January is the first day which is always in the week 1.
=DATE(B1;1;4)+((A1-1)*7)-WEEKDAY(DATE(B1;1;4);3)
If you are using ISO weeks, the accepted answer doesn't account for weeks overlapping on 2 technical years like 2020-w53, which is from 28 Dec 2020 until 3 Jan 2021.
Therefore I'm using this formula instead:
=DATE(K2,1,1)-WEEKDAY(DATE(K2,1,1),2)+7*(WEEKDAY(DATE(K2,1,1),2)>3)+7*(L2-1) +1
Where K is the Year, and L is the Week number (split in 2 columns from yyyy-ww)
to have it in an arrayformula:
=ArrayFormula(if(K2:K="",, DATE(K2:K,1,1)-WEEKDAY(DATE(K2:K,1,1),2)+7*(WEEKDAY(DATE(K2:K,1,1),2)>3)+7*(L2:L-1) +1 ))
You can use =ArrayFormula(if(E2:E="",,split(E2:E,"-"))) to split yyyy-ww in two columns.
NOTE: This formula would return the Monday (Which is the first day of the week in international standard, ISO)
Worked this up for 2023. It will work through end of 2024 too .. that said the AND logic is flawed .. feel free to suggest something to make this better
=IFS(
AND(ISOWEEKNUM(A8)=52,YEAR(A8)<>YEAR(A7)),
DATE(YEAR(A8-1),1,1)-WEEKDAY(DATE(YEAR(A8-1),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8-1),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1),
DATE(YEAR(A8),1,1)-WEEKDAY(DATE(YEAR(A8),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1)
)

Convert week number to date range with Saturday as beginning_of_week

I need to use Saturday as the week start and calculate the beginning and the end of week from week numbers. I need week 53 to be properly accounted for as well.
Date.beginning_of_week = :saturday
works fine, but I have not found a way to generate week start and end dates only from a year and week number. Date.commercial is the only method I have been able to use thus far to convert a week number and year only to a date. I have been unable to get Date.commercial to recognize Saturdays as the week start.
I need to use Saturday as the week start and calculate the beginning and the end of week from week numbers.
Given an instance of Date representing the first Saturday, and the week number, this is quite simple, unless I'm missing something.
def beginning_of_week(first_saturday, week_num) {
return first_saturday + (7 * week_num.to_i).days
}
The days method comes from activesupport.

Resources