Tip 8 : IsDate() function in C#

C# does not provide IsDate() function, but you can build one pretty easily:

You can build other functions such as IsInteger() by using int.Parse() instead of DateTime.Parse().

Based on feedbacks from the community, I am adding an alternative using TryParse.

Published Thursday, January 14, 2010 9:24 PM by StanleyGu
Filed under: ,

Comments

# re: IsDate() function in C#

Thursday, January 14, 2010 10:24 PM by Chris Donges

Might be better off using DateTime.TryParse.  Could be faster than throwing an exception.

# re: IsDate() function in C#

Thursday, January 14, 2010 10:37 PM by Benjamin Roux

Better with TryParse

DateTime dt;

return DateTime.TryParse(inputDate, out dt);

You could even make it an extension method for string!

# re: IsDate() function in C#

Thursday, January 14, 2010 10:53 PM by Arun Mahendrakar

You might want to use a TryParse instead of a Parse. You'll avoid throwing an exception which causes perf issues.

vbnotebookfor.net/.../comparing-string-to-integer-conversion-methods-in-vbnet

says TryParse is slightly better.

Arun

# re: IsDate() function in C#

Thursday, January 14, 2010 11:23 PM by Jeff

I hear you should use TryParse.

# re: IsDate() function in C#

Thursday, January 14, 2010 11:24 PM by Kumu

I agree with the comments for a TryParse, you could also make use of the 3.5 extension feature

# re: IsDate() function in C#

Friday, January 15, 2010 3:57 AM by Petar Petrov

Hi.

I'm a little skeptical about this method. Because probably if it's a date you will need the value of the data so once again you will parse the date.

I'm sorry but your method is useless to me.

Just my 2 cents.

# re: IsDate() function in C#

Friday, January 15, 2010 6:17 AM by Hassan

I would prefer it without a try catch

DateTime dt = DateTime.Now;

return DateTime.TryParse(inputDate, dt);

# re: IsDate() function in C#

Friday, January 15, 2010 11:28 AM by zoldello

This is a nice post. However, I think it should be edited to take into account the nice comments/feedback.

# [Tips C#] Mi opinión sobre el artículo “IsDate() function in C#”

Monday, January 18, 2010 12:10 PM by Amigo mío Siempre estas Programando en .NET

Esta máñana leyendo mis RSS , he visto este artículo y he pensado en comentarlo, pero mejor me hago un

# re: Tip 8 : IsDate() function in C#

Monday, March 22, 2010 5:18 AM by Valora

Badly need your help. Nice site - pity you have to go to such lengths to moderate it. Help me! I can not find sites on the: Online brokerage firms stocks. I found only this - <a href="wai.alaw.org/.../online-brokerage-account-comparison">online brokerage account comparison</a>. Most of the fees will result you with a firm business given to your stock capital, and you can get your challenge from any of the value which improves the interest you have, online brokerage. You must start the rail to hold with their market, online brokerage. Waiting for a reply :confused:, Valora from Algeria.

# re: Tip 8 : IsDate() function in C#

Thursday, April 22, 2010 1:50 PM by Ashwani

Hello:

DateTime.TryParse does not give a SQL Date when we use "March 16 -17" which in SQL is invalid, but in C# it is march 16, 2017

# re: Tip 8 : IsDate() function in C#

Thursday, June 30, 2011 9:16 AM by Helen

Don't you love it when someone writes a "helpful" article using 10 lines of wasteful code... when there alread IS a built-in method using 1 line of code?

I wonder how many people just read the article... and use the bad code... and never wade through all the "corrections" below it.

Sure would be nice if the original author would admit:  "I made a mistake!  Just use TryParse instead of my mess!"  and remove his code.

# re: Tip 8 : IsDate() function in C#

Thursday, June 30, 2011 9:24 AM by Bonnie

But he DID post the "alternate" method.

(It really shouldn't be "alternate"... it should be *THE* method you use.)

The 2 code samples are now implied as "correct method" and "alternate method".

They really should be cleary marked "bad example" and "use this always".

# re: Tip 8 : IsDate() function in C#

Tuesday, August 02, 2011 12:58 PM by could u wright function

could u wright function

IsPicture()

Leave a Comment

(required) 
(required) 
(optional)
(required)