简体   繁体   English

使用bg-BG文化和dd-MMM-yyyy格式解析11月的日期时间。

[英]Parsing a date time in november using bg-BG culture and dd-MMM-yyyy format.

I am working on localization code for dates and times, and I am seeing an odd behavior when the culture is set to Bulgarian, and the date I pass in is in November. 我正在研究日期和时间的本地化代码,当文化被设置为保加利亚语时,我看到一种奇怪的行为,我传递的日期是11月。

Essentially, I am trying to call: 基本上,我试着打电话:

if (DateTime.TryParse(theDateToParse, formatProvider, DateTimeStyles.None, out localDate))
{ //Do code work here }

Where theDateToParse = 15-Нов-2013 15:20:39 (This value comes from a jquery datepicker, with a time appended to it, and is November 15th) and the formatProvider is the bg-BG culture information. 其中theDateToParse = 15-Нов-2013 15:20:39(此值来自jquery日期选择器,附加时间,并且是11月15日),formatProvider是bg-BG文化信息。

Testing other values, and other cultures, it would appear that the problem is specific to any date in November in the bg-BG culture. 测试其他价值观和其他文化,似乎问题特定于11月bg-BG文化中的任何日期。 Additionally, if I change it to use the full month name, it can parse it correctly. 此外,如果我将其更改为使用完整的月份名称,它可以正确解析它。

To test this theory, I went the other way around and created a dateTime object for a day in each month and converted them to strings using the dd-MMM-yyyy format. 为了测试这个理论,我反过来为每个月的一天创建了一个dateTime对象,并使用dd-MMM-yyyy格式将它们转换为字符串。 For each month I got a shortened month name, except for November, where I got a full month name. 对于每个月我都有一个缩短的月份名称,除了11月,我有一个完整的月份名称。

I was just looking to see if anyone had any background around why this is happening, or had any clever solutions to make this case work. 我只是想看看是否有人有关于为什么会发生这种情况的任何背景,或者有任何聪明的解决方案来使这个案例有效。 I have spent some time searching, and I can't even find someone that has come across the same problem. 我花了一些时间搜索,我甚至找不到遇到同样问题的人。

Running this code: 运行此代码:

var culture = CultureInfo.CreateSpecificCulture("bg-BG");
foreach (var name in culture.DateTimeFormat.AbbreviatedMonthNames)
    Debug.WriteLine(name);

Gives these month name abbreviations: 给出这些月份名称缩写:

яну фев мар апр май юни юли авг сеп окт ное дек януфевмарапрмайюниюлиавгсепоктноедек

It would seem that .Net has the abbreviation for November in Bulgarian to be "ное", not the "Нов" string in your input. 似乎.Net在保加利亚语中将11月的缩写设为“ное”,而不是输入中的“Нов”字符串。

I don't speak Bulgarian, but Google Translate accepts "ное" as "November", but "Нов" is translated to English as "new". 我不会说保加利亚语,但谷歌翻译接受“ное”为“11月”,但“Нов”被翻译为英语为“新”。 So my guess is that the source data is incorrect. 所以我的猜测是源数据不正确。

The best solution is to never transmit dates as localized strings between client and server. 最好的解决方案是永远不要将日期作为本地化字符串在客户端和服 Instead, use an ISO8601 format, such as 2013-11-15T15:20:39 . 而是使用ISO8601格式,例如2013-11-15T15:20:39

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM