Convert a String to a DateTime in C#

Sometimes user enter dates as string values. So to convert a string-based date to a System.DateTime object, you can use the Convert.ToDateTime(String) method

String date = "07 October, 2012";
DateTime dt = Convert.ToDateTime(date);
Console.WriteLine("Month: {0}, Day: {1}, Year: {2}", dt.Month, dt.Day, dt.Year);  

No comments yet.

Leave a Reply