我正在尝试将日志文件日期格式转换为对象dateTime。
但是,我无法找到用于转换字符串格式的字符串格式?
有谁能帮我写一下格式吗:
日志文件行:-2014年12月28日开始16:53:47.48“
我的代码:
string pattern1 = @"(\d+)[/](\d+)[/](\d+)";
Match match1 = Regex.Match(lineOfLog, pattern1, RegexOptions.IgnoreCase);
if (match1.Success)
{
string dateFormat = "dd/MM/yyyy HH:mm:ss.zzz";
string dateString = match1.Groups[1].Value;
DateTime date = new DateTime();
try
{
date = DateTime.ParseExact(dateString, dateFormat, CultureInfo.InvariantCulture);
}
catch
{
}
}异常:“字符串不被识别为有效的日期时间。
发布于 2014-12-29 14:31:43
这里有三个问题:
zzz,当它应该是FF (百分之一),或者是FFF (数千)。试着做这样的事情:
string lineOfLog = "- Started 28/12/2014 16:53:47.48";
string dateFormat = "dd/MM/yyyy HH:mm:ss.FF";
string pattern1 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\.(\d+)";
Match match1 = Regex.Match( lineOfLog, pattern1, RegexOptions.IgnoreCase );
if( match1.Success )
{
var dateString = match1.Value; // note the change here
var d = DateTime.ParseExact( dateString, dateFormat, CultureInfo.InvariantCulture );
}请注意,您可以完全省略(),它们并不真正有任何好处,但它们确实使正则表达式更容易阅读(IMHO)。
https://stackoverflow.com/questions/27690577
复制相似问题