Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[delphi] day of week issue

Status
Not open for further replies.

Flisk

Programmer
Oct 8, 2004
11
SE
hi there, it's me again :)

i found this code on the net how to calculate the current day from a date i write in a maskedit.

Code:
 procedure TForm1.Button1Click(Sender: TObject);
var
MyDate: TDateTime;
begin
MyDate:=StrToDateTime(MaskEdit1.Text);
case DayOfWeek(MyDate) of
 1: Label2.Caption:='Sunday';
 2: Label2.Caption:='Monday';
 3: Label2.Caption:='Tuesday';
 4: Label2.Caption:='Wednesday';
 5: Label2.Caption:='Thursday';
 6: Label2.Caption:='Friday';
 7: Label2.Caption:='Saturday';
end;
end;

this code works perfect for me, BUT ! i have from the beginning forced my maskedit1.text to show the current date in YYYY-MM-DD format

Code:
MaskEdit1.text := formatdatetime('yyyy-mm-dd', Now);

so if i choose english keyboard layout that have the M/D/YYYY format, it get a error like this
'2005-05-08' is not a valid date and time'


how can i do to force the calculation so it always use the YYYY-MM-DD format ?

Thanks
 
Change the ShortDateFormat variable to 'yyyy-mm-dd'. Remember to restore it afterwards preferably using a try ... finally construct.

Delphi stores the names of the days of the week in LongDayNames so there is no need for the case statement.

Something like this might help:
Code:
var
 savedDateFormat: string;
begin
 savedDateFormat := ShortDateFormat;
 try
  ShortDateFormat := 'yyyy-mm-dd';
  Label1.Caption := LongDayNames [ DayOfWeek ( StrToDate ( MaskEdit1.Text ) ) ];
 finally
  ShortDateFormat := savedDateFormat;
 end;
end;

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top