Get Week Of The Year - delphi

Delphi Xe, Win 7, System TimeZone UTC+4:0, The first day of week in system - Monday, Time is synchronised with time.windows.com
In a palette of standard components Delphi Win32 there is component MonthCalendar, at it property WeekNubers=true (display of numbers of weeks), date=now, the first day - Monday.
Shows on 19 number (today) - number of week 43.
I request number through dateutils.WeekOfTheYear(now) = returns 42.
Source (0.5 Mb) http://www.sendspace.com/file/yuz6ko
Judging by an initial code of component MonthCalendar - there like all it is true (even if not to expose the first day of week, it takes value from system parametres).
And in inquiry WeekOfTheYear it is told, that all is considered also standard ISO applied, but numbers do not coincide :(
In any way I will not understand, where one week disappears - if to look on a calendar, should be 43 and if to consider, that weeks 53 and the first concerns last year...
By the way WeekOfTheYear on 01.01.2011 too returns 52 though like should return 1...
Why distinctions and where it is correct?

Both results are correct, the difference is because the DateUtils.WeekOfTheYear function uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday.
Check this explanation about the Weeks Numbers in the ISO 8601 format.
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in
week 01. If 1 January is on a Friday, Saturday or Sunday, it is in
week 52 or 53 of the previous year (there is no week 00). 28 December
is always in the last week of its year.
Also the Embarcadero online help for the DateUtils.WeekOfTheYear function says
....if the first calendar day of the year is a Friday, Saturday, or
Sunday, then for the first three, two, or one days of the calendar
year, WeekOfTheYear returns the last week of the previous year.
Similarly, if the last calendar day of the year is a Monday, Tuesday,
or Wednesday, then for the last one, two, or three days of the
calendar year, WeekOfTheYear returns 1 (the first week of the next
calendar year).
So due which the 01-01-2011 was Saturday this week is considered the week number 52 of the previous year.
And that also explains why the week number returned for today (19-10-2011) is 42 instead of 43.

First of all, I could see no difference between the week numbers in the TMonthCalendar component and those returned by DateUtils.WeekOfTheYear(). (Monday is the first day of week on my system.) Both show 42 for the current week and 52 (not 53) for the week of 2011-01-01. (It's 53 for 2010-01-01, so maybe you just mixed that up.)
Now, as to why it's 52 or 53, but not 1, well, it's according to the definition of the first week by ISO:
First week is the week with the year's first Thursday in it.
So week number 1 in 2011 starts on 2011-01-03, and the previous one belongs to the previous ISO year.

The TMonthCalendar can treat the last week of the year as the first week of the next year, WeekOfTheYear function does not do it, hence the difference.
For example according to TMonthCalendar, 1 Jan 2012 belongs to both last week of 2011 and first week of 2012; 2 Jan 2012 is the first day of the second week of 2012.
According WeekOfTheYear function, 1 Jan 2012 belongs to the last week of 2011 only; 2 Jan 2012 is the first day of the first week of 2012.
Monday is the first day of the week for me.

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

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)
)

How to determine the current week in a Google Calendar sidebar gadget

I am writing a Google Calendar sidebar gadget to keep track of the total hours per event tag (as determined in details of the event i.e. "tags: work").
Users can change the current week, month, day they are viewing in the calendar and I want to be able to count up the hours pertaining to their current view.
I don't see anywhere in the gadget API (or any other Google Calendar API) that allows gadgets to access the currently displayed view. I have noticed that the URL has an anchor tag that looks like
g|week-2+23127+23137+23131
which corresponds to viewing Monday Feb. 23, 2015 - Sunday March 1, 2015 in week mode.
I have also noticed the following relationships:
23127 is the first day in the view
23137 is the last day in the view
23131 is the day selected in the month view (on the left of the calendar)
If there is a way to get the currently displayed view using the API, that would be ideal but I would settle for parsing the anchor tag. Unfortunately I cannot decipher how the numbers work.
Google API
The currently displayed date range can be accessed using the following call:
google.calendar.subscribeToDates(function(d) {
// do something
});
where d is a Google date range d.startTime and d.endTime being the beginning and end.
Numbers
The numbers in the URL do not correspond directly to epoch date and time. Rather, each year has 512 days associated with it and each month has 32 days. For example, February has 28 days regularly but every leap year it has 29. The calendar never has to adjust for this since it simply allots each month 32 days and comes out with a nice even number every time.
A careful examination of the date ranges displayed will also show you that if you subtract the number for December 31 from January 1 you get 130. Accounting for the beginning and the end (don't count December 31 and January 1) will give you 128.
12 * 32 + 128 = 512 -- 12 months a year, 32 days a month and a 128 gap per year
Also, for some reason January 1, 1970 has the associated number of 33 so add that to your calculations when determining dates.
This wouldn't fit in the comments, but here's how the encoding works:
The encoding scheme makes it easy to find the day/month/year from the number.
Take 23131 which yields Feb 27, 2015 (from the example in your question).
Divide by 512 and add 1970 (epoch) for the year.
23131 / 512 = 45.xxx => 45 + 1970 = 2015.
Get the remainder of that division and divide by 32 to find the month.
23131 mod 512 = 91 / 32 = 2.xxx = February
Get the remainder of that division and it's the day.
91 mod 32 = 27

How does NSMonthCalendarUnit and NSYearCalendarUnit behave exactly when used with UILocalNotification repeatInterval?

How does NSMonthCalendarUnit behave exactly related to UILocalNotification repeatInterval ?
Find same day next month ?
or
Find the date for 30 days later ?
For example:
if i create a UILocalNotification with date:27/02/2015 and repeatInterval:NSMonthCalendarUnit when will be the next notification ? Do not mind whether February is 27 or 28. Just consider it is always 28. It is not the real question.
Will repetition be on 27/03/2015 or 29/03/2015 ?
Same question also applies for NSYearCalendarUnit. Is it just a addition of 365 days or does it mean same day, same month of next year (27/03/2016) ?
I would post this as an edit, but it's a bit long.
I imagine this does is based on the unit. For instance, if you create a uilocalnotification with date Feb 2, 2015 and give it an NSMonthCalendarUnit, it will call it again at every month interval for the same date.
Broken down into components Feb 2 2015 is NSDayCalendarUnit=2, NSMonthCalendarUnit=2 NSYearCalendarUnit=2015. So, the notification will be called whenever NSDayCalendarUnit=2 and NSYearCalendarUnit=2015 for any NSMonthCalendarUnit. (so if you did a leap-year date at the end of the month, like jan 30, and it was a leap year month in february, it wouldn't get called.) So, this would have the same day and year for ever NSMonthCalendarUnit. The same goes for year, except for years.
It would not make much sense for this to be an addition of days. Imagine setting the NSMonth interval for a date on the first and having it be returned on the 31st of the same month, that wouldn't be a very practical API.

Showing days passed in month, or total days in month if the month is over (google spreadsheets)

Looking for a formula (for google spreadsheets) that shows either the days past in the current month if we're not past the last day of the current month, or the total days in that month if that month is behind us.
So, if today's March 25th, the formula would output 25 ... if today's April 1st, though, the formula would output 31.
Based on your sample, this should work (assuming the date is in A1):
=if(day(A1)=1,A1-date(year(A1),month(A1)-1,1),A1-date(year(A1),month(A1),1)+1)

Resources