Search code examples
delphitwitterdelphi-2009formatdatetime

delphi how to convert twitter timestamp to TDatetime


In continue to this question: How to Convert Twitter Timestamp to DateTime?

what is the code to convert twitter date time stamp to TDateTime?

edit: StrDateTime(const string;TFormatSettings);

could handle some of it, now only to figure out how to intoduce new format.


Solution

  • Since we don't have the ParseExact function, you need to parse the components of the timestamp positionally. You could do it with the Copy() function. ex:

    TheMonthAsString := Copy(TwitterDate,5,3);
    TheDayAsString := Copy(TwitterDate,9,2);
    etc..
    

    Convert those pieces to Integers, and then you can use EncodeDateTime (in the DateUtils unit) (Thanks Jens!) to generate a TDateTime.

    Summary: Pick the string apart into the individual components of the timestamp, and convert that to a TDateTime using EncodeDateTime or StrToDateTime.