internet上得到的时间字符串12:13:54 2002-9-24,如何转化为tdatetime型,如果不截取字符串的话

时间:2021-06-16 19:02:44
我这样做觉得太繁了,有人知道简单的吗?
procedure TForm1.FormCreate(Sender: TObject);
var
  NMDayTime1: TNMDayTime;
  currentTime:TSystemTime;
  i:integer;
  str:String;
begin
NMDayTime1:=TNMDayTime.Create(self);
NMDayTime1.Host:='202.109.114.209';
NMDayTime1.Port:=13;
Label1.Caption:=NMDayTime1.DayTimeStr;
Edit1.Text:=Label1.Caption;
str:=copy(Label1.Caption,0,8);
DecodeTime(StrToTime(str),CurrentTime.wHour,CurrentTime.wMinute,
      CurrentTime.wSecond,CurrentTime.wMilliseconds);
str:=copy(Label1.Caption,10,Length(Label1.Caption)-7);
DecodeDate(StrToDate(trim(str)),CurrentTime.wYear,CurrentTime.wMonth,
      CurrentTime.wDay);
SetLocaltime(currentTime);
NMDayTime1.Destroy;
end;

5 个解决方案

#1


这样已经很不错了。

#2


str :=NMDayTime1.DayTimeStr;
  Str := LeftStr(str, Length(Str) - 1);
  str :=  RightStr(Str, Length(Str) - 8) + ' ' + LeftStr(str, 8);
  DateTimeToSystemTime(StrToDateTime(Str), CurrentTime);

---以上代码在Delphi6.0下测试通过!!

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]  

#3


uses StrUtils;

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]  

#4


时间不一定时8位的我用了新的
NMDayTime1:=TNMDayTime.Create(self);
NMDayTime1.Host:='202.109.114.209';
NMDayTime1.Port:=13;
Label1.Caption:=NMDayTime1.DayTimeStr;
i:=pos(' ',Label1.caption);
str:=copy(Label1.Caption,0,i-1);
DecodeTime(StrToTime(str),CurrentTime.wHour,CurrentTime.wMinute,
      CurrentTime.wSecond,CurrentTime.wMilliseconds);
str:=copy(Label1.Caption,i+1,Length(Label1.Caption)-8);
DecodeDate(StrToDate(trim(str)),CurrentTime.wYear,CurrentTime.wMonth,
      CurrentTime.wDay);
SetLocaltime(currentTime);

#5


要不你可以Pos一下时间与日期中间的那个分隔符(空格)
然后再判断时间是多少位!

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]  

#1


这样已经很不错了。

#2


str :=NMDayTime1.DayTimeStr;
  Str := LeftStr(str, Length(Str) - 1);
  str :=  RightStr(Str, Length(Str) - 8) + ' ' + LeftStr(str, 8);
  DateTimeToSystemTime(StrToDateTime(Str), CurrentTime);

---以上代码在Delphi6.0下测试通过!!

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]  

#3


uses StrUtils;

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]  

#4


时间不一定时8位的我用了新的
NMDayTime1:=TNMDayTime.Create(self);
NMDayTime1.Host:='202.109.114.209';
NMDayTime1.Port:=13;
Label1.Caption:=NMDayTime1.DayTimeStr;
i:=pos(' ',Label1.caption);
str:=copy(Label1.Caption,0,i-1);
DecodeTime(StrToTime(str),CurrentTime.wHour,CurrentTime.wMinute,
      CurrentTime.wSecond,CurrentTime.wMilliseconds);
str:=copy(Label1.Caption,i+1,Length(Label1.Caption)-8);
DecodeDate(StrToDate(trim(str)),CurrentTime.wYear,CurrentTime.wMonth,
      CurrentTime.wDay);
SetLocaltime(currentTime);

#5


要不你可以Pos一下时间与日期中间的那个分隔符(空格)
然后再判断时间是多少位!

================================================================
     ◆◆◆ CSDN查询助手,查询方便快捷◆◆◆ 

下载地址:  
 http://CoolSlob.ifood1.com/Download/CSDNFinder.exe  
 http://CoolSlob.8u8.com/Download/Tools/CSDNFinder.Slob[更名为.exe即可]