C# DateTime parse
last modified October 5, 2026
In this article we learn how to parse datetime strings into
DateTime objects in C#.
C# DateTime
The DateTime value type represents dates and times with values
ranging from 00:00:00 (midnight), January 1, 0001, through 11:59:59 P.M.,
December 31, 9999, in the Gregorian calendar. A DateTime value does
not carry time zone information; use DateTimeOffset when an offset
is part of the value.
Parse methods
The DateTime.Parse method converts a datetime string into a
DateTime. It tries the datetime patterns of the current culture and
throws a FormatException when the string cannot be interpreted.
The DateTime.ParseExact method converts the specified string
representation of a datetime to a DateTime. The datetime string
format must match the specified format exactly; otherwise a
FormatException is thrown.
Dates and times are culture specific; the parsing methods use the current culture by default, or a culture passed as an argument.
A string that omits the year, month, or day is completed from the current date.
Optional DateTimeStyles values control additional details, such as
whether a value without time zone information is treated as local or UTC.
C# DateTime.Parse
We use DateTime.Parse to convert datetime strings into
DateTime objects.
using System.Globalization;
string[] dates =
{
"03/19/2021 07:20:58",
"03/19/2021",
"Fri, 19 Mar 2021 05:20:12 GMT",
"2021-03-19T05:20:12.0000000Z",
"2021-03-19T05:20:12.0000000-05:00",
"2021-03-19 05:20:12 -5:00",
};
foreach (var ds in dates)
{
var dt = DateTime.Parse(ds, CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
Console.WriteLine(dt);
}
We have a bunch of datetime strings in an array. We convert them into
DateTime objects with DateTime.Parse. We pass the
invariant culture so that the strings are interpreted the same way on every
machine, and the AdjustToUniversal and AssumeUniversal
styles normalize the results to UTC.
$ dotnet run 3/19/2021 7:20:58 AM 3/19/2021 12:00:00 AM 3/19/2021 5:20:12 AM 3/19/2021 5:20:12 AM 3/19/2021 10:20:12 AM 3/19/2021 10:20:12 AM
C# DateTime.ParseExact
With DateTime.ParseExact, we explicitly specify the format of the
datetime string.
using System.Globalization; var ds = "Thu Nov 11, 2021"; var dt = DateTime.ParseExact(ds, "ddd MMM dd, yyyy", CultureInfo.InvariantCulture); Console.WriteLine(dt); var ds2 = "10-22-2015"; var dt2 = DateTime.ParseExact(ds2, "MM-dd-yyyy", CultureInfo.InvariantCulture); Console.WriteLine(dt2);
The second parameter of the DateTime.ParseExact method is the
format of the datetime string and the third parameter is the culture. The format
must match the string exactly; otherwise a FormatException is
thrown.
$ dotnet run 11/11/2021 12:00:00 AM 10/22/2015 12:00:00 AM
C# DateTime FormatException
If the DateTime.Parse method fails, it throws a
FormatException.
using System.Globalization;
string ds = "11/31/2021";
try
{
DateTime dt = DateTime.Parse(ds, CultureInfo.InvariantCulture);
Console.WriteLine($"{dt:d MMMM, yyyy}");
}
catch (FormatException e)
{
Console.WriteLine("failed to parse string");
Console.WriteLine(e.Message);
}
In the example, we handle a FormatException that is thrown when the
string is not a valid datetime, and we print the exception message.
$ dotnet run failed to parse string String '11/31/2021' was not recognized as a valid DateTime.
C# DateTime.TryParse
The DateTime.TryParse method converts the specified datetime string
into a DateTime. It returns a boolean value that indicates whether
the conversion succeeded and stores the result in an out parameter.
It does not throw an exception when parsing fails.
using System.Globalization;
string ds = "11/31/2021";
var ok = DateTime.TryParse(ds, CultureInfo.InvariantCulture, out var dt);
if (ok)
{
Console.WriteLine($"{dt:d MMMM, yyyy}");
}
else
{
Console.WriteLine("failed to parse datetime string");
}
In the example, we try to parse an invalid datetime string. If the method succeeds, we print the parsed object; otherwise, we print an error message.
$ dotnet run failed to parse datetime string
C# DateTime.Parse with CultureInfo
Dates and times are culture specific. We need to pass the culture information to the parsing methods when a culture other than the current one is used.
using System.Globalization;
string[] dates =
{
"piatok 12. novembra 2021 8:34:10",
"12. 11. 2021",
"pi, 12 nov 2021",
};
var skSk = CultureInfo.GetCultureInfo("sk-SK");
foreach (var ds in dates)
{
var dt = DateTime.Parse(ds, skSk);
Console.WriteLine(dt.ToString(skSk));
}
In the example, we parse three dates written in the Slovak culture. The culture
is passed as the second parameter of the DateTime.Parse method, and
the results are formatted with the same culture.
$ dotnet run 12. 11. 2021 8:34:10 12. 11. 2021 0:00:00 12. 11. 2021 0:00:00
C# parse Last-Modified header value
The Last-Modified response HTTP header contains a datetime when the
origin server believes the resource was last modified.
using System.Globalization;
var url = "https://www.example.com";
using var client = new HttpClient();
var res = await client.GetAsync(url);
string lm = res.Content.Headers.GetValues("Last-Modified").First();
Console.WriteLine(lm);
var lmd = DateTimeOffset.Parse(lm, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
Console.WriteLine(lmd);
In the example, we parse the Last-Modified header value of an HTTP
response. We use DateTimeOffset so that the UTC offset of the header
value is preserved. The value depends on the resource and on when it was last
modified.
$ dotnet run Sun, 04 Oct 2026 20:44:03 GMT 10/4/2026 8:44:03 PM +00:00
Source
DateTime.Parse method
DateTime.ParseExact method
DateTime.TryParse method
DateTimeStyles enumeration
In this article we parsed strings into DateTime and
DateTimeOffset objects in C#.
Author
List all C# tutorials.