views:

96

answers:

1

Is there an equivalent in Delphi? I've looked over the documentation and can't find anything that would give me the output I want.

+8  A: 

kdunlapmo, the DateTime.ToString(“s”) function return an Sortable date/time pattern; conforms to ISO 8601. this pattern is declarated as "yyyy-MM-ddTHH:mm:ss". regardless of the culture, the date must always be returned in the same format. you can use the FormatDateTime function in delphi to format an TDateTime value into a string.

you can use something like this

FormatDateTime('yyyy-mm-dd"T"hh:mm:ss', Now);

but you must be careful because the / character is Substituted by the DateSeparator value and the : character is Substituted by the TimeSeparator value, both variables are depending on the Windows locale configuration. so to avoid problems getting distinct results when the culture changes you must use explicity the - and the : characters in your format string.

FormatDateTime('yyyy"-"mm"-"dd"T"hh":"mm":"ss', Now)

see this sample code

program ProjectTestFormat;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try        
    DateSeparator:='/';
    TimeSeparator:='.';
    //this string is affected by the windows locale configuration
    Writeln(FormatDateTime('yyyy-mm-dd"T"hh:mm:ss', Now));
    //this string is not affected
    Writeln(FormatDateTime('yyyy"-"mm"-"dd"T"hh":"mm":"ss', Now));
    Readln;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

Additionally you can write a function to convert an TDatetime value to the sortable format, see this sample

function GetSortableDatetimeFormat(Value:TDateTime):string;
begin
  Result:=FormatDateTime('yyyy"-"mm"-"dd"T"hh":"mm":"ss', Value);
end;
RRUZ
@RRuz, excellent answer thank you!
kdunlapmo
Indeed, brilliant answer.
Fabricio Araujo