Back


Lennie's Weekly Tip: First Day of the Year Function

31 October 1999 Ref. 02


I'ved written the following simple function, which will return the day of the 1st of January of a specified year.

function FirstDayOfYear (year : integer) : integer;

begin
    Result := DayOfWeek(StrToDate('01/01/' + IntToStr(year)));
end;

This function will return a value between 1 and 7, where Sunday is the first day and Saturday is the seventh.

To use the function, simply place a TEdit, TLabel and TButton components on a Form (TForm). And write the following code in the Button's OnClick-event handler.

....
Label1.Caption := LongDayNames[FirstDayOfYear(StrToInt(Edit1.Text))];
....
 

Type, in any year in the Edit-box and click on the button. The label will respond with the name of the day of the 1st January of that year.

- You may need to add error-checking to make sure that a valid integer is pasted to the function, for example a year value of -10000 is totaly wrong.



Lennie's Weekly Tip : First Day of the Year Function
Fix : 21 November 1999
31 October 1999 Ref. 02
-------------------------------------------------------

Christian R. Conrad had informed me of a problem that he found on my FirstDayOfYear function. The function assume that the user's date-settings are mm/dd/yy, but many user's settings are different.

I have rewritten the function and now it use the EncodeDate function, with no date-setting problems.

function FirstDayOfYear (year : integer) : integer;

begin
  Result := DayOfWeek(EncodeDate(year, 1, 1));
end;

This function will return a value between 1 and 7, where Sunday is the first day and Saturday is the seventh.

To use the function, simply place a TEdit, TLabel and TButton components on a Form (TForm). And write the following code in the Button's OnClick-event handler.

....
  Label1.Caption := LongDayNames[FirstDayOfYear(StrToInt(Edit1.Text))];
....

Type, in any year in the Edit-box and click on the button. The label will respond with the name of the day of the 1st January of that year.

NOTE: You my need to add error-checking to make sure that a valid integer is pasted to the function, for example a year value of -10000 is totaly wrong.


Back