简体   繁体   English

C#:将字符串转换为DateTime

[英]C#: convert string to DateTime

I want to parse strings with date that can have different formats like: 我想解析日期可能具有不同格式的字符串:

"21.12.12", "4,12,2011", "30 Jun 11", "16 12 2013" , "April 2013", "12. April 2012", "12, März 2011". “ 21.12.12”,“ 4,12,2011”,“ 11月30日”,“ 2013年12月16日”,“ 2013年4月”,“ 2012年4月12日”,“ 2012年12月12日”。

I have this code: 我有以下代码:

string[] ll = {"en-US", "de-DE"}; 
date = "4,12,2011";
foreach (string l in ll) {
   if (DateTime.TryParse(date, new CultureInfo(l),
                            DateTimeStyles.None, out pDate)) {
       return pDate;//.ToString("dd.MM.yyyy");
   }
}

And I have problems with dates like this: 我有这样的日期问题:

"21.12.12" is parsed like "21 December 2012" , and it is OK “ 2012年12月21日”那样解析“ 21.12.12” ,就可以了

"4,12,2011" is parsed like "12 April 2011" , it is not OK, I need "4 December 2011" “ 2011年4月12日”那样解析“ 4,12,2011” ,这不行,我需要“ 2011年12月4日”

How to set order for Day and Month? 如何设置日期和月份的顺序?

It must be Day before Month . 一定是前一个月

若要指定要传递的字符串的格式,应使用ParseExact方法。

Use DateTime.ParseExact , it has also an overload tha allows to pass a string[[] for all allowed formats. 使用DateTime.ParseExact ,它也有一个重载,它允许为所有允许的格式传递string[[]

string[] dates = new[] { "21.12.12", "4,12,2011", "30 Jun 11", "16 12 2013", "April 2013", "12. April 2012", "12, März 2011" };
CultureInfo germanCulture = CultureInfo.CreateSpecificCulture("de-DE"); // you are using german culture
string[] formats = new[] { "dd/MM/yy", "d,MM,yyyy", "dd MMM yy", "dd MM yyyy", "MMMM yyyy", "dd. MMMM yyyy", "dd, MMMM yyyy"};

foreach (string dateString in dates)
{
    DateTime dt = DateTime.ParseExact(dateString, formats, germanCulture, DateTimeStyles.None);
    Console.WriteLine(dt.ToString());
}

I have used german culture because your date-strings contain german month names. 我使用德国文化是因为您的日期字符串包含德国月份的名称。 So this code works even if the current culture is different. 因此,即使当前的文化不同,此代码也可以工作。

All of the test dates that you gave actually parse correctly in the de-DE culture that you specify. 您提供的所有测试日期实际上都在您指定的de-DE文化中正确解析。 The problem comes that you try to parse it in the american culture first where they use mm.dd.yyyy style formats. 问题来了,您首先尝试在美国文化中使用mm.dd.yyyy样式格式对其进行解析。

The correct solution in general is to always make sure you know what culture you are using when parsing the string rather than guessing. 通常,正确的解决方案是始终确保您在解析字符串时(而不是猜测)知道所使用的区域性。 If you have to guess you will get these kinds of problems at times. 如果您必须猜测,有时会遇到这类问题。

In this case though it looks like they are all acceptable de-DE date strings so you can just parse them as that without needing the loop of trying different cultures (which as mentioned is probably never likely to be a perfect result). 在这种情况下,尽管看起来它们都是可接受的de-DE日期字符串,所以您可以将它们解析为这样,而无需尝试不同文化的循环(如上所述,这可能永远不可能是完美的结果)。

According to your code 根据你的代码

  string[] ll = {"en-US", "de-DE"}; 

you initially try parse DateTime with "en-US" culture; 您最初尝试使用“ en-US”文化解析DateTime; so the "4,12,2011" will be parsed as americans do - MM/DD/YYYY - month the first (12 April). 因此,“ 4,12,2011”将像美国人一样被解析-MM / DD / YYYY-第一个月(4月12日)。 Change order in your array 更改阵列中的顺序

  string[] ll = {"de-DE", "en-US"}; 

and "4,12,2011" will be 4 December 而“ 4,12,2011”将是12月4日

This is specific for the en-US culture. 这是特定于en-US文化的。 It may be strange for us Europeans, but Americans really write month before day in dates. 对于我们欧洲人来说,这可能很奇怪,但是美国人确实在约会中写着一个月一个月的日期。 You may use en-GB instead - it will handle the same names of months and the European order. 您可以改用en-GB它可以处理相同的月份名称和欧洲订单。

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

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