Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Week Of The Year

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.

enter image description here

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?

like image 431
Gu. Avatar asked Oct 19 '11 05:10

Gu.


3 Answers

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.

like image 150
RRUZ Avatar answered Sep 21 '22 20:09

RRUZ


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.

like image 20
Andriy M Avatar answered Sep 22 '22 20:09

Andriy M


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.

like image 25
kludg Avatar answered Sep 20 '22 20:09

kludg