Board index » delphi » Weeknumber-routine !!

Weeknumber-routine !!

Hello !

Does anybody have a routine to convert a Date to weeknumber.

BTW I'm a finn and in our calendar e.g. the first week of this year began
in monday 30. December in 1996.

Regards,

Arponen
ai...@pp.netppl.fi

 

Re:Weeknumber-routine !!


WWVzLA0KDQogbG9vayBhdCBteSBob21lcGFnZSAgaHR0cDovL291cndvcmxkLmNvbXB1c2VydmUu
Y29tL2hvbWVwYWdlcy9GX1JlaWpzYmVyZ2VuL3BhZ2UyDQogYW5kIGRvd25sb2FkIA0KCQ0KCURh
dGU6IEVlbiBEZWxwaGkgY29tcG9uZW50IHZvb3IgZGF0dW0gYmVyZWtlbmluZ2VuLiANCg0KRm9u
cw0KDQo+IEhlbGxvICENCj4gDQo+IERvZXMgYW55Ym9keSBoYXZlIGEgcm91dGluZSB0byBjb252
ZXJ0IGEgRGF0ZSB0byB3ZWVrbnVtYmVyLiANCj4gDQo+IEJUVyBJJ20gYSBmaW5uIGFuZCBpbiBv
dXIgY2FsZW5kYXIgZS5nLiB0aGUgZmlyc3Qgd2VlayBvZiB0aGlzIHllYXIgYmVnYW4NCj4gaW4g
bW9uZGF5IDMwLiBEZWNlbWJlciBpbiAxOTk2Lg0KPiANCj4gUmVnYXJkcywNCj4gDQo+IEFycG9u
ZW4NCj4gYWltb2FAcHAubmV0cHBsLmZpDQo+IA0KPiANCg0KRm9ucyBSZWlqc2JlcmdlbiAgICAg
ICAgIEVNYWlsIEZvbnMuUmVpanNiZXJnZW5Aa29uYmliLm5sDQogICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgMTAwNDUxLjIzMDFAY29tcHVzZXJ2ZS5jb20NCg0KIGh0dHA6Ly9v
dXJ3b3JsZC5jb21wdXNlcnZlLmNvbS9ob21lcGFnZXMvRl9SZWlqc2Jlcmdlbg0KDQogUHJvZ3Jh
bW1lciAgIlNvZnQgSWRlZSBTb2Z0d2FyZSINCiAgIExlaWRzY2hlbmRhbSAgIE5ldGhlcmxhbmRz
DQogICAgIFRlbCAgKzMxICA3MCAzMjc4NjE5ICANCiAgICAgVGVsICArMzEgIDYgNTQgNzY4NDM4
ICANCg==

Re:Weeknumber-routine !!


This small piece of code should be able to calculate the number of the
week:

var
  ADate,Adate2,firstdayoffirstweek: TDateTime;
  weeknum,Year, Month, Day, firstdayofyear: Word;
begin
  ADate:= StrToDate(Edit1.Text);
  DecodeDate(Adate, Year, Month, Day);
  Adate2:= Encodedate(Year, 1, 1);
  Firstdayofyear:= DayOfWeek(ADate2)-1;
  if Firstdayofyear=0 then Firstdayofyear:= 7; {convert: Monday=1,
Sunday=7}
  if Firstdayofyear in [1,2,3,4] then
    {week no 1 starts 29, 30, 31 or 1}
    Firstdayoffirstweek:= Adate2-Firstdayofyear+1
  else
    {week no 1 starts 2, 3 or 4}
    Firstdayoffirstweek:= Adate2+Firstdayofyear-2;
  weeknum:= 1+round(ADate-firstdayoffirstweek) div 7;
  if (weeknum=53) and (DayOfWeek(ADate)-1 in [1,2,3]) then
    weeknum:= 1;
end;

The basic principle about numbering of weeks is: The first tuesday in the
new year is in week number 1. This week has at least 4 days in the new
year.

--
Uffe Kousgaard
u...@cowi.dk

Other Threads