Recent

Author Topic: [Solved] ScanDateTime error in Sep  (Read 595 times)

ALAU2007

  • New Member
  • *
  • Posts: 20
[Solved] ScanDateTime error in Sep
« on: June 09, 2025, 04:39:07 am »
I convert string '01 Sep 2025' to date, it shows:

  Can't match any allowed value at pattern position 4, string position 4

The other month is OK, only the Sep has error:

procedure TForm1.Button1Click(Sender: TObject);
var tDate: TDateTime;
begin
  tDate := scandatetime( 'dd mmm yyyy', '01 Jan 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 sep 2025' );  // Error
  ShowMessage( DateToStr( tDate ));
end;

Please comment.
« Last Edit: June 09, 2025, 10:57:25 am by ALAU2007 »

dsiders

  • Hero Member
  • *****
  • Posts: 1647
Re: ScanDateTime error in Sep
« Reply #1 on: June 09, 2025, 05:51:33 am »
I convert string '01 Sep 2025' to date, it shows:

  Can't match any allowed value at pattern position 4, string position 4

The other month is OK, only the Sep has error:

procedure TForm1.Button1Click(Sender: TObject);
var tDate: TDateTime;
begin
  tDate := scandatetime( 'dd mmm yyyy', '01 Jan 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 sep 2025' );  // Error
  ShowMessage( DateToStr( tDate ));
end;

Please comment.

Does you system use English month names in FormatSettings.ShortMonthNames? Cause that's what you told ScanDateTijme() to look for.

paweld

  • Hero Member
  • *****
  • Posts: 1676
Re: ScanDateTime error in Sep
« Reply #2 on: June 09, 2025, 06:41:34 am »
As @dsiders wrote, probably your system's regional settings are not English. You need to define an array of short month names in English
Code: Pascal  [Select][+][-]
  1. uses
  2.   DateUtils;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   s: String;
  7.   dt: TDateTime;
  8.   fs: TFormatSettings;
  9. begin
  10.   fs := DefaultFormatSettings;
  11.   //if your system locale is not in English, then you need to define short month names
  12.   fs.ShortMonthNames[1] := 'Jan';
  13.   fs.ShortMonthNames[2] := 'Feb';
  14.   fs.ShortMonthNames[3] := 'Mar';
  15.   fs.ShortMonthNames[4] := 'Apr';
  16.   fs.ShortMonthNames[5] := 'May';
  17.   fs.ShortMonthNames[6] := 'Jun';
  18.   fs.ShortMonthNames[7] := 'Jul';
  19.   fs.ShortMonthNames[8] := 'Aug';
  20.   fs.ShortMonthNames[9] := 'Sep';
  21.   fs.ShortMonthNames[10] := 'Oct';
  22.   fs.ShortMonthNames[11] := 'Nov';
  23.   fs.ShortMonthNames[12] := 'Dec';
  24.   //
  25.   s := '01 Jan 2025';
  26.   dt := ScanDateTime('d mmm yyyy', s, fs);
  27.   ShowMessage(DateToStr(dt));
  28.   //
  29.   s := '01 sep 2025';
  30.   dt := ScanDateTime('d mmm yyyy', s, fs);
  31.   ShowMessage(DateToStr(dt));
  32. end;  
Best regards / Pozdrawiam
paweld

ALAU2007

  • New Member
  • *
  • Posts: 20
[Solved] Re: ScanDateTime error in Sep
« Reply #3 on: June 09, 2025, 10:56:34 am »
Thanks paweld and dsiders. ::)

After defining the ShortMonthName, It works.  BTW, I have tried the other months is OK, except the "Sep" has the error.  I have tried as

procedure TForm1.Button1Click(Sender: TObject);
var tDate: TDateTime;
begin
  tDate := scandatetime( 'dd mmm yyyy', '01 Oct 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Nov 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Dec 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Aug 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Jul 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Jun 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 May 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Apr 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Mar 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Feb 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 Jan 2025' );
  tDate := scandatetime( 'dd mmm yyyy', '01 sep 2025' );
  tDate := formatdatetime( 'dd mmm yyyy', '01 sep 2025' );   // only it has error when shortMonthNme is not defined.
  ShowMessage( DateToStr( tDate ));
end;

cdbc

  • Hero Member
  • *****
  • Posts: 2858
    • http://www.cdbc.dk
Re: [Solved] ScanDateTime error in Sep
« Reply #4 on: June 09, 2025, 11:45:34 am »
Hi
Just an observation:
- I believe the 'ScanDateTime' function is case-sensitive:
Code: Pascal  [Select][+][-]
  1.   tDate := scandatetime( 'dd mmm yyyy', '01 sep 2025' );
That would mean 'sep' is different from 'Sep'!
I noticed all the other months were with a versal, e.g.: '(J)an' --> not '(j)an'.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

paweld

  • Hero Member
  • *****
  • Posts: 1676
Re: [Solved] ScanDateTime error in Sep
« Reply #5 on: June 09, 2025, 12:05:52 pm »
@cdbc: is not case-sensitive - uses the function: https://docs.freepascal.org/docs-html/rtl/sysutils/ansistrlicomp.html

Edit:
@ALAU2007: you may want to check how you have short month names defined, such as:
Code: Pascal  [Select][+][-]
  1. uses
  2.   DateUtils;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   i: Integer;
  7.   s: String;
  8. begin
  9.   s := '';
  10.   for i := Low(DefaultFormatSettings.ShortMonthNames) to High(DefaultFormatSettings.ShortMonthNames) do
  11.     s := s + Format('%2d -> %s'#13#10, [i, DefaultFormatSettings.ShortMonthNames[i]]);
  12.   ShowMessage(s);
  13. end;  
« Last Edit: June 09, 2025, 12:14:48 pm by paweld »
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018