我在NT Service里,使用delphi的CompareDate函数出错,我怀疑这个函数有bug,总是说我的参数多了‘‘,所以没办法自己弄了一个!
- function TForm2.CompareDateEx(const A, B: string): integer;
- var
- temp: integer;
- aList: TStringList;
- bList: TStringList;
- aInt: integer;
- bInt: integer;
- i: integer;
- begin
-
- temp := 0;
-
- try
-
- aList := TStringList.Create;
- aList.Delimiter := ‘-‘;
- aList.DelimitedText := trim(A);
-
-
- bList := TStringList.Create;
- bList.Delimiter := ‘-‘;
- bList.DelimitedText := trim(B);
-
- for i := 0 to aList.Count - 1 do
- begin
- aInt := StrToInt(aList[i]);
- bInt := StrToInt(bList[i]);
-
- if aInt > bInt then
- begin
- temp := 1;
- break;
- end
- else if aInt < bInt then
- begin
- temp := -1;
- break;
- end;
- end;
-
- finally
- aList.Free;
- bList.Free;
- end;
-
- result := temp;
- end;
使用例子
- procedure TForm2.Button4Click(Sender: TObject);
- var
- temp: integer;
- aStr, bStr: string;
- begin
- aStr := ‘2013-07-22‘;
- bStr := ‘2013-07-23‘;
- temp := self.CompareDateEx(aStr, bStr);
- MyLog(aStr + ‘ : ‘ + bStr + ‘ :‘ + IntToStr(temp));
- end;
参考:
http://blog.csdn.net/sunylat/article/details/9421481