我已经为解析ascii文件做了一个通用的解析器.当我想解析日期时,我使用DateTime对象中的ParseExact函数来解析,但是我遇到了年份的问题.
要解析的文本是"090812",其中parseExact字符串为"yyMMdd".
我希望得到一个DateTime对象说"12/8-2009",但我得到"12/8-1909".我知道,我可以通过之后解析它来制作一个丑陋的解决方案,从而修改年份..
有人知道解决这个问题的聪明方法吗?
提前致谢..
索伦
Jon*_*eet 18
这样做的理论上优雅的方式:改变TwoDigitYearMax的财产Calendar所用的DateTimeFormatInfo你使用解析文本.例如:
CultureInfo current = CultureInfo.CurrentCulture;
DateTimeFormatInfo dtfi = (DateTimeFormatInfo) current.DateTimeFormat.Clone();
// I'm not *sure* whether this is necessary
dtfi.Calendar = (Calendar) dtfi.Calendar.Clone();
dtfi.Calendar.TwoDigitYearMax = 1910;
Run Code Online (Sandbox Code Playgroud)
然后dtfi在你的电话中使用DateTime.ParseExact.
实际操作方法:在输入开头添加"20",并用"yyyyMMdd"解析.