Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse function of FormatDateTime

Tags:

delphi

I am looking for a function to reverse any string (YYYYMDD,YY/MM/DD,YYMMDD,...) created by the function FormatDateTime to datetime.

example

I have a string-date in format YYYYMMDDcreated by FormatDateTime

mydatestr:=FormatDateTime('YYYYMMDD',Mydate); 

now how I can convert mydatestr to DateTime again?

UPDATE

these functions

function StrToDate(const S: string): TDateTime; overload;
function StrToDate(const S: string;
  const FormatSettings: TFormatSettings): TDateTime; overload;

function StrToDateTime(const S: string): TDateTime; overload;
function StrToDateTime(const S: string;
  const FormatSettings: TFormatSettings): TDateTime; overload;

do not support passing a string with the format to convert.

I am looking something like this

Mydatetime:=InvFormatDatetime('20091225','yyyymmdd');

or

Mydatetime:=InvFormatDatetime('20090108','yyyyddmm');
like image 835
Salvador Avatar asked Mar 23 '10 20:03

Salvador


1 Answers

It is quite easy with existing solution, StrToDateFmt function in rxDateutil.pas unit from RX package, which can be downloaded here: http://sourceforge.net/projects/rxlib/

EDIT:

Mentioned above function and StrToDateFmt from rxDateutil.pas are doing exactly what you expect, converting string to datetime using specified string mask, the code is too large to be included as this unit contains also other date functions, some of them required for converting string to date.

Example of use:

Result := StrToDateFmtDef('MM/DD/YYYY', '11/11/2011', Now);
like image 158
too Avatar answered Oct 12 '22 23:10

too