Is a date on a weekend

I need to find out if a date I have selected from a datetimepicker is a saturday or sunday. How can i test a date for example; 14/10/2007.
ding-dongAsked:
Who is Participating?
 
rfwoolfCommented:
Add DateUtils to the uses clause of your unit/form

Then this procedure will do it:

function DayOfTheWeek(const AValue: TDateTime): Word;

eg.
var
i: integer;
begin
i := DayOfTheWeek(14/10/2007);
if i = 6 then showmessage('Its a Saturday');
if i = 7 then showmessage('Its a Sunday');
end;

===

From the helpfile:

Call DayOfTheWeek to obtain the day of the week represented by a specified TDateTime value. DayOfTheWeek returns a value between 1 and 7, where 1 indicates Monday and 7 indicates Sunday.
 Note:
 DayOfTheWeek is ISO 8601 compliant (where Monday is considered the first day of the week). To obtain the day of the week where Sunday is considered the first day of the week, use the DayOfWeek function instead.
 
Tip:
 To make the return value more readable, use the Day of week constants.
 
0
 
ramboliviaCommented:
Hi ding-dong,

You can use the DayOfTheWeek function which returns the day of the week constants:

  DayMonday = 1;
  DayTuesday = 2;
  DayWednesday = 3;
  DayThursday = 4;
  DayFriday = 5;
  DaySaturday = 6;
  DaySunday = 7;

Good luck,

R.
0
 
rfwoolfCommented:
er, just to be clear in my previous post, when I said
function DayOfTheWeek(const AValue: TDateTime): Word;
--I didn't mean you should add that function to your unit -- that function is part of DateUtils -- you just call that procedure using that syntax.
0
 
JohnjcesCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
X : Integer;
begin
// SplitStr('This is a test of this stuff', 4, ListBox1.Items);
X := DayofWeek(DateTimePicker1.DateTime);
If (X > 1) and (X < 7) then
 Edit1.text := 'This is a weekday'
  else
   Edit1.Text := 'This is a weekend';
end;
0
 
JohnjcesCommented:
WOOPS!

Had stuff in there from another post. Corrected...

procedure TForm1.Button1Click(Sender: TObject);
 var
 X : Integer;
begin
 X := DayofWeek(DateTimePicker1.DateTime);
 If (X > 1) and (X < 7) then
  Edit1.text := 'This is a weekday'
   else
    Edit1.Text := 'This is a weekend';
 end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.