1、FormatDateTime 格式化日期
1.1 原型:

function FormatDateTime(const Format: string; DateTime: TDateTime): string;
begin
  DateTimeToString(Result, Format, DateTime);
function FormatDateTime(const Format: string; DateTime: TDateTime;
  const FormatSettings: TFormatSettings): string;
begin
  DateTimeToString(Result, Format, DateTime, FormatSettings);

1.2 Format 参数介绍:

  • c 以短时间格式显示时间,即全部是数字的表示
  • FormatdateTime('c',now);  //2020-11-6 9:55:40
  • d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位
  • FormatdateTime('d',now);  //1~31
  • dd 和d的意义一样,但它始终是以两位来显示的
  • FormatdateTime('dd',now);  //01~31
  • ddd 显示的是星期几
  • FormatdateTime('ddd',now);  //星期一
    FormatdateTime('m',now);   //9
    FormatdateTime('mm',now);  //09
    FormatdateTime('mmm',now); //9月
    FormatdateTime('mmmm',now); //9月
  • yy/yyyy 表示年
  • FormatdateTime('yy',now);  //20
    FormatdateTime('yyyy',now);  //2020
  • 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上午

    1.3 加普通的字符串
    可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式:

    FormatdateTime('"today is" c',now);   //today is 2020-11-6 10:26:58

    1.4 加"-"或"\"来分开日期:

    FormatdateTime('"today is" yy-mm-dd',now);   //today is 20-11-06
    FormatdateTime('"today is" yy\mm\dd',now);   //today is 20\11\06

    1.5 用":"来分开时间 

    FormatdateTime('"today is" hh:nn:ss',now);   //today is 10:32:23 

    1.6 Delphi 示例:

    procedure FormatDateTimes;
      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);
    

    2、FormatFloat 格式化浮动值

    2.1 原型:

    function FormatFloat(const Format: string; Value: Extended): string;
      Buffer: array[0..255] of Char;
    begin
      if Length(Format) > SizeOf(Buffer) - 32 then ConvertError(@SFormatTooLong);
      SetString(Result, Buffer, FloatToTextFmt(Buffer, Value, fvExtended,
        PChar(Format)));
    function FormatFloat(const Format: string; Value: Extended;
      const FormatSettings: TFormatSettings): string;
      Buffer: array[0..255] of Char;
    begin
      if Length(Format) > SizeOf(Buffer) - 32 then ConvertError(@SFormatTooLong);
      SetString(Result, Buffer, FloatToTextFmt(Buffer, Value, fvExtended,
        PChar(Format), FormatSettings));
    

    Extended类型是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。

    2.2 参数:

  • 0 指定相应的位数的指令。
  • FormatFloat('000.000',33.33);   //033.330
    FormatFloat('0.00E+00',3333.33);     //3.33E+03
    FormatFloat('0000.00E+00',3333.33);  //3333.33E+00
    FormatFloat('00.0E+0',3333.33);     //33.3E+3