Note that there are some explanatory texts on larger screens.

plurals
  1. PODateTime.TryParseExact not working as expected
    primarykey
    data
    text
    <p>Can anyone explain why the following snippet returns true?</p> <p>According to the docs for <a href="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#dSpecifier">The "d" custom format specifier</a>, "A single-digit day is formatted without a leading zero." So why doesn't TryParseExact fail when I give it a single-digit day with a leading zero?</p> <pre><code>DateTime x; return DateTime.TryParseExact ( "01/01/2001", @"d\/MM\/yyyy", null, System.Globalization.DateTimeStyles.None, out x ); </code></pre> <p><strong>UPDATE</strong></p> <p>I think maybe I was unclear originally. What I am really trying to get at is: <strong>Why does TryParseExact accept some values that don't match exactly?</strong> from all of the documentation I have seen, 'd' matching '01' and '1' is just as much a bug as if 'MM' matched 'March' as well as '03'. <strong>The issue here isn't that the values are equivalent, its that they don't match the format.</strong></p> <p>The relevant snippets of documentation are:</p> <ul> <li><p>From <a href="http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx">TryParseExact</a>: <strong>The format of the string representation must match a specified format exactly.</strong></p></li> <li><p>From <a href="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#dSpecifier">The 'd' Specifier</a>: <strong>A single-digit day is formatted without a leading zero.</strong></p></li> </ul> <p>It seems abundantly clear to me that '01' has a leading 0, and therefore doesn't exactly match 'd'. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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