Recent

Author Topic: [solved] StrToDate - why not?  (Read 383 times)

Nicole

  • Hero Member
  • *****
  • Posts: 638
[solved] StrToDate - why not?
« on: November 26, 2022, 02:11:38 pm »
Here comes a painful thing. Painful, because it takes about 15 minutes of processing until I see this error message

[Content]
"29/04/1983" is not a valid date format.

Press OK to ignore and risk data corruption.
Press Abort to kill the program.

[OK] [Abort]

Can anybody give me a hint, why this date format is not valid?
« Last Edit: November 26, 2022, 03:46:27 pm by Nicole »

wp

  • Hero Member
  • *****
  • Posts: 10497
Re: StrToDate - why not?
« Reply #1 on: November 26, 2022, 02:18:31 pm »
What are your FormatSettings?

This works for me, in Germany where FormatSettings.DateSeparator = '.' (and it should work everywhere):
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5. var
  6.   s: String;
  7.   d: TDate;
  8. begin
  9.   s := '29/04/1983';
  10.   FormatSettings.ShortDateFormat := 'dd/mm/yyyy';
  11.   FormatSettings.DateSeparator := '/';   // remove this line and you'll have the error when the default DateSeparor is not a '/'
  12.   d := StrToDate(s);
  13.   WriteLn(s, ' --> ' , DateToStr(d));
  14.  
  15.   ReadLn;
  16.  
  17. end.
« Last Edit: November 26, 2022, 02:25:57 pm by wp »

Nicole

  • Hero Member
  • *****
  • Posts: 638
Re: StrToDate - why not?
« Reply #2 on: November 26, 2022, 02:22:32 pm »
I just wrote
myDate:=StrToDate(s);   

The format is a thing, I can configure.
I thought Delphi would understand dd/mm/yyyy

All formats are possible.

winni

  • Hero Member
  • *****
  • Posts: 3195
Re: StrToDate - why not?
« Reply #3 on: November 26, 2022, 02:24:25 pm »
Hi!

"29/04/1983" is british

"29.04.1983" is german

"04/29/1983" is US

"1983-04-29" is ISO

tbc ...

Winni

wp

  • Hero Member
  • *****
  • Posts: 10497
Re: StrToDate - why not?
« Reply #4 on: November 26, 2022, 02:34:33 pm »
I thought Delphi would understand dd/mm/yyyy
It's the same as with Lazarus - I just checked it.

It depends on what is listed in the FormatSettings. "dd/mm/yyyy" defines mainly one thing: the order of day-month-year elements (and the number of digits to be used for day/month/year, but this is ignored in the conversion from string to date). The other important thing is the date separator. When the format string is parsed, the '/' is replaced by the date separator. In Germany, the date separator is '.', therefore Lazarus (as well as Delphi) expects a '.' between day and month and between month and year - but you give it a slash. This will fail in all countries in which the date sepator is not a slash.

Only when you specify the date separator to be a '.' it will work.

Nicole

  • Hero Member
  • *****
  • Posts: 638
[solved] Re: StrToDate - why not?
« Reply #5 on: November 26, 2022, 03:46:12 pm »
Thank you all for the answers.
Indeed: The software, which is the source of the text-generation, - it is from the USA.

I was glad, that I could bring it to  dd / mm / yyyy instead from mm / dd / yyyy, which I never can get used to.
So I will check, if I can change the separator or even better, try to define it, which looks more robust.

Thaddy

  • Hero Member
  • *****
  • Posts: 12642
Re: [solved] StrToDate - why not?
« Reply #6 on: November 26, 2022, 04:23:17 pm »
The programmer's way of handling dates is year first, month second, day third and then time. And all in UTC.
@Nicole : That is the native representation in - almost - all programming languages. What you see on screen is a formatting representation depending on natural language and should never be how it is stored. Homework: How does TDateTime work?. Should be comprehensible in under 10 minutes. Representation should be handled through TFormatSettings.
« Last Edit: November 26, 2022, 04:29:57 pm by Thaddy »
"Looks" OK, doesn't mean it is IS OK.

Nicole

  • Hero Member
  • *****
  • Posts: 638
Re: [solved] StrToDate - why not?
« Reply #7 on: November 26, 2022, 04:54:28 pm »
FormatSettings.DateSeparator := '/';
solves it.

It is a format, which contains just the date.

Thaddy

  • Hero Member
  • *****
  • Posts: 12642
Re: [solved] StrToDate - why not?
« Reply #8 on: November 26, 2022, 06:18:50 pm »
We almost all know, but I am afraid you still not know date and time handling in programming.
"Looks" OK, doesn't mean it is IS OK.

 

TinyPortal © 2005-2018