解析“12/25/35"到使用 InvariantCulture 的日期(2 位数年份) [英] Parsing "12/25/35" to a date using InvariantCulture (2-digit year)

查看:32
本文介绍了解析“12/25/35"到使用 InvariantCulture 的日期(2 位数年份)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 InvariantCulture 解析一个字符串时,同时使用:

When I use the InvariantCulture to parse a string, with both:

var christ1 = DateTime.Parse("12/25/35", CultureInfo.InvariantCulture);

和:

var christ2 = DateTime.ParseExact("12/25/35", "MM/dd/yy", CultureInfo.InvariantCulture);

结果取决于运行应用程序的计算机.在一台机器上,我得到了相当于:

the result depends on what computer the application runs on. On one machine I get the equivalent of:

12 月 25 日,2035

December 25, 2035

而在另一台机器上我得到一个世纪前的日期,那就是:

while on another machine I get a date one century earlier, that is:

12 月 25 日,1935

December 25, 1935

这是怎么回事?

推荐答案

所谓的不变文化在这种情况下并不是那么一成不变.这取决于您的操作系统版本和当前用户在操作系统中的区域设置.

The so-called invariant culture is not so invariant in this case. It depends on both your OS version and the current user's regional settings in the OS.

在 .NET 术语中,感兴趣的属性是日历上的 TwoDigitYearMax,因此检查 CultureInfo.InvariantCulture.DateTimeFormat.Calendar.TwoDigitYearMax(new GregorianCalendar()).TwoDigitYearMax.GregorianCalendar.TwoDigitYearMax 的文档覆盖.

In .NET terms, the property of interest is TwoDigitYearMax on the calendar, so inspect CultureInfo.InvariantCulture.DateTimeFormat.Calendar.TwoDigitYearMax or (new GregorianCalendar()).TwoDigitYearMax. Documentation of GregorianCalendar.TwoDigitYearMax override.

在用户未更改区域设置的 Windows 10 版本 1809(2018 年 10 月更新"、Redstone 5")机器上(更多内容见下文),属性 TwoDigitYearMax2029,因此您会得到 12 月 25 日,1935.

On a Windows 10 version 1809 ("October 2018 Update", "Redstone 5") machine where the user has not changed the regional settings (more on that below), the property TwoDigitYearMax is 2029, and as a consequence you get December 25, 1935.

但在 Windows 10 版本 1903(2019 年 5 月更新"、19H1")上,相同的属性返回 2049,因此您的结果是 12 月 25 日,2035.

But on Windows 10 version 1903 ("May 2019 Update", "19H1"), the same property returns 2049, and therefore your result is December 25, 2035.

但是,该值不仅取决于 Windows 版本.用户在区域设置中输入的内容也很重要.所以这并不是真正的不变".

However, the value depends not only on the Windows version. Also what the user inputs in regional settings, matters. So that is not really "invariant".

您可以通过以下方式在控制面板中更改截止年份:

You can change the cutoff year in the Control Panel by:

  1. 点击开始,点击齿轮";左侧的图标(设置),然后单击 Time &语言.
  2. 点击左侧的区域.
  3. 相关设置下,点击其他日期、时间和区域设置.
  4. (您在控制面板中.)点击区域.
  5. (在新的区域窗口中.)点击页面底部附近的其他设置...按钮,然后在新窗口中点击日期标签.
  6. 日历组中,调整当输入两位数的年份时,将其解释为介于两者之间的年份.
  1. Click Start, click "gear" icon on the left (Settings), and click Time & Language.
  2. Click Region on the left.
  3. Under Related settings, click Additional date, time, & regional settings.
  4. (You are in Control Panel.) Click Region.
  5. (In new Region window.) Click button Additional settings... near bottom of page, and in new window click Date tab.
  6. In the Calendar group, adjust When a two-digit year is entered, interpret it as a year between.

在较旧的 Windows 版本中,它可能是(从 microsoft.com 粘贴的):

In older Windows flavors, it may be (pasted from microsoft.com):

  1. 点击开始,指向设置,然后点击控制面板.
  2. 双击区域设置图标.
  3. 点击日期标签.
  4. 输入两位数年份后,解释年份之间框中,输入所需的截止年份,然后单击确定.
  1. Click Start, point to Settings, and then click Control Panel.
  2. Double-click the Regional Settings icon.
  3. Click the Date tab.
  4. In the When a two digit year is entered, interpret a year between box, type the cutoff year that you want, and then click OK.

或者您可以直接使用 Windows 注册表,转到键 HKEY_CURRENT_USER\Control Panel\International\Calendars\TwoDigitYearMax,并为每个相关的值"在该键中,保持 name 组件不变,并将值的 data 组件编辑为您想要的截止年份(通常的十进制字符串表示形式).如果用户从未从控制面板更改此设置,则最里面的两个键 (Calendars\TwoDigitYearMax) 尚不存在.有关与注册表混合的常见警告适用.

Or you can use Windows Registry directly, go to key HKEY_CURRENT_USER\Control Panel\International\Calendars\TwoDigitYearMax, and for every relevant "value" in that key, keep the name component unchanged and edit the data component of the value to (the usual decimal string representation of) the cutoff year you desire. If the user has never changed this setting from the Control Panel, the two innermost keys (Calendars\TwoDigitYearMax) do not exist yet. Usual warnings about mingling with the registry apply.

一旦您在控制面板中更改了年份,在您进行这些更改后启动的每个 .NET 进程都会在通过InvarantCulture!

Once you changed the year in the Control Panel, every .NET process that is started after you made these changes, will reflect your choice in the TwoDigitYearMax property reached through InvarantCulture!

因此,如果您希望跨操作系统版本和用户首选项保持一致的行为,我认为您不能使用 InvariantCulture.相反,这样的事情可以工作:

So if you want consistent behavior across OS versions and user preferences, I think you cannot use InvariantCulture. Instead, something like this could work:

var tmp = (CultureInfo)(CultureInfo.InvariantCulture.Clone());
tmp.DateTimeFormat.Calendar.TwoDigitYearMax = 2039; // any cutoff you need
 // incorrect: tmp.Calendar.TwoDigitYearMax = 2039
var newInvariantCulture = CultureInfo.ReadOnly(tmp);

然后在解析 2 位数年份的日期时使用该 newInvariantCulture 实例(您可以将它放在某个 static readonly 字段中).

then use that newInvariantCulture instance whenever you parse dates with 2-digit years (you can put it in some static readonly field).

警告:每个CultureInfo x 带有两个不同的Calendar,它们可能具有不同的2位数年份最大值,即:

WARNING: Each CultureInfo x carries two distinct Calendars that may have different 2-digit year max, namely:

x.DateTimeFormat.Calendar.TwoDigitYearMax
  !=
x.Calendar.TwoDigitYearMax

这篇关于解析“12/25/35"到使用 InvariantCulture 的日期(2 位数年份)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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