Date comparison function of string format parameters
I encountered an error using the comparedate function of Delphi in the NT Service. I suspect there is a bug in this function. I always say that I have more parameters than '', so I cannot get one by myself!
[Delphi]View plaincopy
- {
- // Function: Compare date
- // Parameters:
- // A: Date of comparison, in the format)
- // B: Date to be compared. Format)
- // Note: the date format must be "yyyy-mm-dd" and "-",
- // Two date strings must have the same length of year, month, and day; otherwise, the comparison result is incorrect.
- }
- Function tform2.comparedateex (const A, B: string): integer;
- VaR
- Temp: integer; // The returned value.
- Alist: tstringlist; // a string list
- Blist: tstringlist; // B string list
- Aint: integer;
- Bint: integer;
- I: integer;
- Begin
- Temp: = 0;
- Try
- // A string list
- Alist: = tstringlist. Create;
- Alist. delimiter: = '-';
- Alist. delimitedtext: = trim ();
- // B string list
- 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;
Example
[Delphi]View plaincopy
- Procedure tform2.button4click (Sender: tobject );
- VaR
- Temp: integer;
- Astr, BSTR: string;
- Begin
- Astr: = '2017-07-22 ';
- BSTR: = '2017-07-23 ';
- Temp: = self. comparedateex (astr, BSTR );
- Mylog (astr + ':' + BSTR + ':' + inttostr (temp ));
- End;
Refer:
Http://blog.csdn.net/sunylat/article/details/9421481