我有一个程序可以将不规则的日期和时间字符串转换为系统日期时间。
但由于系统无法识别不规则字符串,因此.ParseExact、toDateTime和TryParse方法不起作用。
程序只需要转换两种类型的日期时间字符串:
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"));
我真的会避免使用正则表达式并使用已经内置的.NET(尝试精确解析 http://msdn.microsoft.com/en-us/library/ms131044.aspx方法和日期格式 http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx):
DateTime result;
string dateToParse = "Thu Dec 9 05:12:42 2010";
string format = "ddd MMM d HH:mm:ss yyyy";
if (DateTime.TryParseExact(
dateToParse,
format,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
out result)
)
{
// The date was successfully parsed => use the result here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)