• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Check date

Does anybody know how I can test if a specific date is valid?

I use the following code to check the date but it does not seem to work correct. When I enter the 29th February 2000 ('29-02-2000') the function says it is an invalid date.

function IsDateOK (DateToTest: String): Boolean;
begin
  try
    StrToDate (DateToTest);
    Result := True;
  except
    Result := False;
  end;
end;

The function should also be able to check the date even if it is another date-order (DD-MM-YYYY or MM-DD-YYYY). Finally it should be able to check the date even if there are different date seperators.

Regards, Micheal
0
mjame
Asked:
mjame
  • 6
  • 3
  • 2
  • +8
1 Solution
 
rwilson032697Commented:
Hmm.. You are going to have to write a specific routine yourself. However, it is unusual to require such a routine to copy with the different orders and formats as date entry is normally with respect to the local settings for date format (which is probably why your date test fails).

Cheers,

Raymond.
0
 
VendiCommented:
There are date/time formatting variables that you can use.  For example:

ShortDateFormat := 'mm/dd/yy';
TestOne := IsDateOk(DateToTest);
ShortDateFormat := 'dd/mm/yy';
TestTwo := IsDateOk(DateToTest);

If TestOne or TestTwo then
    {valid date}



0
 
bryan7Commented:
listenning
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JoeBoothCommented:
Function ChkDate(cStr:string) : boolean;
var
   cOldFormat : string;
begin
   cOldFormat      := ShortDateFormat;
   ShortDateFormat := 'dd-mm-yyy';
   try
     StrToDate(cStr);
     result := true;
   except
     result := false;
   end;
   ShortDateFormat := cOldFormat;
end;
   
0
 
JoeBoothCommented:
P.S.  Make that yyyy instead of yyyy
in my code example, so for the typo
0
 
david_XuCommented:
Date format is very important when you check date valid. please ref Delphi demo -- Masapp. There is a sample to check date just like what you did.
0
 
mjameAuthor Commented:
I can't accept the answer because I couldn't find any date check function.
0
 
JoeBoothCommented:
Did you try my ChkDate function?
0
 
SteveWaiteCommented:
Maybe this is what you need…

I just used

function EncodeDate(Year, Month, Day: Word): TDateTime;  (in sysutils unit)

to do the checking. I put three edits and a button on a fresh project form1.
Then used exception handler to catch the bad dates!
//========
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Year: TEdit;
    Month: TEdit;
    Day: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    EncodeDate(StrToInt(Year.Text), StrToInt(Month.Text), StrToInt(Day.Text));
    ShowMessage('Date OK!');
  except
    ShowMessage('Bad Date!');
  end;
end;

end.
//========
Regards

Steve
0
 
SteveWaiteCommented:
Here, iv'e done a function as well!

function DateOK(Year, Month, Day: Word): Boolean;
begin
  Result := True;
  try
    EncodeDate(Year, Month, Day);
  except
    Result := False;
  end;
end;

0
 
SteveWaiteCommented:
Sorry mjame,

I just noticed the bit at the bottom of your question!
How could a program know if 2-3-2000 is
Feb or March? Does you requirement care?
Steve
0
 
david_XuCommented:
date format can't be random. You only use one format, otherwise you need to write a lot code to check date valid.Just like what you did.
Comment: Please use date component to input date -- It will help you check date valid and also provide calendar to help user choice date.There are a lot of such date entry component, such as Infopower 2000, RxLib...
0
 
SteveWaiteCommented:
Yeh!
Theres a calendar/date picker component in Delphi/components/Samples that demonstrated lots of nice date coding!
0
 
florisbCommented:
Hello,

what I didn't see and what I think you need is:

FormatDateTime('d/m/yyyy', MyDate);
//your test here.

Floris.
0
 
GurkanCommented:
Function IsValidDate(sd:string; sdf:string; ss:char) : boolean;
var
   oldDateFormat : string;
   oldDateSeparator: Char;
begin
  OldDateFormat:= ShortDateFormat;
  OldDateSeparator:= DateSeparator;
  ShortDateFormat := sdf;
  DateSeparator:=ss;
  try
    StrToDate(sd);
    Result := true;
  except
    Result := false;
  end;
  ShortDateFormat := OldDateFormat;
  DateSeparator:= OldDateSeparator;
end;

Separator is also an important parameter that should be considered here. For example it may be '.' , ',' , '/' or anything other from user to user.

This function checks a given datestring for a given format and separator.

Example of usage:
if IsValidDate(Edit1.Text,'dd.mm.yyyy','.') then
ShowMessage('valid') else ShowMessage('invalid');



0
 
gandalf_the_whiteCommented:
you have the following problem:

if the date format is: DD-MM-YYYY
then 13-12-2000 is valid and 12-13-2000 is not

if the format is: MM-DD-YYYY
then 12-13-2000 is valid and 13-12-2000 is not

you'll need more information to process dates the right way
0
 
IammeCommented:
ok.......íf you want to test if a date is valid....
delphi do it for you....there is a component that
that valid that....
it's on
WIN32
and the name is
-----------Tdatetimepicker
if you have problems whit the format of the date
ask me...¿ok?..
and if you want a function that do it ask
me I have one that test all the dates...

(I had a lot of problems with dates previous)..
bye

0
 
SteveWaiteCommented:
That's right! It's the component I spoke about earlier on. If you have the source then it's worth taking a look at it.
0
 
IammeCommented:
mjame...what's up???........did you try with my,,,suggestion............?..
0
 
florisbCommented:
What!?!?!?




0
 
SteveWaiteCommented:
gasp!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 3
  • 2
  • +8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now