码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

 不同的电脑时间格式可能不一样,有的时候可能会报错,用个简单例子写个字符串和时间的转换,算做个笔记,如要放到正式的代码中,需要好好更改

 

 

 

 

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     Edit1: TEdit;
12     Button1: TButton;
13     Label1: TLabel;
14     Edit2: TEdit;
15     Button2: TButton;
16     procedure Button1Click(Sender: TObject);
17     procedure Button2Click(Sender: TObject);
18   private
19     { Private declarations }
20   public
21     { Public declarations }
22   end;
23 
24 var
25   Form1: TForm1;
26 
27 implementation
28 
29 {$R *.dfm}
30 function ConvertDateTime(sTime:String;DateFormat:string='yyyy/mm/dd';
31   TimeFormat:String='hh:nn:ss';defDatetime:TDatetime=36526):TDateTime;
32 var
33   settings,oldSettings: TFormatSettings;
34   vDateSeparator:string;
35   d,t:string;
36 begin
37   GetLocaleFormatSettings(GetUserDefaultLCID, settings);
38   oldSettings:=settings;
39   if true then
40   begin
41 
42   end;
43 
44   //这个只是做个例子,
45   d:= Copy(DateFormat,5,1);
46   settings.DateSeparator:=d[1];
47   t:=Copy(TimeFormat,3,1);
48   settings.TimeSeparator:=t[1];
49 
50   if DateFormat<>'' then
51     settings.ShortDateFormat :=DateFormat;
52   if TimeFormat<>'' then
53     settings.ShortTimeFormat :=TimeFormat;
54   try
55     Result:= strToDateTime(sTime,settings);
56   except
57     result:=defDatetime;
58   end;
59   oldSettings:=settings;
60 end;
61 procedure TForm1.Button1Click(Sender: TObject);
62 begin
63   Label1.Caption:=FormatDateTime('yyyy/mm/dd hh:mm:ss zzz',ConvertDateTime(Edit1.Text,Edit2.text ));
64   //ShowMessage(FormatDateTime('yyyy/mm/dd hh:mm:ss zzz',VarToDateTime(Edit1.Text))); 这个一般般,不太好用
65 end;
66 
67 procedure TForm1.Button2Click(Sender: TObject);
68 var
69   vDatetime01,vDatetime02:TDateTime ;
70 begin
71   vDatetime01:=0;
72   vDatetime02:=vDatetime01+8888+0.258;
73   ShowMessage(FormatDateTime('yyyy-mm-dd hh:mm:ss',vDatetime02));
74   ShowMessage(IntToStr(Trunc(0)));
75 
76 end;
77 
78 end.

 

posted on 2021-10-25 16:32  码农的笔记  阅读(174)  评论(0编辑  收藏  举报