DateTime.TryParse世纪控制C# [英] DateTime.TryParse century control C#

查看:86
本文介绍了DateTime.TryParse世纪控制C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码片段的结果是1930年12月6日12:00:00。我如何控制隐含的世纪,使30年6月12日成为2030年呢?

 字符串dateString =30年6月12日; //从用户输入
    DateTime的结果;
    DateTime.TryParse(dateString,新System.Globalization.CultureInfo(EN-GB),System.Globalization.DateTimeStyles.None,出结果);
    Console.WriteLine(result.ToString());

请抛开,就目前而言,这样一个事实的正确的解决方案是在第一时间正确指定日期。

注意:结果是系统日期时间为运行code PC的独立

答:感谢Deeksy

 的for(int i = 0; I< = 9;我++)
    {
        字符串dateString =6月12日+((INT)I * 10)的ToString();
        Console.WriteLine(解析+ dateString);
        DateTime的结果;
        System.Globalization.CultureInfo的CultureInfo =新System.Globalization.CultureInfo(EN-GB);
        cultureInfo.Calendar.TwoDigitYearMax = 2099;
        DateTime.TryParse(dateString,CultureInfo的,System.Globalization.DateTimeStyles.None,出结果);
        Console.WriteLine(result.ToString());
    }


解决方案

这是棘手的,因为这样两位数的年用的TryParse工作,是基于你所使用的CultureInfo对象的日历属性的TwoDigitYearMax财产。 (CultureInfo->日历 - > TwoDigitYearMax)

为了使两位数的年份有20 prepended,你需要手动创建一个与2099组的TwoDigitYearMax财产Calendar对象CultureInfo对象。不幸的是,这意味着任何分析两位数日期将有20 prepended(包括98,99等),这可能不是你想要的。

我怀疑你最好的选择是使用第三方日期解析库,而不是将使用+ 50 / -50年的统治2位数的年标准的TryParse的。 (一个2位数的年份应该翻译成一系列,今年前50年,今年大于到50岁之间)。

另外,你可以覆盖日历对象的ToFourDigitYear方法(它是虚拟的),并用它来实现-50 / + 50统治。

The result of the following snippet is "12/06/1930 12:00:00". How do I control the implied century so that "12 Jun 30" becomes 2030 instead?

    string dateString = "12 Jun 30"; //from user input
    DateTime result;
    DateTime.TryParse(dateString, new System.Globalization.CultureInfo("en-GB"),System.Globalization.DateTimeStyles.None,out result);
    Console.WriteLine(result.ToString());

Please set aside, for the moment, the fact that a correct solution is to specify the date correctly in the first place.

Note: The result is independant of the system datetime for the pc running the code.

Answer: Thanks Deeksy

    for (int i = 0; i <= 9; i++)
    {
        string dateString = "12 Jun " + ((int)i * 10).ToString();
        Console.WriteLine("Parsing " + dateString);
        DateTime result;
        System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-GB");
        cultureInfo.Calendar.TwoDigitYearMax = 2099;
        DateTime.TryParse(dateString, cultureInfo , System.Globalization.DateTimeStyles.None, out result);
        Console.WriteLine(result.ToString());
    }

解决方案

It's tricky, because the way two digit years work with TryParse is based on the TwoDigitYearMax property of the Calendar property of the CultureInfo object that you are using. (CultureInfo->Calendar->TwoDigitYearMax)

In order to make two digit years have 20 prepended, you'll need to manually create a CultureInfo object which has a Calendar object with 2099 set as the TwoDigitYearMax property. Unfortunately, this means that any two digit date parsed will have 20 prepended (including 98, 99 etc.) which is probably not what you want.

I suspect that your best option is to use a 3rd party date parsing library instead of the standard tryparse that will use the +50/-50 year rule for 2 digit years. (that a 2 digit year should be translated into a range between 50 years before this year and 50 years greater than this year).

Alternatively, you could override the ToFourDigitYear method on the calendar object (it's virtual) and use that to implement the -50/+50 rule.

这篇关于DateTime.TryParse世纪控制C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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