一好百好的意思:Delphi中FormatDateTime函数的用法

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 11:17:12

DelphiFormatDateTime函数的用法
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
Format
参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串,重点来看Format参数中的指令字符:
c以短时间格式显示时间,即全部是数字的表示
FormatdateTime('c',now);
输出为:2004-8-7 9:55:40
d
对应于时间中的日期,日期是一位则显示一位,两位则显示两位
FormatdateTime('d',now);
输出可能为131
dd
d的意义一样,但它始终是以两位来显示的
FormatdateTime('dd',now);
输出可能为0131
ddd
显示的是星期几
FormatdateTime('ddd',now);
输出为: 星期六
dddd ddd显示的是一样的。
但上面两个如果在其他国家可能不一样。
ddddd 以短时间格式显示年月日
FormatdateTime('ddddd',now);
输出为:2004-8-7
dddddd
以长时间格式显示年月日
FormatdateTime('dddddd',now);
输出为:200487
e/ee/eee/eeee 以相应的位数显示年
FormatdateTime('ee',now);
输出为:04 (表示04年)
m/mm/mmm/mmmm 表示月
FormatdateTime('m',now);
输出为:8
FormatdateTime('mm',now);
输出为 08
FormatdateTime('mmm',now);
输出为 八月
FormatdateTime('mmmm',now);
输出为 八月
ddd/dddd 一样,在其他国家可能不同
yy/yyyy 表示年
FormatdateTime('yy',now);
输出为 04
FormatdateTime('yyyy',now);
输出为 2004
h/hh,n/nn,s/ss,z/zzz
分别表示小时,分,秒,毫秒
t 以短时间格式显示时间
FormatdateTime('t',now);
输出为 10:17
tt
以长时间格式显示时间
FormatdateTime('tt',now);
输出为10:18:46
ampm
以长时间格式显示上午还是下午
FormatdateTime('ttampm',now);
输出为:10:22:57上午
如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:
FormatdateTime('"today is" c',now);
输出为:today is 2004-8-7 10:26:58
时间中也可以加"-""\"来分开日期:
FormatdateTime('"today is" yy-mm-dd',now);
FormatdateTime('"today is" yy\mm\dd',now);
输出为: today is 04-08-07
也可以用":"来分开时间
FormatdateTime('"today is" hh:nn:ss',now);
输出为:today is 10:32:23

FormatDateTime
var
s: string;
begin
//FormatDateTime
的参数1 String 格式指令, 参数2 TDateTime 类型的时间

s := FormatDateTime('c', Now); {返回: 2007-12-18 23:56:05}
{
指令 c 表示用短格式显示日期与时间}

s := FormatDateTime('d', Now); {
返回: 19}

s := FormatDateTime('d', StrToDateTime('2008-1-1')); {
返回: 1}
{d
表示日期}

s := FormatDateTime('dd', Now); {
返回: 19}

s := FormatDateTime('dd', StrToDateTime('2008-1-1')); {
返回: 01}
{dd
表示双位日期}

s := FormatDateTime('ddd', Now); {
返回: 星期三}

s := FormatDateTime('dddd', Now); {
返回: 星期三}
{ddd
dddd 表示星期; 可能对不同的语种会有区别}

s := FormatDateTime('ddddd', Now); {
返回: 2007-12-19}
{ddddd
五个 d 表示短格式日期}

s := FormatDateTime('dddddd', Now); {
返回: 20071219}
{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('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('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;