Mark Wagner's .NET C# Cogitation

.NET Architect and Developer - Simple Thoughts from a Simple Mind

  Home :: Contact :: Syndication  :: Login
  90 Posts :: 59 Articles :: 1548 Comments :: 207 Trackbacks

News

You can download this .TEXT blog skin free. Download Cogitation Blue skin.
Updated May 6, 2004

My Sites
- New SharePoint Blog
- My Developer blog
- My Personal blog
ASP.NET Web Hosting
I host my site here. The best value for quality hosting. Read my opinion here. If you decide to join, please use this link. Thank you very much.


Legal
Any and all code, software, examples, suggestions and anything else on this web site is available for you to use at your own risk. No warranty is express or implied.
Views and Opinions
Mark Wagner works for Microsoft in the Consulting Services division and covers the West Region. The views and opinions expressed on this web site are not necessarily the views or opinions of his employer.

Article Categories

Archives

Post Categories

.: Architecture Links :.

.: Developer Links :.

.Text and RSS Links

Blog Roll - Top Guns

Developer: .NET Security

Developer: C# Team

Developer: Flash

Developer: JavaScript

Developer: Pocket PC

Personal: Aviation

Personal: Favorites

Simple string date validator.

I am a big fan of maintaining a library of simple and clean helper methods.  Here is a simple and clean way to verify if a string formatted date is a valid date.  This allows you to encapsulate the exception handling making it easy to use and very readable - another important coding practice.

private static bool IsDate(string sDate)
{
    DateTime dt;
    bool isDate = true;

    try
    {
        dt = DateTime.Parse(sDate);
    }
    catch
    {
        isDate = false;
    }

    return isDate;
}

posted on Wednesday, April 06, 2005 8:19 PM

Feedback

# re: String Date Validator 4/7/2005 6:44 AM Jake Good
I do this kind of checking for decimals as well...

# re: String Date Validator 4/12/2005 6:09 AM Anthony
You should only catch the FormatException

# re: String Date Validator 6/10/2005 1:10 PM Jeremy Wilson
The Microsoft.VisualBasic namespace has an IsDate function that doesn't take nearly as long to verify whether a string is a valid date (try passing a non-date string value to your function - on my old Pentium 3 at work, there is about a 5 second pause before the code in the exception block returns false).

I'm not suggesting one should use the VB.Net fuction, but I'd like to know how they are managing the process without the overhead of exception checking, and use that instead. Surprises me that the .NET framework doesn't have a set of validation classes for stuff like this :/



# re: String Date Validator 6/10/2005 1:19 PM Mark Wagner
Hi Jeremy,

Good point. I'll have to crack open Reflector and see how the Microsoft.VisualBasic namespace is doing that. I think I can use Reflector???

Thanks for pointing this out.

# re: String Date Validator 1/2/2006 11:11 PM yabogs
godbless you all i wish that you become a great programmer.....

# re: String Date Validator 2/24/2006 1:37 PM John Balderston
This is a static method. Would isDate not retain its last state? In other words, an invalid sDate would set isDate to false, then another call to IsDate would also return false because the try{} block does not set state, only reports it? Simply put, the bool isDate = true; is ONLY an initialization of the state for isDate. It then retain state until it is changes. Or is this only true in Java?

# re: String Date Validator 3/23/2006 12:48 PM Rob Gaudet
Is there an asp.net date validator for checking that a date is in valid format?

# new visitor 3/30/2006 5:38 AM sanjay chauhan
dear sir,
myself sanjay chauhan . the new visitor of this site. i have MCA degree and with 1year exp i m searching a job on ASP.net platform.
can u send me the interview question based on ADO.net, .Net framwork 1.1, ASP.net with C# and SQL SERVER 2000.
THank you sir.


# re: String Date Validator 4/10/2006 12:36 AM Bob Dale Ocer
Hello there... I've used a similar function to check if a given string represents a valid date or not. There is just one problem about the above validator, it allows dates which are out of range. For example, the string "June 35, 1982" is interpreted as "July 5, 1982"... is there a way to catch this kind of error? Thanks...

# re: String Date Validator 12/2/2006 3:50 PM 窃听器
ok

# re: String Date Validator 2/21/2007 2:06 AM cgjvghjbhk
dfxfdxfgcgcgmvgvh

Post Feedback

Title:
Name:
Url:
Comments: 
Enter the code you see: