.net - Date.TryParse incorrect results -


when using date.tryparse, why string input of "2015-03-02-01" succeed during conversion string date, result of "3/1/2015 8:00:00 pm"? using vs2013 targeting .net 4.0.

i expect date.tryparse return false in case.

if did succeed, expect return either

  1. "3/2/2015" ignoring final "-01"
  2. "3/1/2015 11:00:00 pm" if treated final "-01" minus 1 hour "2015-03-02"
  3. "3/1/2015 7:00:00 pm" if treated final "-01" minus 1 hour , applied current eastern daylight time utc offset of -4 well.

any idea of going on here internally cause return "3/1/2015 8:00:00 pm"?

here test case:

<testmethod()> public sub testdatetryparse()     dim s string = "2015-03-02-01"     dim d date     if date.tryparse(s, d)         assert.fail("date parsed when should have failed. input: ""{0}"", output: ""{1}""", s, d)     end if end sub 

test result message: assert.fail failed. date parsed when should have failed. input: "2015-03-02-01", output: "3/1/2015 8:00:00 pm"

the string interpreted 2015-03-02 @ timezone -01.

the documentation datetime.tryparse has say:

if s contains no time zone information, result contains datetime value kind property datetimekind.unspecified when method returns. if string parsed contains time zone information, result contains datetime value kind property datetimekind.local when method returns.

so, normal date string 2015-03-02, when parse using datetime.tryparse(), see date's kind property datetimekind.unspecified documented.

but if check kind property in funny case, see give datetimekind.local, indicating parsing operation detected timezone information.

so happens in case your local timezone must -05. (edit: or more precisely, was -05 on 2015-03-02. because of daylight savings time, current timezone may different, timezone matters 1 in effect on 2015-03-02) when parses 2015-03-02-01, here does:

  • interpret 2015-03-02-01 2015-03-02 12:00:00 am @ timezone -01
  • convert 2015-03-02 12:00:00 am @ timezone -01 local timezone -05: 2015-03-01 08:00:00 pm

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

c# - MSDN OneNote Api: Navigate to never before opened page without opening a OneNote Application Window -