在Delphi中使用FormatDateTime函数
函数格式数据时间(常数格式:字符串; datetime : tdatetime (:字符串;
格式参数是格式化的字符串。 DateTime是时间类型。 返回值是一个格式化字符串,聚焦于格式参数中的命令字符。
c以短时间格式显示时间。 也就是说,都是数字的显示
格式日期(' c ',now );
输出为2004-8-79:55:40
d对应于时间中的日期,日期为1位时显示1位,2位时显示2位
格式日期(d )、新);
输出可能是1到31
dd与d的含义相同,但始终显示为两位数
格式日期(DD )、新);
输出可能是01~31
ddd显示星期几
格式日期(滴滴涕)、新);
输出为:星期六
ddd和ddd表示相同的东西。
但是,上面的两个在其他国家可能不同。
滴滴涕在短时间内显示年月日
格式日期(滴滴涕)、新;
输出为2004-8-7
dddddd以长时间格式显示年月日
格式日期(滴滴涕)、新;
输出时间为2004年8月7日
e/ee/eee/eeee以相应的位数显示年份
格式日期(ee )、新);
输出为04 (表示04年) )。
m/mm/mmm/mmmm表示月份
格式日期(' m ',now );
输出: 8
格式日期(' mm ',now );
输出为08
格式日期(' mmm ',now );
输出是8月
格式日期(' mmmm ',now );
输出是8月
和滴滴涕/滴滴涕一样,其他国家可能不同
yy/yyyy表示年份
格式日期(YY )、新);
输出为04
格式日期(' yyyy ',now );
输出为2004
h/hh、n/nn、s/ss和z/zzz分别表示时间、分钟、秒和毫秒
t以短时间形式表示时间
格式日期(' t ',now );
输出为10:17
tt以长时间格式显示时间
格式日期(' TT ',now );
输出为10:18:46
ampm是以长时间格式显示上午还是下午
格式日期(TT ampm )、新);
输出为10:22:57上午
向格式中添加常规字符串时,可以用双引号分隔特定字符。 这样,如果常规字符串包含特殊字符,它将不再以时间格式显示。
formatdatetime(''Todayis'c ',now );
输出为today is2004-8-7 10:26:58
也可以在时间中添加“-”或“”来分隔日期。
formatdatetime (' ' today is ' YY-mm-DD ',now );
formatdatetime (' today is ' YYmmDD ',now );
输出为today is04-08-07
也可以用“:”来划分时间
formatdatetime (' ' today is ' hh : nn : ss ',now );
输出为today is10:32:23
格式日期时间
var
s:斯特林;
比根
//FormatDateTime参数1是字符串形式的命令,参数2是TDateTime类型的时间
s:=formatdatetime('c ',Now ); 返回{:2007-12-182:56:05}
{命令c表示以短格式显示日期和时间}
s :=格式数据时间(d )、否); 返回: 19}
s :=格式数据时间(d )、strtodatetime (2008-1-1 ) ); 返回{:
{d表示日期}
s:=formatdatetime('DD ',Now ); 返回: 19}
s :=格式数据时间(DD )、strtodatetime (2008-1-1 ) ); 返回: 01}
{dd表示2位的日期}
s :=格式数据时间(' DDD ',Now );星期三}
r> s := FormatDateTime('dddd', Now); {返回: 星期三}{ddd 与 dddd 表示星期; 可能对不同的语种会有区别}
s := FormatDateTime('ddddd', Now); {返回: 2007-12-19}
{ddddd 五个 d 表示短格式日期}
s := FormatDateTime('dddddd', Now); {返回: 2007年12月19日}
{dddddd 六个 d 表示长格式日期}
s := FormatDateTime('e', Now); {返回: 7}
{e 表示年, 1位}
s := FormatDateTime('ee', Now); {返回: 07}
{ee 表示年, 2位}
s := FormatDateTime('eee', Now); {返回: 2007}
s := FormatDateTime('eeee', Now); {返回: 2007}
{eee 与 eeee 返回4位数年}
s := FormatDateTime('m', Now); {返回: 12}
{m 表示月, 1位}
s := FormatDateTime('mm', StrToDateTime('2008-1-1')); {返回: 01}
{mm 表示月, 2位}
s := FormatDateTime('mmm', Now); {返回: 十二月}
s := FormatDateTime('mmmm', Now); {返回: 十二月}
{mmm 与 mmmm 表示长格式月}
s := FormatDateTime('y', Now); {返回: 07}
s := FormatDateTime('yy', Now); {返回: 07}
s := FormatDateTime('yyy', Now); {返回: 2007}
s := FormatDateTime('yyyy', Now); {返回: 2007}
{y yy yyy yyyy 表示年; 和 e 略有不同}
s := FormatDateTime('t', Now); {返回: 0:21}
s := FormatDateTime('tt', Now); {返回: 0:22:13}
{t tt 表示时间}
s := FormatDateTime('ampm', Now); {返回: 上午}
s := FormatDateTime('tampm', Now); {返回: 0:24 上午}
{ampm 表示上午、下午}
s := FormatDateTime('h', StrToDateTime('2007-12-30 9:58:06')); {返回: 9}
s := FormatDateTime('hh', StrToDateTime('2007-12-30 9:58:06')); {返回: 09}
{h hh 表示时}
s := FormatDateTime('n', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('nn', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
{n nn 表示分}
s := FormatDateTime('s', StrToDateTime('2007-12-30 9:58:06')); {返回: 6}
s := FormatDateTime('ss', StrToDateTime('2007-12-30 9:58:06')); {返回: 06}
{s ss 表示秒}
s := FormatDateTime('z', Now); {返回: 24}
s := FormatDateTime('zz', Now); {返回: 524}
s := FormatDateTime('zzz', Now); {返回: 524}
{z zz zzz 表示毫秒}
s := FormatDateTime('yymmdd', Now); {返回: 071219}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy-mm-dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy*mm*dd', Now); {返回: 07*12*19}
{使用分隔符, - 是默认的, / 是与 - 等效的, 假如我非要用 / 显示呢?}
s := FormatDateTime('yy"/"mm"/"dd', Now); {返回: 07/12/19}
s := FormatDateTime('"当前时间是: "yyyy-m-d h:n:s:zz', Now);
{返回: 当前时间是: 2007-12-19 0:47:16:576}
{混入的字符串要包含在双引号中}
ShowMessage(s);
end;