Delphi: 在午夜格式化没有时间部分的日期时间
Delphi: format datetime without time part in midnight
Delphi西雅图:
我想使用此规则(如 DateTimeToStr)格式化 cxGridDBTableView(Devexpress、cxGrid)中的日期时间字段:
- 午夜 - 00:00:00 - 时间部分不可见
- 其他时间 - 时间部分可见
我记得格式日期时间 HH:NN:SS 显示时间部分 "everytime"。
您知道模拟 DateTimeToStr 的选项吗?
我想在存储库中使用它。
谢谢!
感谢 Remy Lebeau 和 Uwe Raabe,我制作了以下示例:
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()
则不行。在那里它被设置为空。那么当 Format
是一个空字符串时,DateTimeToString()
是什么意思呢?它添加了一个 'C':
if Format <> '' then AppendFormat(Pointer(Format)) else AppendFormat('C');
因此,是否使用 'C'
作为格式字符串并不重要。
注意: 基于 Delphi 10.2.3 东京!
对字段的 DisplayFormat
使用 "c"。文档指出:
The date using the format given by the ShortDateFormat global
variable, followed by the time using the format given by the
LongTimeFormat global variable. The time is not displayed if the
fractional part of the DateTime value is zero.
Delphi西雅图:
我想使用此规则(如 DateTimeToStr)格式化 cxGridDBTableView(Devexpress、cxGrid)中的日期时间字段:
- 午夜 - 00:00:00 - 时间部分不可见
- 其他时间 - 时间部分可见
我记得格式日期时间 HH:NN:SS 显示时间部分 "everytime"。
您知道模拟 DateTimeToStr 的选项吗? 我想在存储库中使用它。
谢谢!
感谢 Remy Lebeau 和 Uwe Raabe,我制作了以下示例:
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()
则不行。在那里它被设置为空。那么当 Format
是一个空字符串时,DateTimeToString()
是什么意思呢?它添加了一个 'C':
if Format <> '' then AppendFormat(Pointer(Format)) else AppendFormat('C');
因此,是否使用 'C'
作为格式字符串并不重要。
注意: 基于 Delphi 10.2.3 东京!
对字段的 DisplayFormat
使用 "c"。文档指出:
The date using the format given by the ShortDateFormat global variable, followed by the time using the format given by the LongTimeFormat global variable. The time is not displayed if the fractional part of the DateTime value is zero.