views:

228

answers:

2

Can someone tell me what's wrong with this piece of code:

ShortDateFormat := 'dd/mm/yyyy';
j:=StrToDate('05/05/1999');

I keep getting

An unhandled exception occurred at $000000000042FA33 :
EConvertError : Invalid date format

I'm using fpc.

+3  A: 

From here (paraphrased):

StrToDate does not use ShortDateFormat to check the actual format; it uses it only to determine the order of y,m,d and then uses DateSeparator to determine the actual date separator character.

So what you need is:

ShortDateFormat := 'd/m/y';
DateSeparator := '/';
j:=StrToDate('05/05/1999');

You may want to think about either:

  • saving ShortDateFormat and DateSeparator before doing this so you can restore them (they're set initially based on your locale); or
  • using dates based on your actual settings, and not change those two values at all.
paxdiablo
+1  A: 

Insert this in your code before the StrToDate call:

Writeln(DateTimeToStr(Date));

If the output does not contain / but another character, you must use this character as separator in your string.

On my system this gives: 11.11.2009. If I then use 05.05.1999it does work.

Gerd Klima