title: .NET Date to string gives invalid strings in Vista Pseudo-cultures
body: <p>My computer is configured with a culture that is <a href="https://stackoverflow.com/questions/7042920/how-to-use-enable-pseudo-locale-in-windows-for-testing">not <code>en-US</code></a>. </p>
  
  <p>When using the native Win32 <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd318086%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>GetDateFormat</code></a> function, i get correctly formatted dates:</p>
  
  <ul>
  <li>22//11//2011 4::42::53 P̰̃M]</li>
  </ul>
  
  <blockquote>
    <p>This is correct; and is also how Windows renders it:</p>
    
    <ul>
    <li><p>the taskbar</p>
    
    <p><img src="https://i.stack.imgur.com/tDvRD.png" alt="enter image description here"></p></li>
    <li><p>Region and Language settings</p>
    
    <p><img src="https://i.stack.imgur.com/aNEc8.png" alt="enter image description here"></p></li>
    <li><p>Windows Explorer</p>
    
    <p><img src="https://i.stack.imgur.com/R0IR2.png" alt="enter image description here"></p></li>
    <li><p>Outlook</p>
    
    <p><img src="https://i.stack.imgur.com/Ujs9E.png" alt="enter image description here"></p></li>
    </ul>
  </blockquote>
  
  <p>When i try to convert a date to a string in .NET using my current locale, e.g.:</p>
  
  <pre><code>DateTime.Now.ToString();
  DateTime.Now.ToString(CultureInfo.CurrentCulture);
  </code></pre>
  
  <p>i get an incorrect date:</p>
  
  <ul>
  <li>22////11////2011 4::::42::::53 P̰̃M]</li>
  </ul>
  
  <blockquote>
    <p>This bug in .NET is evident anyplace in Windows that uses the buggy
    .NET code:</p>
    
    <ul>
    <li><p>Windows Event Viewer:</p>
    
    <p><img src="https://i.stack.imgur.com/0DzCv.png" alt="enter image description here"></p></li>
    <li><p>Task Scheduler:</p>
    
    <p><img src="https://i.stack.imgur.com/3DuqT.png" alt="enter image description here"></p></li>
    <li><p>SQL Server Management Studio:</p>
    
    <p><img src="https://i.stack.imgur.com/BbuiD.png" alt="enter image description here"></p></li>
    </ul>
  </blockquote>
  
  <p>How do i make .NET not buggy?</p>
  
  <p>How do i convert dates and times to strings using the current culture (correctly)?</p>
  
  <blockquote>
    <p><strong>Note</strong>: The user is allowed to set <em>their</em> Windows to any locale preferences <em>they</em> want. As it is now, my program will not handle
    <em>valid</em> settings properly. Telling the user, "Don't do that" is pretty mean-spirited. </p>
    
    <p>A similar example comes from Delphi, which assumes that a date
    separator can never be more than one character. When Windows is
    configured with a locale that uses multiple characters for the date
    separator, e.g.:</p>
    
    <ul>
    <li>sk-SK (Slovak - Slovakia) : <code>.</code></li>
    </ul>
    
    <p>where dates should be formatted as:</p>
  
  <pre><code>22. 11. 2011
  </code></pre>
    
    <p>the code library fails to accept a date separator longer than one
    character, and falls back to:</p>
  
  <pre><code>22/11/2011
  </code></pre>
    
    <p>In the past some might <a href="http://www.delphipages.com/forum/showthread.php?t=28798" rel="nofollow noreferrer">suggest that you not to bother with such edge
    cases</a>. Such suggestions carry no weight with me.</p>
    
    <p>i'll avoid getting into a pissing match with someone who wants to alter the meaning of my question by changing the title. But the question is not limited to pseudo-locales, specifically 
    designed to find bugs in applications.</p>
  </blockquote>
  
  <h2>Bonus Chatter</h2>
  
  <p>Here's a unique list of date formats from around the world:</p>
  
  <ul>
  <li>11.11.25</li>
  <li>11.25.2011</li>
  <li>11/25/2011</li>
  <li>2011.11.25</li>
  <li>2011.11.25.</li>
  <li>2011/11/25</li>
  <li>2011-11-25</li>
  <li><ol start="25">
  <li><ol start="11">
  <li>2011</li>
  </ol></li>
  </ol></li>
  <li>25.11.11</li>
  <li>25.11.2011</li>
  <li>25.11.2011 г.</li>
  <li>25.11.2011.</li>
  <li>25//11//2011</li>
  <li>25/11 2011</li>
  <li>25/11/2011</li>
  <li>25/11/2554</li>
  <li>25-11-11</li>
  <li>25-11-2011</li>
  <li>29/12/32</li>
  </ul>
  
  <p>Of particular interest is the last example which <a href="http://en.wikipedia.org/wiki/Islamic_calendar" rel="nofollow noreferrer">doesn't use the gregorian calendar</a>:</p>
  
  <ul>
  <li>Arabic (Saudi Arabia) <code>ar-SA</code>: 29/12/32 02:03:07 م</li>
  <li>Divehi (Maldives) <code>dv-MV</code>: 29/12/32 14:03:07</li>
  <li>Dari/Pashto (Afghanistan) <code>prf-AF / ps-AF</code>: 29/12/32 2:03:07 غ.و</li>
  </ul>
  
  <p>Although those are edge cases that you'd never have to worry about.</p>
  
  <hr>
  
  <p><strong>Update 14//12//2011:</strong></p>
  
  <p>Another demonstration of the bug is that <code>Datetime.Parse</code> cannot parse <code>DateTime.ToString</code>:</p>
  
  <pre><code>String s = DateTime.Today.ToString("d");   //returns "14////12////2011"
  DateTime d = DateTime.Parse(s);            //expects "dd//MM//yyyy"
  </code></pre>
  
  <p>The <code>.Parse</code> throws an exception.</p>
  
  <hr>
  
  <p><strong>Update 02//8, 2012  09::56'12:</strong></p>
  
  <p>Any use of a <strong><em>date separator</em></strong> is depricated, in addition to being incorrect. From MSDN:</p>
  
  <blockquote>
    <h2><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd373837%28v=vs.85%29.aspx" rel="nofollow noreferrer">LOCALE_SDATE</a></h2>
    
    <p><strong>Windows Vista and later:</strong> This constant is deprecated. Use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd373886%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>LOCALE_SSHORTDATE</code></a> instead. A custom locale might not have a single, uniform separator character. For example, a format such as "12/31, 2006" is valid.</p>
    
    <h2><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd373896%28v=vs.85%29.aspx" rel="nofollow noreferrer">LOCALE_STIME</a></h2>
    
    <p><strong>Windows Vista and later:</strong> This constant is deprecated. Use <code>LOCALE_STIMEFORMAT</code> instead. A custom locale might not have a single, uniform separator character. For example, a format such as "03:56'23" is valid.</p>
  </blockquote>