首页 > 其他 > 详细

字符串格式参数的日期比较函数

时间:2014-07-29 21:18:02      阅读:294      评论:0      收藏:0      [点我收藏+]

字符串格式参数的日期比较函数

 

我在NT Service里,使用delphi的CompareDate函数出错,我怀疑这个函数有bug,总是说我的参数多了‘‘,所以没办法自己弄了一个!

 

[delphi] view plaincopy
 
  1. { 
  2.   //功能:比较日期 
  3.   //参数: 
  4.   //a:比较的日期,格式(2013-07-23) 
  5.   //b:要比较的日期 ,格式(2013-07-24) 
  6.   //注意:日期格式必须是"yyyy-mm-dd",年月日中间必须有"-", 
  7.   //两个日期字符串必须年月日长短一致,否则比较结果错误 
  8. }  
  9. function TForm2.CompareDateEx(const A, B: string): integer;  
  10. var  
  11.   temp: integer; // 返回的值  
  12.   aList: TStringList; // a字符串List  
  13.   bList: TStringList; // b字符串List  
  14.   aInt: integer;  
  15.   bInt: integer;  
  16.   i: integer;  
  17. begin  
  18.   
  19.   temp := 0;  
  20.   
  21.   try  
  22.     // a字符串List  
  23.     aList := TStringList.Create;  
  24.     aList.Delimiter := ‘-‘;  
  25.     aList.DelimitedText := trim(A);  
  26.   
  27.     // b字符串List  
  28.     bList := TStringList.Create;  
  29.     bList.Delimiter := ‘-‘;  
  30.     bList.DelimitedText := trim(B);  
  31.   
  32.     for i := 0 to aList.Count - 1 do  
  33.     begin  
  34.       aInt := StrToInt(aList[i]);  
  35.       bInt := StrToInt(bList[i]);  
  36.   
  37.       if aInt > bInt then  
  38.       begin  
  39.         temp := 1;  
  40.         break;  
  41.       end  
  42.       else if aInt < bInt then  
  43.       begin  
  44.         temp := -1;  
  45.         break;  
  46.       end;  
  47.     end;  
  48.   
  49.   finally  
  50.     aList.Free;  
  51.     bList.Free;  
  52.   end;  
  53.   
  54.   result := temp;  
  55. end;  

使用例子

 

 

[delphi] view plaincopy
 
  1. procedure TForm2.Button4Click(Sender: TObject);  
  2. var  
  3.   temp: integer;  
  4.   aStr, bStr: string;  
  5. begin  
  6.   aStr := ‘2013-07-22‘;  
  7.   bStr := ‘2013-07-23‘;  
  8.   temp := self.CompareDateEx(aStr, bStr);  
  9.   MyLog(aStr + ‘ : ‘ + bStr + ‘ :‘ + IntToStr(temp));  
  10. end; 

 

参考:

http://blog.csdn.net/sunylat/article/details/9421481

字符串格式参数的日期比较函数,布布扣,bubuko.com

字符串格式参数的日期比较函数

原文:http://www.cnblogs.com/findumars/p/3876233.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!