Recent

Author Topic: TryStrToDateTime is this totally useless?  (Read 457 times)

BlackVoid

  • Newbie
  • Posts: 1
TryStrToDateTime is this totally useless?
« on: May 25, 2025, 05:12:14 pm »
FormatStr: 'yyyymmdd'; DateSeparator: #0; TimeSeparator: #0; IsDateTime: False

I call it with 20250525 and it fails (???).

I ended up using AI to write a totally new function that actually works.

Thaddy

  • Hero Member
  • *****
  • Posts: 17449
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: TryStrToDateTime is this totally useless?
« Reply #1 on: May 25, 2025, 05:26:20 pm »
If it "does not work" it is more likely you made an error, since TryStrToDateTime <> FormatStr...
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: TryStrToDateTime is this totally useless?
« Reply #2 on: May 25, 2025, 05:37:37 pm »
It appears that a separator is required

Sieben

  • Sr. Member
  • ****
  • Posts: 375
Re: TryStrToDateTime is this totally useless?
« Reply #3 on: May 25, 2025, 05:48:39 pm »
TryISOStrTo* functions from unit DateUtils should work.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

tetrastes

  • Hero Member
  • *****
  • Posts: 666
Re: TryStrToDateTime is this totally useless?
« Reply #4 on: May 25, 2025, 05:50:51 pm »
FormatStr: 'yyyymmdd'; DateSeparator: #0; TimeSeparator: #0; IsDateTime: False

I call it with 20250525 and it fails (???).

Of course it fails, as with your settings it will work with '2025'#0'05'#0'25'. #0 does not mean nothing. You have to learn pascal strings.
And from https://www.freepascal.org/docs-html/current/rtl/sysutils/trystrtodatetime.html:
Quote
If only one number is given, it is supposed to represent the day of the current month
So what do you think is your 20250525?

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: TryStrToDateTime is this totally useless?
« Reply #5 on: May 25, 2025, 06:04:02 pm »
So what do you think is your 20250525?
It is a valid (scientific) date representation, but it may need preformatting, which can be easily done with LeftStr/MidStr/RightStr if the representations are consistently 8 digits. Or use Copy() directly:

Code: Pascal  [Select][+][-]
  1. DateStr := '20250525';
  2.  
  3. Year := StrToInt(Copy(DateStr, 1, 4));
  4. Month := StrToInt(Copy(DateStr, 5, 2));
  5. Day := StrToInt(Copy(DateStr, 7, 2));
  6.  
  7. ParsedDate := EncodeDate(Year, Month, Day);
  8. Writeln('Parsed Date: ', DateToStr(ParsedDate));
  9. Writeln('Formatted Date: ', FormatDateTime('yyyy-mm-dd', ParsedDate));
« Last Edit: May 25, 2025, 06:09:21 pm by munair »
It's only logical.

tetrastes

  • Hero Member
  • *****
  • Posts: 666
Re: TryStrToDateTime is this totally useless?
« Reply #6 on: May 25, 2025, 06:21:37 pm »
So what do you think is your 20250525?
It is a valid (scientific) date representation

Not for TryStrToDateTime with OP's Format settings. Note "your 20250525".

Handoko

  • Hero Member
  • *****
  • Posts: 5458
  • My goal: build my own game engine using Lazarus
Re: TryStrToDateTime is this totally useless?
« Reply #7 on: May 25, 2025, 07:09:46 pm »
I ended up using AI to write a totally new function that actually works.

You don't need to write a new function. TryStrToDate is working correctly. If you ask question in the here, you will learn more.

Below I write a demo following tetrastes suggestion:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. var
  31.   FormatStr:  TFormatSettings;
  32.   DateTime:   TDateTime;
  33. begin
  34.  
  35.   Memo1.Clear;
  36.   FormatStr.DateSeparator   := #0;
  37.   FormatStr.ShortDateFormat := 'yyyymmdd';
  38.  
  39.   Memo1.Append('Try with '''+'20250525'+'''');
  40.   case TryStrToDate('20250525', DateTime, FormatStr) of
  41.     True:  Memo1.Append('Correct');
  42.     False: Memo1.Append('Incorrect');
  43.   end;
  44.  
  45.   Memo1.Append(LineEnding);
  46.   Memo1.Append('Try with ''2025''#0''05''#0''25''');
  47.   case TryStrToDate('2025'#0'05'#0'25', DateTime, FormatStr) of
  48.     True:  Memo1.Append('Correct');
  49.     False: Memo1.Append('Incorrect');
  50.   end;
  51.  
  52.  
  53. end;
  54.  
  55. end.

wp

  • Hero Member
  • *****
  • Posts: 12911
Re: TryStrToDateTime is this totally useless?
« Reply #8 on: May 25, 2025, 07:43:23 pm »
TryStrToDate uses the ShortDateFormat elements of the FormatSettings record to determine which parts of the date string correspond to day, month and year. And it uses the FormatSetting's DateSeparator to separate the date parts - there is no "no DateSeparator" setting, just counting the 'y', 'm' and 'd' characters in the format mask does not work...

A more flexible way to convert string to date is the function ScanDateTime in the DateUtils unit:
Code: Pascal  [Select][+][-]
  1. var
  2.   d: TDate;
  3. ...
  4.   d := ScanDateTime('yyyymmdd', '20250525');
  5.  

 

TinyPortal © 2005-2018