Get weeknumber and year from date

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.
Stef MerlijnDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
0
Stef MerlijnDeveloperAuthor Commented:
But that function only returns the weeknumber, not the actual year it belongs to.
0
Sinisa VukSoftware architectCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stef MerlijnDeveloperAuthor Commented:
Thank you very much. Works perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.