Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 日期时间格式不正确_C#_Date_Datetime - Fatal编程技术网

C# 日期时间格式不正确

C# 日期时间格式不正确,c#,date,datetime,C#,Date,Datetime,我编写的代码可以以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

我编写的代码可以以
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);
IFormatProvider culture=新系统。全球化。文化信息(“fr-fr”,true);
foreach(fiArray中的FileInfo-fi)
{
尝试
{
StreamReader=fi.OpenText();
字符串日期;
字符串logContent=reader.ReadLine();
string patternDate=“(?(\\d){2}-(\\d{2})-(\\d{4})\\s(\\d{2}):(\\d{2})\\s?(?i)(am | pm))”;
Regex reg=新的Regex(patternDate);
date=reg.Match(logContent.Value.ToString();
//对于dt2,错误发生在这里
DateTime dt2=DateTime.Parse(日期、区域性、系统性、全球化、DateTimeStyles.AssumeLocal);
//DateTime dt2=DateTime.ParseExact(日期、格式、提供程序);
而((line=reader.ReadLine())!=null)
{
正则表达式reg1=新正则表达式(“^(ARCH(?!9{2}))”;
bool flag=reg1.IsMatch(行);
如果(标志==真)
{
字符串dt=DateTime.Now.ToString(“yyyymmddHHMMss”);
string[]values=line.Split(',')。选择(sValue=>sValue.Trim()).ToArray();
//字符串uniqueGuid=SequentialGuidGenerator.NewGuid().ToString();
字符串uniqueGuid=Utility.generateID();
uniqueGuid=uniqueGuid.Replace(“-”,”).Substring(0,18);
字符串RPT_ID=值[0]。ToString();
RPT_ID=RPT_ID.Remove(0,4);
table.Rows.Add(uniqueGuid,RPT_ID,值[1].ToString(),dt2);
}
其他的
{ }
}
reader.Close();
}
捕获(MyException e)
{
抛出e.MyExceptiona(e,fi);
}
}
实用程序.插入数据(表);
此代码正在读取
01-02-2016 12:40 AM
,但我需要它读取
01-15-2016 12:40 AM

如何:

DateTime mydt;
DateTime.TryParseExact("01-16-2016 12:40 AM", "MM-dd-yyyy hh:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out mydt)
有关custome datetime解析的参考信息:

请尝试以下方法

DateTime parsedDate ;
string YourDate = "01-15-2016 12:40 AM";
string pattern = "MM-dd-yyyy hh:mm tt";
DateTime.TryParseExact(YourDate, pattern, null,
                               DateTimeStyles.None, out parsedDate);
谢谢,但是它将返回所有输入值的“{1/1/0001 12:00:00 AM}”。