How to convert a string to a date

Finding out how to convert one type to another in .NET is tricky. Here is the answer to converting a string to a DateTime type. Notice that any of the string formats work -- how nice!

//string strDate="2002-10-03";
//string strDate="10/04/2002";
string strDate="10/05";
DateTime dt = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990,5,6)).ConvertFrom(strDate));
Response.Write("<hr>" + dt.DayOfWeek);

You might also like...

Comments

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow