You need to parse a date and time string in the C# programming language. Explore the versatile DateTime.Parse and DateTime.TryParse methods for this purpose. Here we see examples of parsing formatted date and time strings, with examples from databases and the Internet.
First we see that DateTime.Parse is a static method in the C# language that you can call without using a DateTime instance. It returns a new DateTime instance, which is represented as a struct. If you provide no time, the time used is 12 AM.
--- Program that uses DateTime.Parse (C#) ---
using System;
class Program
{
static void Main()
{
string date = "2000-02-02";
DateTime time = DateTime.Parse(date);
Console.WriteLine(time);
}
}
--- Output of the program ---
2/2/2000 12:00:00 AMHere we note that whenever you have a string containing a date and time that may be invalid in formatting or that never occurred, you can use the DateTime.TryParse static method in the System namespace to convert it to a DateTime instance without raising an exception on errors. This site has a more detailed look at the TryParse method for your usage.
See DateTime.TryParse Example.
Here we see how the DateTime.Parse method works on various different date time formats. Several different time string formats were found on the Internet, and the DateTime.Parse method was used on each. Each call below succeeded and returned the expected value. The final comment shows the output.
--- Program that parses times (C#) ---
using System;
class Program
{
static void Main()
{
// Taken from the author's head
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);
}
}
--- Output of the program ---
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 PMThe DateTime.Parse method can also be used on SQL Server and MySQL date strings. Although it is best to use an ADO.NET provider for this, often when you are working with the data in text format it is ideal to use DateTime.Parse. You can find detailed explanations of this in the separate article.
In some scenarios you can use DateTime.ParseExact if you have a confusing or non-standard date time string. This is ideal when your date string could be interpreted wrong by .NET, but it is valid. Use the formatting characters to specify how ParseExact and TryParseExact work.
The DateTime format strings supported in .NET are dealt with in another article at this site. The article has summaries and notes on many of the formatting strings, as well as lists of what they can output.
Here we saw how you can use the DateTime.Parse method in the C# programming language, and on what strings it works. The tester-doer pattern used in TryParse is probably the most effective in many real-world programs, but the Parse method itself is also useful in some situations.