1. Home
  2. Computing & Technology
  3. Delphi Programming

Week number calculating

By Zarko Gajic, About.com

Here's how to calculate a week-of-the-year index (0-51) for a given date. Week 0 is the week containing the first Sunday of the year.

~~~~~~~~~~~~~~~~~~~~~~~~~
function WeekNum(const TDT:TDateTime) : Word;
var
  Y,M,D:Word;
  dtTmp:TDateTime;
begin
  DecodeDate(TDT,Y,M,D) ;
  dtTmp := EnCodeDate(Y,1,1) ;
  Result :=
    (Trunc(TDT-dtTmp)+(DayOfWeek(dtTmp)-1)) DIV 7;
  if Result <> 0 then Result := Result - 1;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Create new program group in the Start menu
« Standard Dialogs: changing default button title

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Week number calculating

©2009 About.com, a part of The New York Times Company.

All rights reserved.