How to convert a string into valid DateTime object


If you have a string in your program which is convertible into a valid DateTime object you can use Parse method of DateTime class as shown in following examples:

string s = "12-January-2008"; 
DateTime d = DateTime.Parse(s); 

s = "12-Mar-2008"; 
d = DateTime.Parse(s); 

s = "12-25-2008"; 
d = DateTime.Parse(s);

Note that the Parse method throws FormatException if your string is not valid and not convertible into valid DateTime object.

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments