[fpc-pascal] Ho to convert a special format of datetime?

Marco van de Voort fpc at pascalprogramming.org
Thu Dec 13 21:27:18 CET 2018


Op 2018-12-13 om 21:07 schreef luciano de souza:
> Hello all,
> I'd like to convert this date "2017-01-11T17:47:22.2912317-02:00" to TDatetime.

A quick attempt with some standard functions, note that it only parses 
till millisecond precision, the rest (317) is ignored.

uses dateutils,sysutils,strutils;
const teststring = '2017-01-11T17:47:22.2912317-02:00';
var dt : TDatetime;
   myYear, myMonth, myDay : Word;
   myHour, myMin, mySec, myMilli : Word;
   s3:string;
   i : integer;
begin
   i:=rpos('-',teststring);
   s3:=copy(teststring,1,i-1);


   dt:=scandatetime('yyyy-mm-dd?hh:nn:ss.zzz????',s3);
  writeln(dt);
  decodedatetime(dt,myYear, myMonth, myDay,
                  myHour, myMin, mySec, myMilli);

   writeln('myHour     = '+IntToStr(myHour));
   writeln('myMin      = '+IntToStr(myMin));
   writeln('mySec      = '+IntToStr(mySec));
   writeln('myMilli    = '+IntToStr(myMilli));
   writeln('myDay      = '+IntToStr(myDay));
   writeln('myMonth    = '+IntToStr(myMonth));
   writeln('myYear     = '+IntToStr(myYear));
   s3:=copy(teststring,i+1,length(teststring)-i);

   // parse timezone info
   dt:=scandatetime('hh:nn',s3);
   decodetime(dt,
                  myHour, myMin, mySec, myMilli);
   writeln('tzmyHour     = '+IntToStr(myHour));
   writeln('tzmyMin      = '+IntToStr(myMin));
end.





More information about the fpc-pascal mailing list