Note that there are some explanatory texts on larger screens.

plurals
  1. POC# TimeSpan.Parse invalid format returns incorrect value instead of exception
    primarykey
    data
    text
    <p>TimeSpan.Parse("23:00:00") returns 23 hours.</p> <p>TimeSpan.Parse("24:00:00") returns 24 days!</p> <p>I realize that I made a mistake in that the allowable range of hours is 0-23. But for minutes and seconds if you attempt to parse an out of range value you get an exception. In the case of hours with an out of range value, the parser incorrectly assumes you meant days instead of hours.</p> <p>Can someone explain this?</p> <p>This example here covers this very topic and indicates that <a href="http://msdn.microsoft.com/en-us/magazine/ee309881.aspx">http://msdn.microsoft.com/en-us/magazine/ee309881.aspx</a></p> <p>The same appears to be true about TryParse. I get 24 days despite the docs stating that the parse should fail.</p> <p><a href="http://msdn.microsoft.com/en-us/library/3z48198e">http://msdn.microsoft.com/en-us/library/3z48198e</a></p> <pre><code>// String to Parse TimeSpan // --------------- --------------------- // 0 00:00:00 // 14 14.00:00:00 // 1:2:3 01:02:03 // 0:0:0.250 00:00:00.2500000 // 10.20:30:40.50 10.20:30:40.5000000 // 99.23:59:59.9999999 99.23:59:59.9999999 // 0023:0059:0059.0099 23:59:59.0099000 // 23:0:0 23:00:00 // 24:0:0 Parse operation failed. // 0:59:0 00:59:00 // 0:60:0 Parse operation failed. // 0:0:59 00:00:59 // 0:0:60 Parse operation failed. // 10: Parse operation failed. // 10:0 10:00:00 // :10 Parse operation failed. // 0:10 00:10:00 // 10:20: Parse operation failed. // 10:20:0 10:20:00 // .123 Parse operation failed. // 0.12:00 12:00:00 // 10. Parse operation failed. // 10.12 Parse operation failed. // 10.12:00 10.12:00:00 </code></pre> <p>Did I find a bug or am I doing something wrong?</p> <p>EDIT: I've tested this in LinqPad and using a Console app in .NET4 on Windows 7 64bit.</p> <pre><code> var result = TimeSpan.Parse("24:00:00"); Console.WriteLine(result); result = TimeSpan.Parse("24:00:00", CultureInfo.InvariantCulture); Console.WriteLine(result); </code></pre> <p>This results in:</p> <pre><code>24.00:00:00 24.00:00:00 </code></pre>
    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