Recent

Author Topic: Converting Month Day, Year String to Datetime Object  (Read 301 times)

cacikmagic

  • Newbie
  • Posts: 1
Converting Month Day, Year String to Datetime Object
« on: May 26, 2025, 02:06:55 pm »
Hello. I need to convert a date string in the format 'June 3, 2024' into a datetime object. I'm unsure about the correct format specifiers and necessary settings to achieve this. For instance, what should I use as the datetime separator, and which format settings are truly essential?

« Last Edit: June 09, 2025, 08:35:49 am by cacikmagic »

paweld

  • Hero Member
  • *****
  • Posts: 1435
Re: Converting Month Day, Year String to Datetime Object
« Reply #1 on: May 26, 2025, 02:38:16 pm »
function ScanDateTime from DateUtils unit:
Code: Pascal  [Select][+][-]
  1. uses
  2.   DateUtils;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   s: String;
  7.   dt: TDateTime;
  8.   fs: TFormatSettings;
  9. begin
  10.   fs := DefaultFormatSettings;
  11.   //if your system is not in English, then you need to define long month names
  12.   fs.LongMonthNames[1] := 'January';
  13.   fs.LongMonthNames[2] := 'February';
  14.   fs.LongMonthNames[3] := 'March';
  15.   fs.LongMonthNames[4] := 'April';
  16.   fs.LongMonthNames[5] := 'May';
  17.   fs.LongMonthNames[6] := 'June';
  18.   fs.LongMonthNames[7] := 'July';
  19.   fs.LongMonthNames[8] := 'August';
  20.   fs.LongMonthNames[9] := 'September';
  21.   fs.LongMonthNames[10] := 'October';
  22.   fs.LongMonthNames[11] := 'November';
  23.   fs.LongMonthNames[12] := 'December';
  24.   //
  25.   s := 'June 3, 2024';
  26.   dt := ScanDateTime('mmmm d, yyyy', s, fs);
  27.   ShowMessage(DateToStr(dt));
  28. end;    
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018