delphi判断不同格式的时间是否相等  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi判断不同格式的时间是否相等


今天测试写了很久的模拟期货交易平台这个软件的时候,发现K线中的数据很多都是走错了的。

原因分析

Delphi调试很麻烦,于是打出log来看看,最终发现服务器传过来的数据的日期格式和本机的时间格式不一样,导致在判断时间是否相等的时候出现了错误。所以需要一个函数来判断不同时间格式的时间是否相等。当然服务器的时间格式是固定不变的,服务器的时间格式是"yyyy-mm-dd",本地时间可以是所有时间格式中的任意一种。

代码实现

//判断是否是日期格式字符串
function IsDateStr(Value: String): Boolean;
var
  Temp: TDateTime;
begin
  Result := TryStrToDate(Value, Temp);
end;

function IsTheSameDate(date1,date2:String):Boolean;
begin
  Result := False;
  if (date1 = date2) then
  begin
    Result := True;
  end else if (IsDateStr(date1)) then
  begin
    if (FormatDateTime('yyyy-mm-dd',StrToDate(date1)) = date2) then
    begin
      Result := True;
    end;
  end else if (IsDateStr(date2)) then
  begin
    if (FormatDateTime('yyyy-mm-dd',StrToDate(date2)) = date1) then
    begin
      Result := True;
    end;
  end;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.070467948913574 seconds