我写的代码可以以dd/mm/yyyy hh:mm AM/PM
格式读取日期,但现在我需要它以格式读取日期,如果mm-dd-yyyy hh:mm Am/PM
。不正确的日期时间格式
有人能指导我正确的方向吗?
我的代码:
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
foreach (FileInfo fi in fiArray)
{
try
{
StreamReader reader = fi.OpenText();
string date;
string logContent = reader.ReadLine();
string patternDate = "(?<logDate>(\\d){2}-(\\d{2})-(\\d{4})\\s(\\d{2}):(\\d{2})\\s?(?i)(am|pm))";
Regex reg = new Regex(patternDate);
date = reg.Match(logContent).Value.ToString();
// for dt2, the error happens here
DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
// DateTime dt2 = DateTime.ParseExact(date, format, provider);
while ((line = reader.ReadLine()) != null)
{
Regex reg1 = new Regex("^(ARCH(?!9{2}))");
bool flag = reg1.IsMatch(line);
if (flag == true)
{
string dt = DateTime.Now.ToString("yyyymmddHHMMss");
string[] values = line.Split(',').Select(sValue => sValue.Trim()).ToArray();
// string uniqueGuid = SequentialGuidGenerator.NewGuid().ToString();
string uniqueGuid = Utility.generateID();
uniqueGuid = uniqueGuid.Replace("-", "").Substring(0,18);
string RPT_ID = values[0].ToString();
RPT_ID = RPT_ID.Remove(0, 4);
table.Rows.Add(uniqueGuid, RPT_ID, values[1].ToString(), dt2);
}
else
{ }
}
reader.Close();
}
catch (MyException e)
{
throw e.MyExceptiona(e, fi);
}
}
Utility.InsertData(table);
此代码正在读取01-02-2016 12:40 AM
但我需要它来阅读01-15-2016 12:40 AM
谢谢,但它对所有输入值都返回“{1/1/0001 12:00:00 AM”。 – vish1990