我将时间值存储为格式为HH:mm
我使用以下代码将其解析为Today Date
的日期DateTime.ParseExact("09:00","HH:mm",New System.Globalization.CultureInfi("En-GB"))
结果为2017-03-15 09:00:00
运行此代码时从另一台计算机上我得到以下结果:1899-12-31 09:00:00
我将我的代码替换为以下
DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-dd") & " 09:00","yyyy-MM-dd HH:mm",New System.Globalization.CultureInfi("En-GB"))
它工作正常,但我仍然想知道为什么每个系统都将日期解析为不同的值???
答案 0 :(得分:1)
DateTime.ParseExact
中缺少的年/月/日的默认值
来自.NET源代码:
下表介绍了获取默认值的行为
当某个年/月/日值缺失时。
一个" X"表示该值存在。和" - "意味着价值缺失。
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.