我有一个程序,它将不规则的日期和时间字符串转换为系统日期时间。C#如何将不规则的日期和时间字符串转换为DateTime?
但是,由于系统无法识别不规则的字符串,因此.ParseExact,toDateTime和TryParse方法无法使用。
只有2种,该方案需要转换日期时间字符串:
Thu Dec 9 05:12:42 2010
Mon Dec 13 06:45:58 2010
请注意,单日有,我已经使用了.replace方法将单日期转换的双重间距到Thu Dec 09 05:12:42 2010
。
可能有人请告诉代码?谢谢!
的代码:
String rb = re.Replace(" ", " 0");
DateTime time = DateTime.ParseExact(rb, "ddd MMM dd hh:mm:ss yyyy", CultureInfo.CurrentCulture);
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
@Darin:正则表达式是只存在于提取日期来自更大的字符串。看到他的[早期问题](http://stackoverflow.com/questions/4426597/c-how-to-retrieve-a-certain-text-located-within-a-string)。 – AgentConundrum 2010-12-14 07:29:27
@AgentConundrum,从我可以看到他正在使用正则表达式来替换原始字符串中的空格,在一天的开始时添加0,等等......正确的格式字符串不需要的东西。 – 2010-12-14 07:31:25
@Darin:你说得对。我掩饰了第二个正则表达式。对于那个很抱歉。 – AgentConundrum 2010-12-14 07:32:28