我有一个字符串"2/15/50",需要转换成datetime格式,我使用以下代码:
DateTime.TryParse("2/15/50", out dt);我得到的输出是:02/15/1950 12:00:00 AM
预期的输出是:02/15/2050 12:00:00 AM
有什么办法我可以达到后者。欢迎使用其他功能的建议。我也尝试使用Convert.ToDateTime,它返回与TryParse相同的输出。
发布于 2020-12-15 17:07:52
这应该对你有用,
CultureInfo ci = new CultureInfo("en-US");
ci.Calendar.TwoDigitYearMax = 2099;
DateTime.TryParse("2/15/50", ci, System.Globalization.DateTimeStyles.None, out DateTime dt)下面是该方法的文档,ParseExact允许您指定格式等https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=net-5.0
这里是我获得文化信息代码https://www.hanselman.com/blog/how-to-parse-string-dates-with-a-two-digit-year-and-split-on-the-right-century-in-c的地方
https://stackoverflow.com/questions/65310144
复制相似问题