Thursday, January 24, 2013

Compare two date in CSharp (C#)

Some times you may have a situation to compare two dates for the date validation. You can do this easily using the DateTime.Compare method.
  1. public bool ValidateDate(string from,string to)
  2. {
  3. bool isValid = false;
  4. if (from != string.Empty && to != string.Empty)
  5. {
  6. if (DateTime.Compare(Convert.ToDateTime(txt_From.Text), Convert.ToDateTime(txt_To.Text)) == 1)
  7. {
  8. isValid = false;
  9. }
  10. else
  11. {
  12. isValid = true;
  13. }
  14. if (DateTime.Compare(Convert.ToDateTime(to), DateTime.Now) > 0)
  15. {
  16. isValid = false;
  17. }
  18. }
  19. return isValid;
  20. }

No comments:

Post a Comment