Time parsing Issue using DateTime.ParseExact()

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-21 22:05:11

问题


I have a Time Value stored as string with the format HH:mm

I used the following code to parse it as date with Today Date

DateTime.ParseExact("09:00","HH:mm",New System.Globalization.CultureInfi("En-GB"))

The Result Is 2017-03-15 09:00:00

When Running this Code From another machine i get the following Result: 1899-12-31 09:00:00

I replaced my code to the following

DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-dd") & " 09:00","yyyy-MM-dd HH:mm",New System.Globalization.CultureInfi("En-GB"))

And it works Fine, But i still want to Know why every system parsed the date to a different Value???


回答1:


Default values for missing Year/Month/Day in DateTime.ParseExact
From .NET source code:

The following table describes the behaviors of getting the default value when a certain year/month/day values are missing.
An "X" means that the value exists. And "--" means that value is missing.

Year    Month   Day =>  ResultYear  ResultMonth     ResultDay       Note

X       X       X       Parsed year Parsed month    Parsed day
X       X       --      Parsed Year Parsed month    First day       If we have year and month, assume the first day of that month.
X       --      X       Parsed year First month     Parsed day      If the month is missing, assume first month of that year.
X       --      --      Parsed year First month     First day       If we have only the year, assume the first day of that year.

--      X       X       CurrentYear Parsed month    Parsed day      If the year is missing, assume the current year.
--      X       --      CurrentYear Parsed month    First day       If we have only a month value, assume the current year and current day.
--      --      X       CurrentYear First month     Parsed day      If we have only a day value, assume current year and first month.
--      --      --      CurrentYear Current month   Current day     So this means that if the date string only contains time, you will get current date.


来源:https://stackoverflow.com/questions/42802362/time-parsing-issue-using-datetime-parseexact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!