Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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# 字符串到日期时间dd/mm/yy错误_C#_Datetime - Fatal编程技术网

C# 字符串到日期时间dd/mm/yy错误

C# 字符串到日期时间dd/mm/yy错误,c#,datetime,C#,Datetime,我正试图从我的程序将要扫描的文件名中获取一个DateTime。DateTime格式为dd/mm/yy。每次我看到这行代码时: Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf(““+1”),6),“dd/mm/yy”,null),file) 程序不做任何操作就中断到应用程序。它不会给我一个错误消息和任何关于正在发生什么的信息。我确信我写这篇文章的方式有些混乱,所以请让我知道你需要澄清什么 下面是一

我正试图从我的程序将要扫描的文件名中获取一个
DateTime
DateTime
格式为dd/mm/yy。每次我看到这行代码时:

Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf(““+1”),6),“dd/mm/yy”,null),file)

程序不做任何操作就中断到应用程序。它不会给我一个错误消息和任何关于正在发生什么的信息。我确信我写这篇文章的方式有些混乱,所以请让我知道你需要澄清什么

下面是一个有帮助的文件名的例子


每周时间和费用表\u 073111

您需要使用大写字母
MM
来表示
月份
,而不是小的
MM

从您的示例日期字符串
07311011
来看

前两位数字-
07
=>
月份

下两位数字-
31
=>
日期

接下来的四位数字-
2011
=>
Year

因此,您的格式应为
MMddyyyy

编辑:关闭
LastIndexOf()
函数后,需要添加1

试试这个:

foreach (var file in Directory.GetFiles(@"C:\Users\Andrew\Desktop\Timesheets\2011", "*.xlsx", SearchOption.AllDirectories)){             
  Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf("_" + 1), 6), "dd/mm/yy", null), file);
  _Paychecks.Add(p);
}

您需要使用大写字母
MM
来表示
Month
,而不是小写的
MM

从您的示例日期字符串
07311011
来看

前两位数字-
07
=>
月份

下两位数字-
31
=>
日期

接下来的四位数字-
2011
=>
Year

因此,您的格式应为
MMddyyyy

编辑:关闭
LastIndexOf()
函数后,需要添加1

试试这个:

foreach (var file in Directory.GetFiles(@"C:\Users\Andrew\Desktop\Timesheets\2011", "*.xlsx", SearchOption.AllDirectories)){             
  Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf("_" + 1), 6), "dd/mm/yy", null), file);
  _Paychecks.Add(p);
}
试试这个

将字符串转换为日期时间时, 使用

Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(
                   file.LastIndexOf("_") + 1, 6), "MMddyyyy", null), file);
试试这个

将字符串转换为日期时间时, 使用

Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(
                   file.LastIndexOf("_") + 1, 6), "MMddyyyy", null), file);

将这一行一分为二-在第一行创建
DateTime
实例..并使用调试程序查看它,这样做
DateTime test=DateTime.ParseExact(file.Substring(file.LastIndexOf(“”+1),8),“dd/MM/yy”,null);工资支票p=新工资支票(测试,文件)将这一行一分为二-在第一行创建您的
日期时间
实例..并使用调试程序查看它,像这样吗
DateTime test=DateTime.ParseExact(file.Substring(file.LastIndexOf(“”+1),8),“dd/MM/yy”,null);工资支票p=新工资支票(测试,文件)仍然只是中断到应用程序,而不执行code@teepee:检查我编辑的答案,你的格式应该是
mmddyyy
好的,我刚刚意识到有些文件是MMddyy,有些是ddMMyy,我手动将粘贴到这篇文章上的文件更改为0731“20”11作为一个测试,我试图解决我的问题,但在我发布之前意外忘记了解决它。但是,程序尝试扫描的第一个文件的格式为MMddyy,并且它仍在断开,没有任何错误。要尝试简化此操作,请知道我的程序正在扫描的第一个文件的格式为MMddyy“073111”,但程序在运行格式化为的代码时仍在断开
Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf(“”+1),6),“MMddyy”,null),file)
@teepee:关闭lastindexof函数后需要添加1,请尝试此
Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.lastindexof(“”)+1,6),“MMddyyyy”,null),file)检查我编辑的回答仍然只是中断到应用程序而不执行code@teepee:检查我编辑的答案,你的格式应该是
mmddyyy
好的,我刚刚意识到有些文件是MMddyy,有些是ddMMyy,我手动将粘贴到这篇文章上的文件更改为0731“20”11作为一个测试,我试图解决我的问题,但在我发布之前意外忘记了解决它。但是,程序尝试扫描的第一个文件的格式为MMddyy,并且它仍在断开,没有任何错误。要尝试简化此操作,请知道我的程序正在扫描的第一个文件的格式为MMddyy“073111”,但程序在运行格式化为的代码时仍在断开
Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf(“”+1),6),“MMddyy”,null),file)
@teepee:关闭lastindexof函数后需要添加1,请尝试此
Paycheck p=newpaycheck(DateTime.ParseExact(file.Substring(file.lastindexof(“”)+1,6),“MMddyyyy”,null),file)检查我编辑的答案