Link to home
Start Free TrialLog in
Avatar of Jeb
Jeb

asked on

TDateTime and StrToDate problem(Convertionerror)

I got a problem with the TDateTime type !!
It works fine with most Dateformats but
When windows date is set to dd-mmm-yy (09-nov-98) the
function StrToDate or DateToStr doesn't work !!
I get an EConvertionError exception and a '09-nov-98' is not
a valid date !! Is there another way to convert between String and TDateTime ??
Avatar of Stuart_Johnson
Stuart_Johnson

Try setting this in your FormCreate or FormShow procedures:

   ShortDateFormat := 'DD/MM/YYYY';

It _should_ convert your dates over to this format on in Delphi.

Hope it helps,

Stuart.
ShortDateFormat does not work all the time. I have had problems with it working on all machines.
Sounds like a very strange problem.  We write software using D1, D2 and D4 which is shipped around the globe, and never have we encountered a problem like this.

Stu
hi, jeb. hi, all.

i agree: there are certain difficulties with StrToDate / DateToStr.
at least you should make sure "DateSeparator" is set properly.
but even with this i can't get a conversion from an input like '09-nov-98' working.
all i can manage is to convert '09-11-98'.

i think the problem is indeed in ShortDateFormat. StrToDate determines the sequence of day, month and year on ShortDateFormat.
and ShortDateFormat only accepts d, dd, m, mm, yy, yyyy.
mmm is not included and thus, i'm afraid, you can't use ShortMonthNames for input to conversion from String to TDateTime.

or am i wrong with this?

alas, no big help.

Black Death.

How about creating your own StrToDate and DateToStr routine:
function MyDateToStr(V: TDateTime): ShortString;
var d,m,y: Integer;
begin
   DecodeDate(V,d,m,y);
   if D<10 then Result:='0';
   Result:=Result + intToStr(d);
   case M of
   1: Result:=Result + 'Jan';
   2:...
   12:...
   end;
   Result:=Result+IntToStr(y);
end;

function MyStrToDate(V: String, Divider: string[1]): TDateTime;
begin
   ...
end;
Why do you not use FormatDateTime function?
That seems to me that the FormatDateTime would be a good solution. As long as the date has the four digit year. If you only have the 2 digit year you lose some Y2K compatability.

One question is: Are you using the datetostr and strtodate for display purposes or for storing in a table?
ASKER CERTIFIED SOLUTION
Avatar of slautin
slautin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeb

ASKER

ShortDateFormat :='dd/mm/yy' worked !!