Get weeknumber and year from date

Stef Merlijn
Stef Merlijn used Ask the Experts™
on
Hi,

I need a function that will return a string with the weeknumber and the calculated year for any date on monday (first day of the week).

Normally it would by easy:
IntToStr(WeekOf(myDate)) + ' ' + IntToStr(YearOf(MyDate))

Open in new window

But, when week of given date is arount 1 january than it should determine year based on following criteria.
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.

Example:
Week 1 of 2014 starts on 30-12-2013 till 6-1-2014
Running the code above would result in Week 1 Year 2013, where it should be Week 1 Year 2014.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
Stef MerlijnDeveloper

Author

Commented:
But that function only returns the weeknumber, not the actual year it belongs to.
Software architect
Top Expert 2012
Commented:
There is overloaded function which will return belonging year too...
var
  w, y: word;
  dt: TDateTime;
begin
  dt := DateTimePicker1.Date;
  w := WeekOfTheYear(dt, y);

  Edit4.Text := Format('Week %d Year %d', [w,  y]);

Open in new window

Stef MerlijnDeveloper

Author

Commented:
Thank you very much. Works perfectly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial