Delphi Seattle:
我想使用此规则(作为DateTimeToStr)在cxGridDBTableView(Devexpress,cxGrid)中格式化日期时间字段:
我记得formatdatetime HH:NN:SS表示时间部分为“每次”。
您知道模拟DateTimeToStr的选项吗? 我想在存储库中使用它。
谢谢!
答案 0 :(得分:6)
在字段DisplayFormat
中使用“ c”。文档指出:
使用ShortDateFormat全局给定格式的日期 变量,然后使用由 LongTimeFormat全局变量。如果 DateTime值的小数部分为零。
答案 1 :(得分:-2)
感谢雷米·勒博和乌韦·拉贝,我举了以下例子:
function GridDateTime: string;
var
dt: TDateTime;
fs: TFormatSettings;
begin
dt := Now;
fs := TFormatSettings.Create;
fs.LongTimeFormat := 'HH:NN:SS';
result := DateTimeToStr(dt, fs);
end;
在与@UweRaabe另一个答案相关的评论中,他们讨论了格式字符串'c'
以及它是否适用于FormatDateTime()
或DateTimeToStr()
。
答案是:两者都适用!
这两个函数在内部调用DateTimeToString()
,后者具有字符串参数Format
。使用FormatDateTime()
可以直接进行设置,而不能使用DateTimeToString()
进行设置。在那里设置为空。那么,DateTimeToString()
是一个空字符串时,Format
是什么呢?它添加了一个“ C”:
如果格式为<>'',则为AppendFormat(Pointer(Format))否则为AppendFormat('C');
因此,是否使用'C'
作为格式字符串都没有关系。
注意:基于Delphi 10.2.3 Tokyo!