[英]Delphi equivalent of .net's DateTime.ToString(“s”) (DateTime Sortable)
Is there an equivalent in Delphi? Delphi中有与之等效的东西吗? I've looked over the documentation and can't find anything that would give me the output I want. 我查看了文档,找不到任何可以提供所需输出的内容。
kdunlapmo, the DateTime.ToString(“s”) function return an Sortable date/time pattern; kdunlapmo, DateTime.ToString(“ s”)函数返回可排序的日期/时间模式; conforms to ISO 8601. this pattern is declarated as "yyyy-MM-ddTHH:mm:ss"
. 符合ISO8601。此模式声明为"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. 您可以在delphi中使用FormatDateTime函数将TDateTime值格式化为字符串。
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. 但您必须小心,因为-
字符由DateSeparator值替代,而:
字符由TimeSeparator值替代,两个变量均取决于Windows区域设置。 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 此外,您可以编写将TDatetime值转换为可排序格式的函数,请参见此示例
function GetSortableDatetimeFormat(Value:TDateTime):string;
begin
Result:=FormatDateTime('yyyy"-"mm"-"dd"T"hh":"mm":"ss', Value);
end;
Little bugfix - right is: 小错误修正-正确的是:
Result:=FormatDateTime('yyyy"-"mm"-"dd"T"hh":"nn":"ss', Value); 结果:= FormatDateTime('yyyy“-” mm“-” dd“ T” hh“:” nn“:” ss',Value);
(minutes is "nn", not "mm") (分钟是“ nn”,不是“ mm”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.