Date comparison function of string format parameters

Source: Internet
Author: User
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
  1. {
  2. // Function: Compare date
  3. // Parameters:
  4. // A: Date of comparison, in the format)
  5. // B: Date to be compared. Format)
  6. // Note: the date format must be "yyyy-mm-dd" and "-",
  7. // Two date strings must have the same length of year, month, and day; otherwise, the comparison result is incorrect.
  8. }
  9. Function tform2.comparedateex (const A, B: string): integer;
  10. VaR
  11. Temp: integer; // The returned value.
  12. Alist: tstringlist; // a string list
  13. Blist: tstringlist; // B string list
  14. Aint: integer;
  15. Bint: integer;
  16. I: integer;
  17. Begin
  18. Temp: = 0;
  19. Try
  20. // A string list
  21. Alist: = tstringlist. Create;
  22. Alist. delimiter: = '-';
  23. Alist. delimitedtext: = trim ();
  24. // B string list
  25. Blist: = tstringlist. Create;
  26. Blist. delimiter: = '-';
  27. Blist. delimitedtext: = trim (B );
  28. For I: = 0 to alist. Count-1 do
  29. Begin
  30. Aint: = strtoint (alist [I]);
  31. Bint: = strtoint (blist [I]);
  32. If aint> bint then
  33. Begin
  34. Temp: = 1;
  35. Break;
  36. End
  37. Else if aint <bint then
  38. Begin
  39. Temp: =-1;
  40. Break;
  41. End;
  42. End;
  43. Finally
  44. Alist. Free;
  45. Blist. Free;
  46. End;
  47. Result: = temp;
  48. End;

Example

 

 

[Delphi]View plaincopy
  1. Procedure tform2.button4click (Sender: tobject );
  2. VaR
  3. Temp: integer;
  4. Astr, BSTR: string;
  5. Begin
  6. Astr: = '2017-07-22 ';
  7. BSTR: = '2017-07-23 ';
  8. Temp: = self. comparedateex (astr, BSTR );
  9. Mylog (astr + ':' + BSTR + ':' + inttostr (temp ));
  10. End;

 

Refer:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.