Posted By

MMDeveloper on 03/31/09


Tagged

parse date try isdate


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

ddevening
paelgr
umang_nine
mheinrich
danfinrud55


C# IsDate function


 / Published in: C#
 

Just a small simple method I wrote in C# that's similar to Visual Basic's IsDate() function.. Inputs string date, returns boolean true/false if string is a valid date or not.

  1. public bool IsDate(string sdate) {
  2. DateTime dt;
  3. bool isDate = true;
  4.  
  5. try {
  6. dt = DateTime.Parse(sdate);
  7. }
  8. catch {
  9. isDate = false;
  10. }
  11.  
  12. return isDate;
  13. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: GothikX on April 23, 2009

A more efficient alternative would be DateTime.TryParse (eliminates the need and overhead of try/catch).

Posted By: MMDeveloper on April 23, 2009

thanks! I'm not that great of a C# coder (very very green to this language). I didn't know that method existed.

You need to login to post a comment.