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;

相关阅读 >>

Delphi 测试字符串写入类: tstringwriter

Delphi目录操作示例

Delphi 窗体只显示控件

Delphi 获取打开的记事本中的内容

cnvcl 组件包

Delphi android实例-trectangle加载图片(xe8+小米2)

Delphi和outputdebugstring

Delphi fmx检查应用程序状态更改

Delphi webbroker 输出图片的问题

Delphi替换字符串中的单引号

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...