DateTime.Parse不会根据线程文化反转月和日 [英] DateTime.Parse not reversing month and day based on thread culture
问题描述
根据这个MSDN文章我应该只能使用Parse,只有日期字符串作为参数。我也应该能够明确提供一个文化对象。无论是工作还是我的日期都保持为mm / dd,而不是dd / mm。我已经验证了线程的CurrentCulture和CurrentUICulture都设置正确,如果我做了一个新的DateTime,那么输出正确。
我错过了什么?
编辑:没有什么奇怪的代码,只是.NET API。
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentCulture =文化;
Thread.CurrentThread.CurrentUICulture =文化;
DateTime formattedDate = DateTime.Parse(5/9/2014);
formattedDate.ToShortDateString(); // this return 5/9/2014
DateTime.Today.ToShortDateString(); //返回9/5/2014
问题你所拥有的是,5/9/2014是一个完全有效的月份字符串,以dd / mm / yyyy或mm / dd / yyyy格式,所以当你做 DateTime.Parse(5/9/2014 )
它将成功解析为2014年9月5日(由于es-es日期格式为dd / mm / yyyy)。
然后解释为什么当你输出你得到的东西不同于 DateTime.Today
(这显然是5月9日)。
您的程序的工作版本将是:
var outputCulture = CultureInfo.CreateSpecificCulture(es-es);
var inputCulture = CultureInfo.CreateSpecificCulture(en-us);
Thread.CurrentThread.CurrentCulture = outputCulture;
Thread.CurrentThread.CurrentUICulture = outputCulture;
DateTime formattedDate = DateTime.Parse(5/9/2014,inputCulture);
Console.WriteLine(formattedDate.ToShortDateString()); // this returns 09/05/2014
Console.WriteLine(DateTime.Today.ToShortDateString()); //这返回09/05/2014
正如你所看到的,我指定输入的文化,它知道使用en-us文化,而不是明确设定es文化解析。
I'm parsing a date string from a database so that I can display it in the current culture of the UI thread. For some reason, the date is not parsing with respect to the culture - specifically, I'm parsing a en-US date to switch to a es-ES date and the month/day positions are not switching.
According to this MSDN article I should just be able to use Parse with only the date string as a parameter. I should also be able to explicitly provide a culture object. Neither works and my date remains as mm/dd instead of dd/mm. I've verified that both the thread's CurrentCulture and CurrentUICulture are set properly and if I do a new DateTime, that outputs correctly.
Am I missing something?
EDIT: Nothing fancy in the code, just the .NET API.
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
DateTime formattedDate = DateTime.Parse("5/9/2014");
formattedDate.ToShortDateString(); //this returns 5/9/2014
DateTime.Today.ToShortDateString(); //this returns 9/5/2014
The problem you are having is that 5/9/2014 is a perfectly valid month string in either dd/mm/yyyy or mm/dd/yyyy format so when you do DateTime.Parse("5/9/2014")
it will successfully parse it as 5th September 2014 (since the es-es date format is dd/mm/yyyy).
This then explains why when you output you get something different to DateTime.Today
(which is obviously 9th May).
A working version of your program would be:
var outputCulture = CultureInfo.CreateSpecificCulture("es-es");
var inputCulture = CultureInfo.CreateSpecificCulture("en-us");
Thread.CurrentThread.CurrentCulture = outputCulture;
Thread.CurrentThread.CurrentUICulture = outputCulture;
DateTime formattedDate = DateTime.Parse("5/9/2014", inputCulture);
Console.WriteLine(formattedDate.ToShortDateString()); //this returns 09/05/2014
Console.WriteLine(DateTime.Today.ToShortDateString()); //this returns 09/05/2014
As you see I am specifying the culture for input so that it knows to use the en-us culture rather than the explicitly set es-es culture for parsing.
这篇关于DateTime.Parse不会根据线程文化反转月和日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!