Delphi:格式化日期时间,不包括午夜的时间

时间:2019-05-13 14:33:14

标签: datetime delphi time format repository

Delphi Seattle:

我想使用此规则(作为DateTimeToStr)在cxGridDBTableView(Devexpress,cxGrid)中格式化日期时间字段:

  • 午夜-00:00:00-时间部分不可见
  • 其他时间-时间部分可见

我记得formatdatetime HH:NN:SS表示时间部分为“每次”。

您知道模拟DateTimeToStr的选项吗? 我想在存储库中使用它。

谢谢!

2 个答案:

答案 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!