DateTime.TryParseExact() rejecting valid formats

C#.NetCultureinfo

C# Problem Overview


I'm parsing a DateTime value in an ASP.NET WebForms page and the date string keeps getting rejected by the DateTime.TryParseExact() method even though it clearly matches one of the supplied format strings.

It seems to fail on my development machine at home but work on the production server, so I am thinking of local date settings being involved, but this error occurs even when I supply an IFormatProvider (CultureInfo) object as a parameter

Here's the code:

DateTime startDate;
string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy",
                    "dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy"};

var errStart = row.FindControl("errStartDate"); //my date format error message
if (!DateTime.TryParseExact(txtStartDate.Text, formats, null, DateTimeStyles.None, out startDate))
{
    errStart.Visible = true; //we get here even with a string like "20/08/2012"
    return false;
}
else
{
    errStart.Visible = false;
}

Note I'm giving a null FormatProvider in the above, but the same problem occurs when I provide a CultureInfo object as (CultureInfo provider = new CultureInfo("en-US")) for this parameter.

What am I missing?

C# Solutions


Solution 1 - C#

Try:

 DateTime.TryParseExact(txtStartDate.Text, formats, 
		System.Globalization.CultureInfo.InvariantCulture,
		System.Globalization.DateTimeStyles.None, out startDate)

Solution 2 - C#

Here you can check for couple of things.

  1. Date formats you are using correctly. You can provide more than one format for DateTime.TryParseExact. Check the complete list of formats, available here.

  2. CultureInfo.InvariantCulture which is more likely add problem. So instead of passing a NULL value or setting it to CultureInfo provider = new CultureInfo("en-US"), you may write it like. .

    if (!DateTime.TryParseExact(txtStartDate.Text, formats, 
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.None, out startDate))
    {
        //your condition fail code goes here
        return false;
    }
    else
    {
        //success code
    }
    

Solution 3 - C#

This is the Simple method, Use ParseExact

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime result;
String dateString = "Sun 08 Jun 2013 8:30 AM -06:00";
String format = "ddd dd MMM yyyy h:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);

This should work for you.

Solution 4 - C#

Try C# 7.0

var Dob= DateTime.TryParseExact(s: YourDateString,format: "yyyyMMdd",provider: null,style: 0,out var dt)
 ? dt : DateTime.Parse("1800-01-01");

Solution 5 - C#

string DemoLimit = "02/28/2018";
 string pattern = "MM/dd/yyyy";
 CultureInfo enUS = new CultureInfo("en-US"); 
 DateTime.TryParseExact(DemoLimit, pattern, enUS, 
                     DateTimeStyles.AdjustToUniversal, out datelimit);

For more https://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsee sharperView Question on Stackoverflow
Solution 1 - C#Adil MammadovView Answer on Stackoverflow
Solution 2 - C#Amnesh GoelView Answer on Stackoverflow
Solution 3 - C#Jidheesh RajanView Answer on Stackoverflow
Solution 4 - C#Waleed A.K.View Answer on Stackoverflow
Solution 5 - C#islam elgaidiView Answer on Stackoverflow