DateTime.Parse
In C# DateTime.Parse()
handles many formats. We get a DateTime
from a string
. When we know the format is correct, use Parse
—otherwise, prefer TryParse
.
We can parse formatted date and time strings. Formats from various databases and Internet sources can be handled. Parse
, and ParseExact
, are shown.
DateTime.Parse
works on various date time formats. Several time string
formats were found on the Internet, and the DateTime.Parse
method was used on them.
DateTime.Parse
is a static
method. It returns a new DateTime
instance, which is represented as a struct
.using System; // A simple example. string simpleTime = "1/1/2000"; DateTime time = DateTime.Parse(simpleTime); Console.WriteLine(time); // Taken from HTTP header string httpTime = "Fri, 27 Feb 2009 03:11:21 GMT"; time = DateTime.Parse(httpTime); Console.WriteLine(time); // Taken from w3.org string w3Time = "2009/02/26 18:37:58"; time = DateTime.Parse(w3Time); Console.WriteLine(time); // Taken from nytimes.com string nyTime = "Thursday, February 26, 2009"; time = DateTime.Parse(nyTime); Console.WriteLine(time); // Taken from this site string perlTime = "February 26, 2009"; time = DateTime.Parse(perlTime); Console.WriteLine(time); // Taken from ISO Standard 8601 for Dates string isoTime = "2002-02-10"; time = DateTime.Parse(isoTime); Console.WriteLine(time); // Taken from Windows file system Created/Modified string windowsTime = "2/21/2009 10:35 PM"; time = DateTime.Parse(windowsTime); Console.WriteLine(time); // Taken from Windows Date and Time panel string windowsPanelTime = "8:04:00 PM"; time = DateTime.Parse(windowsPanelTime); Console.WriteLine(time);1/1/2000 12:00:00 AM 2/26/2009 7:11:21 PM 2/26/2009 6:37:58 PM 2/26/2009 12:00:00 AM 2/26/2009 12:00:00 AM 2/10/2002 12:00:00 AM 2/21/2009 10:35:00 PM 2/26/2009 8:04:00 PM
ParseExact
You can use DateTime.ParseExact
if you have a confusing or non-standard string
. This is ideal when your date string
could be interpreted wrong by .NET, but it is valid.
ParseExact
and TryParseExact
work.string
that has letters in it that tell ParseExact
where to read in the values from your string
.using System; using System.Globalization; string dateString = "Mon 16 Jun 8:30 AM 2008"; string format = "ddd dd MMM h:mm tt yyyy"; // Use ParseExact. DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture); Console.WriteLine(dateTime);6/16/2008 8:30:00 AM
TryParse
Whenever you have a string
containing a date and time that may be invalid in formatting or that never occurred, you can use DateTime.TryParse
.
string
to a DateTime
instance without raising an exception on errors.using System; string value = "invalid"; if (DateTime.TryParse(value, out DateTime result)) { // Use result. } Console.WriteLine("DONE");DONE
FormatException
We see an exception when you specify a date that cannot exist. If Tuesday was the tenth of a month, you cannot make a DateTime
where it is the eleventh.
DateTime.ParseExact
throws an exception, a FormatException
instance, in this case.DateTime.ParseExact
can help with dates that DateTime.Parse
doesn't handle.DateTime.Parse
or TryParse
in most cases.using System; using System.Globalization; // This date never existed. string dateString = "Tue 16 Jun 8:30 AM 2008"; string format = "ddd dd MMM h:mm tt yyyy"; DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime because the day of week was incorrect.
SQL date strings can be handed with DateTime.Parse
. This is useful when you have database text-only data. DateTime.Parse
works on the date time format in the MySQL
database.
DateTime.Parse
call here succeeds, and returns the correct value.MySQL
dates and times are stored in columns of types DATETIME, DATE, and TIMESTAMP.using System; // Taken from MySQL: SELECT CURTIME() // SELECT TIME(...) string mySqlTime = "23:50:26"; DateTime time = DateTime.Parse(mySqlTime); // Taken from MySQL: SELECT TIMESTAMP(...) string mySqlTimestamp = "2003-12-31 00:00:00"; time = DateTime.Parse(mySqlTimestamp); Console.WriteLine(time); // Taken from MySQL: SELECT CURDATE() // SELECT DATE(...) string mySqlDate = "2008-06-13"; time = DateTime.Parse(mySqlDate); Console.WriteLine(time);12/31/2003 12:00:00 AM 6/13/2008 12:00:00 AM
With the examples from Microsoft SQL Server, the DateTime.Parse
method will parse the string
representations from the database.
MySQL
and SQL Server, you can use DateTime.Parse
to get the dates from the SQL strings.using System; // Taken from SQL SERVER: SELECT GETDATE() // SELECT GETUTCDATE() string sqlServerDate = "2007-04-30 13:10:02.047"; DateTime time = DateTime.Parse(sqlServerDate); Console.WriteLine(time); // Taken from SQL SERVER: SELECT SYSDATETIME() // SELECT SYSUTCDATETIME() string sqlSysDate = "2007-04-30 20:10:02.0474381"; time = DateTime.Parse(sqlSysDate); Console.WriteLine(time); // Taken from SQL SERVER: SELECT SYSDATETIMEOFFSET() string sqlSysDateOffset = "2007-04-30 13:10:02.0474381 -07:00"; time = DateTime.Parse(sqlSysDateOffset); Console.WriteLine(time);4/30/2007 1:10:02 PM 4/30/2007 8:10:02 PM 4/30/2007 1:10:02 PM
We used the DateTime.Parse
method. We saw what strings it works on. The tester-doer pattern used in TryParse
is probably the most effective in many real-world programs.