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


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

     

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

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

  • 相关阅读:
    简单 dp 题选做
    UVa11327
    Codeforces Round #641 (div.2) 题解
    新博客
    数位dp的学习
    stl粗略用法
    cf437C The Child and Toy
    poj1995 Raising Modulo Numbers
    Tarjan的学习
    最短路模板
  • 原文地址:https://www.cnblogs.com/findumars/p/3876233.html
Copyright © 2020-2023  润新知