DateTime TryParse - 映射'99'到2099,而不是1999 [英] DateTime TryParse - mapping '99' to 2099, not 1999

查看:152
本文介绍了DateTime TryParse - 映射'99'到2099,而不是1999的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

DateTime.TryParse世纪控制C#

我需要TryParse一个字符串,如下所示:01/01/99到一个DateTime对象,我使用这样的东西:

I need to TryParse a string such as the following: "01/01/99" into a DateTime object, and I'm using something like this:

DateTime outDate;
if (DateTime.TryParseExact("01/01/99", "dd/MM/yy", null, System.Globalization.DateTimeStyles.None, out outDate))
{
     //Do stuff here
}

当然,这个日期被解析为01/01 / 1999年,这不是我想要的 - 我想要解析为2099年。有一个简单的方法来做到这一点吗?悲伤地修改我正在解析的数据,包括全年不是一个选项。

Of course, this date gets parsed as 01/01/1999, which is not what I want - I want it to parse as 2099. Is there an easy way to do this? Sadly modifying the data I'm parsing to include the full year is not an option.

推荐答案

取自这个答案,您可以提供 ParseExact()与文化对象。可疑 TryParseExact()将是一样的:

Taken from this answer, you can supply ParseExact() with a culture object. Suspect TryParseExact() would be the same:

CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.LCID);
ci.Calendar.TwoDigitYearMax = 2099;
//Parse the date using our custom culture.
DateTime dt = DateTime.ParseExact(input, "MMM-yy", ci);

这篇关于DateTime TryParse - 映射'99'到2099,而不是1999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆