Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would DateTime.ParseExact fail with CultureInfo.CurrentCulture?
    primarykey
    data
    text
    <p>I'm trying to parse dates with a given format and I've found this which I cannot explain:</p> <pre><code>var date = new DateTime(2001, 01, 10); var cultureInfo1 = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name); var cultureInfo2 = new CultureInfo(CultureInfo.CurrentCulture.Name); Assert.AreEqual(date, DateTime.ParseExact("10/01/01", "dd/MM/yy", cultureInfo1)); Assert.Throws&lt;FormatException&gt;(() =&gt; DateTime.ParseExact("10/01/01", "dd/MM/yy", CultureInfo.CurrentCulture)); Assert.Throws&lt;FormatException&gt;(() =&gt; DateTime.ParseExact("10/01/01", "dd/MM/yy", cultureInfo2)); </code></pre> <p>Why would this call fail with the CurrentCulture, fail with a new instance created using the Culture name but not fail with a Culture obtained using GetCultureInfo(). The current culture is en-US and Windows is configured with this two changes (done in intl.cpl): use the Metric system, and short date format 'dd-MMM-yyyy'.</p> <p><strong>Update:</strong></p> <p>The original question was correctly answered, but I want to add that both cultures are able to parse the date. The problem seems to lie with the date separator specified in CultureInfo.DateTimeFormat.DateSeparator. </p> <p>The cultureInfo which has my overrides, has '-' as separator, while the original culture has '/'. </p> <p>To make ParseExact parse the date irrespective of the culture specified separator, the format spec should be changed from <strong>"dd/MM/yy"</strong> to <strong>"dd'/'MM'/'yy"</strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload