Recent

Author Topic: Parse uncommon format to datetime  (Read 265 times)

Mo0211

  • Newbie
  • Posts: 4
Parse uncommon format to datetime
« on: March 19, 2025, 07:27:21 am »
Hey,

i'm trying to parse the format 'June 3, 2024' to a datetime-object.
What is not clear to me, how to set the formatsettings correct in this case.
What is the datetimeseperator for example?
Which settings are really needed?

I hope someone can give me a small hint.

Thanks and have a good day!


TRon

  • Hero Member
  • *****
  • Posts: 4328
Re: Parse uncommon format to datetime
« Reply #1 on: March 19, 2025, 08:26:17 am »
In case locale of the machine on which code is running is same as that from code:
Code: Pascal  [Select][+][-]
  1. program test;
  2. uses
  3.   sysutils,dateutils;
  4. var
  5.   dt:TDateTime;
  6. begin
  7.   dt:=ScanDateTime('mmmm" "d", "yyyy', 'June 3, 2024');
  8.   writeln('date : ', FormatDateTime('yyyy"-"mm"-"dd', dt));
  9. end.
  10.  
Today is tomorrow's yesterday.

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: Parse uncommon format to datetime
« Reply #2 on: March 19, 2025, 08:36:27 am »
If everything else fails....
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Uses SysUtils, Regexpr;
  3. Var r:Tregexpr;
  4.     b:Boolean;
  5.     i:Integer;
  6. begin
  7.   r:=TRegExpr.Create;
  8.   r.ModifierI:=True;
  9.   r.ModifierG:=True;
  10.   r.Expression:='(\D+)\s*(\d{1,2})\s*,\s*(\d{4})';
  11.   r.InputString:='June 6, 2025';
  12.   b:=r.exec;
  13.   If b Then
  14.     Begin
  15.       //Index 0 is the Match itself. From Index 1 up are the capturing groups
  16.       For i:=1 To r.SubExprMatchCount Do Writeln(r.Match[i]);
  17.     end;
  18.   Readln;
  19.   r.free;
  20. end.
  21.  

Still your job to THEN check if it's a valid Date.
If you have to run that thing through a loop, then you should probably use "Compile"-Function of Regex to compile everything beforehand, and in the loop just change InputString incl. Exec
« Last Edit: March 19, 2025, 08:39:14 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Mo0211

  • Newbie
  • Posts: 4
Re: Parse uncommon format to datetime
« Reply #3 on: March 19, 2025, 12:10:18 pm »
Hey,

thank you for your answers!
I will check them and let you know :)

 

TinyPortal © 2005-2018