C# DateTime.ParseExact Usage

You want to see examples and discussion of the DateTime.ParseExact method. This method, and DateTime.TryParseExact, are useful when you have a rare format or a format that confuses DateTime.Parse. Here we see some examples of DateTime.ParseExact, and notes from the author's experiments, using the C# programming language.

Using DateTime.ParseExact

Here we see how you must specify the exact formatting string for DateTime.ParseExact. Basically, you need to use a format string that has the letters in it that will tell ParseExact where to read in the values from your string.

~~~ Program that uses DateTime.ParseExact (C#) ~~~

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
        string format = "ddd dd MMM h:mm tt yyyy";

        DateTime dateTime = DateTime.ParseExact(dateString, format,
            CultureInfo.InvariantCulture);
        Console.WriteLine(dateTime);
    }
}

~~~ Output of the program ~~~

6/16/2008 8:30:00 AM

Note on example. The strings ddd, dd, MMM, h:mm, tt, and yyyy indicate where the values are located in the string. The date string here is unusual in that the values are arranged oddly. However, it is parsed correctly. DateTime.ParseExact will not let you create a date that it deems to not exist in the world. For example, if you change Mon to Tue in the above example, it won't work, because Jun 16 was a Monday.

DateTime.ParseExact exceptions

Here we see an exception raised by DateTime.ParseExact when you specify a date that never actually existed or will exist in the world. If Tuesday was the tenth of a month, you cannot make a DateTime where it is the eleventh. DateTime.ParseExact throws an exception in this case.

=== Program that throws exception in ParseException (C#) ===

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string dateString = "Tue 16 Jun 8:30 AM 2008"; // <-- Never existed!
        string format = "ddd dd MMM h:mm tt yyyy";
        DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
    }
}

=== Output of the program ===

Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime
because the day of week was incorrect.

Note on exceptions. In the example above, the day of the week was found to be incorrect. When the date format is invalid, an exception will also be thrown.

Summary

In this article, we saw examples of using DateTime.ParseExact. It is useful in cases where you have confusing dates that DateTime.Parse doesn't handle. The author's experience is that you want to use DateTime.Parse or TryParse in the vast majority of cases. Finally, please notice there is a distinct article on this site for DateTime.TryParseExact.

See DateTime.TryParseExact Method.

See DateTime Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen