在Delphi中FormatDateTime函数的用法

IT教程 4年前 (2020) https://www.55wd.com

formatdatetime

在Delphi中FormatDateTime函数的用法 

function FormatDateTime(const Format: string; DateTime: TDateTime): string; 

Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串,重点来看Format参数中的指令字符:

c以短时间格式显示时间,即全部是数字的表示

FormatdateTime('c',now);

输出为:2004-8-79:55:40

d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位

FormatdateTime('d',now);

输出可能为1~31

dd 和d的意义一样,但它始终是以两位来显示的

FormatdateTime('dd',now);

输出可能为01~31

ddd 显示的是星期几

FormatdateTime('ddd',now);

输出为: 星期六

dddd 和ddd显示的是一样的。

但上面两个如果在其他国家可能不一样。

ddddd 以短时间格式显示年月日 

FormatdateTime('ddddd',now);

输出为:2004-8-7

dddddd 以长时间格式显示年月日

FormatdateTime('dddddd',now); 

输出为:2004年8月7日

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 is2004-8-7 10:26:58

时间中也可以加"-"或"\"来分开日期:

FormatdateTime('"today is" yy-mm-dd',now);

FormatdateTime('"today is" yy\mm\dd',now);

输出为: today is04-08-07

也可以用":"来分开时间 

FormatdateTime('"today is" hh:nn:ss',now);

输出为:today is10: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); {返回: 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('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;

铁路12306怎么查询各铁路局客服中心电话?

手机12306可以查询客服中心的电话,该怎么查询呢额?下面我们就来看看详细的教程。1、打开手机app,点击右下角我的12306按钮。2、在新

在双十一的营销厮杀中,京东到家如何步步为营?

在双十一营销厮杀中,京东是如何突围的?诚如papi酱所言,往年阻扰我过双十一的只有钱,今年还有脑子。预售、定金膨胀金、满减红包、直降

SQL server 数据库中dbo的含义

最近在看SQL server 的存储过程,发现在新建存储过程有这样的格式操作:CREATE PROCEDURE [dbo].[proc_city_4_5], 不知所云何物。 百

Sony Vegas Pro 13.0 简体中文破解安装详细教程

Sony Vegas破解版是一款专业影像编辑软件。挑战 After Effects 。剪辑、特效、合成、Streaming 一气呵成。结合高效率的操作介面

Excel中frequency函数怎么用

你还在为Excel中frequency函数怎么用而苦恼吗,今天小编教你Excel中frequency函数怎么用,让你告别Excel中frequency函数怎么用的烦恼

文章回顾

大家看了本文在Delphi中FormatDateTime函数的用法的精彩教程资源内容,是不是对在Delphi中FormatDateTime函数的用法了解更多,真心希望在Delphi中FormatDateTime函数的用法能帮助到你, 小编会一直给你带来更多教程资源文章信息。

版权声明: 发表于 2020-07-07 15:48:34。

本文由第三方用户分享仅代表作者观点,不代表本网站立场,秉承互联网开放分享的精神,目的在于传递更多信息,加强各行业互通交流,但对内容不作任何保证或承诺,请读者自行参考斟酌。网站发布的信息(包含但不限于版式、图片、字体、文章等素材)由第三方用户分享,版权归原作者所有,本站不承担任何相关的版权纠纷等相关责任。如您认为本篇内容侵犯了您的权益,请与我们联系,我们会及时处理。

豌豆资源网专注分享全网综合资源网站大全,致力于超实用的内容资源搜索。

转载请注明:
本文标题:在Delphi中FormatDateTime函数的用法
本文地址:https://55wd.com/s100698/