Convert a String to a DateTime in C#
This post will discuss how to convert the specified string representation of a date and time format to its DateTime equivalent in C#.
1. Using DateTime.Parse() method
To convert a string representation of a date and time to the DateTime object, use the DateTime.Parse() method. The return value of this method indicates whether the conversion succeeded. The following example validates a date and time string by converting it into a DateTime object using the Parse() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; public class Example { public static void Main() { string date = "28/12/2019"; try { DateTime dateTime = DateTime.Parse(date); Console.WriteLine("The specified date is valid: " + dateTime); } catch (FormatException) { Console.WriteLine("Unable to parse the specified date"); } } } /* Output: The specified date is valid: 28-12-2019 12.00.00 AM */ |
2. Using DateTime.TryParse() method
We can avoid the try-catch block using the DateTime.TryParse() method. The method returns a boolean value to indicate whether the conversion is succeeded or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; public class Example { public static void Main() { string date = "28/12/2019"; DateTime dateTime; if (DateTime.TryParse(date, out dateTime)) { Console.WriteLine("The specified date is valid: " + dateTime); } else { Console.WriteLine("Unable to parse the specified date"); } } } /* Output: The specified date is valid: 28-12-2019 12.00.00 AM */ |
3. Using DateTime.ParseExact() method
Alternatively, we can also use DateTime.ParseExact() method to convert the specified string to its equivalent DateTime representation. This also allows us to pass the required format of the string and culture-specific format information about the string. If the format of the string representation doesn’t match with the specified format, an exception will be thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Globalization; public class Example { public static void Main() { string date = "12/28/2019"; string[] validformats = new[] { "MM/dd/yyyy", "yyyy/MM/dd", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy hh:mm tt", "yyyy-MM-dd HH:mm:ss, fff" }; CultureInfo provider = new CultureInfo("en-US"); try { DateTime dateTime = DateTime.ParseExact(date, validformats, provider); Console.WriteLine("The specified date is valid: " + dateTime); } catch (FormatException) { Console.WriteLine("Unable to parse the specified date"); } } } /* Output: The specified date is valid: 28-12-2019 12.00.00 AM */ |
4. Using DateTime.TryParseExact() method
We can avoid the try-catch block using the DateTime.TryParseExact() method. This method returns true if the string is successfully converted to DateTime and false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Globalization; public class Example { public static void Main() { string date = "12/28/2019"; DateTime dateTime; string[] validformats = new[] { "MM/dd/yyyy", "yyyy/MM/dd", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy hh:mm tt", "yyyy-MM-dd HH:mm:ss, fff" }; CultureInfo provider = CultureInfo.InvariantCulture; if (DateTime.TryParseExact(date, validformats, provider, DateTimeStyles.None, out dateTime)) { Console.WriteLine("The specified date is valid: " + dateTime); } else { Console.WriteLine("Unable to parse the specified date"); } } } /* Output: The specified date is valid: 28-12-2019 12.00.00 AM */ |
That’s all about converting a String to DateTime in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)