Delphi的时间与字符串函数代码示例

  1. SysUtils.StrToDate();  
  2. SysUtils.StrToDateDef();  
  3. SysUtils.TryStrToDate();  
  4.   
  5. SysUtils.StrToTime();  
  6. SysUtils.StrToTimeDef();  
  7. SysUtils.TryStrToTime();  
  8.   
  9. SysUtils.StrToDateTime();  
  10. SysUtils.StrToDateTimeDef();  
  11. SysUtils.TryStrToDateTime();  
  12.   
  13. SysUtils.DateToStr();  
  14. SysUtils.TimeToStr();  
  15. SysUtils.DateTimeToStr();  
  16. SysUtils.DateTimeToString();  
  17.   
  18. SysUtils.FormatDateTime();  
  19. SysUtils.FormatDateTime(); 函数参见:  
  20.  unit Unit1;  
  21. interface  
  22. uses  
  23.  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  24.  Dialogs;  
  25.   
  26. type  
  27.  TForm1 = class(TForm)  
  28.   procedure FormCreate(Sender: TObject);  
  29.  end;  
  30.   
  31. var  
  32.  Form1: TForm1;  
  33.   
  34. implementation  
  35.   
  36. {$R *.dfm}  
  37.   
  38. uses DateUtils;  
  39.   
  40. procedure TForm1.FormCreate(Sender: TObject);  
  41. var  
  42.  d: TDateTime;  
  43.  str: string;  
  44.  fmt: TFormatSettings;  
  45. begin  
  46.  d := StrToDateTime('2009-5-21 11:22:33');  
  47.   
  48.  str := DateTimeToStr(d);  
  49.  ShowMessage(str); // 2009-5-21 11:22:33  
  50.   
  51.  GetLocaleFormatSettings(GetThreadLocale, fmt);  
  52.  fmt.DateSeparator := '/';  
  53.  fmt.TimeSeparator := '_';  
  54.  fmt.ShortDateFormat := 'dd/mm/yyyy';  
  55.  fmt.ShortTimeFormat := 'hh:mm:ss';  
  56.  str := DateTimeToStr(d, fmt);  
  57.  ShowMessage(str); // 21/05/2009 11_22_33  
  58.   
  59.  DateTimeToString(str, 'yyyy 年 m 月 d 日 h 点 m 分 s 秒', d);  
  60.  ShowMessage(str); //2009 年 5 月 21 日 11 点 22 分 33 秒  
  61. end;  
  62.   
  63. end.  
原文地址:https://www.cnblogs.com/qingsong/p/3507172.html