c# 分割字符串 用字符串_如何使用两位数年份解析字符串日期并在C#中在正确的世纪分割

c# 分割字符串 用字符串

c# 分割字符串 用字符串

So you've been asked to parse some dates, except the years are two digit years. For example, dates like "12 Jun 30" are ambiguous...or are they?

因此,系统要求您分析一些日期,但年份是两位数的年份。 例如,像“ 12 Jun 30”这样的日期是模棱两可的……还是?

If "12 Jun 30" is intended to express a birthday, given it's 2019 as the of writing of this post, we can assume it means 1930. But if the input is "12 Jun 18" is that last year, or is that a 101 year old person's birthday?

如果打算将“ 12 Jun 30”表示生日,考虑到本文撰写之日为2019,则可以假设它表示1930。但是如果输入的是“ 12 Jun 18”是去年,或者是101岁生日?

Enter the Calendar.TwoDigitYearMax property.

输入Calendar.TwoDigitYearMax属性

For example, if this property is set to 2029, the 100-year range is from 1930 to 2029. Therefore, a 2-digit value of 30 is interpreted as 1930, while a 2-digit value of 29 is interpreted as 2029.

例如,如果将此属性设置为2029,则100年范围是从1930到2029。因此,将2位数的值30解释为1930,而将2位数的值29解释为2029。

The initial value for this property comes out of the DEPTHS of the region and languages portion of the Control Panel. Note way down there in "additional date, time, & regional settings" in the "more settings" and "date" tab, there's a setting that (currently) splits on 1950 and 2049.

此属性的初始值来自“控制面板”的区域和语言部分的DEPTHS。 请注意,在“更多设置”和“日期”标签的“其他日期,时间和区域设置”中,有一个设置(当前)在1950年和2049年分开。

Two Digit Year regional settings

If you're writing a server-side app that parses two digit dates you'll want to be conscious and explicit about what behavior you WANT so that you're not surprised.

如果您正在编写一个解析两位数日期的服务器端应用程序,那么您将需要意识到并明确知道自己想要什么行为,以免感到惊讶。

Setting TwoDigitYearMax sets a 100 year RANGE that your two digit years will be interpreted to be within. You can also just change it on the current thread's current culture's calendar. It's up to you.

设置TwoDigitYearMax设置一个100年范围,您的两位数年份将被解释为在该范围之内。 您也可以只在当前线程的当前区域性日历上进行更改。 由你决定。

For example, this little app:

例如,这个小应用程序:

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

culture.Calendar.TwoDigitYearMax = 2099;

DateTime.TryParse(dateString, culture, DateTimeStyles.None, out result);
Console.WriteLine(result.ToLongDateString());

gives this output:

给出以下输出:

Thursday, June 12, 1930
Wednesday, June 12, 2030

Note that I've changed TwoDigitYearMax from and moved it up to the 1999-2099 range so "30" is assumed to be 2030, within that 100 year range.

请注意,我已将TwoDigitYearMax从更改为2000-20,并将其上移到1999-2099范围,因此假定“ 30”为2030,即该100年范围内。

Hope this helps!

希望这可以帮助!

翻译自: https://www.hanselman.com/blog/how-to-parse-string-dates-with-a-two-digit-year-and-split-on-the-right-century-in-c

c# 分割字符串 用字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值