[英]String was not recognized as a valid DateTime: Format exception
I have path like this..."C:\\restore\\restoredb\\" 我有这样的路径...“ C:\\ restore \\ restoredb \\”
In that path i have files like this.. 在那个路径中,我有这样的文件..
backup-2011-10-12T17-16-51.zip
backup-2011-10-11T13-24-45.zip
I have a form , in that form i have a listbox and combobox(cbrestore) I have got the combobox items like this ...Month, 3 months,6 months,year... 我有一个表单,在那个表单中,我有一个列表框和combobox(cbrestore)我有这样的combobox项...月,3个月,6个月,年...
what i want is , if i select the combobox item(month) i want to display the file names which are stored in that folder between these dates (12-10-2011 to 12-09-2011).. 我想要的是,如果我选择组合框项目(月份),我想显示这些日期(2011年12月10日至2011年12月9日)之间存储在该文件夹中的文件名。
If i select combobox item(3 months) i want to display the file names which are stored in that folder between these dates (12-10-2011 to 12-07-2011)..in listbox 如果我选择组合框项目(3个月),我想显示在这些日期(2011年12月10日至2011年12月7日)之间存储在该文件夹中的文件名。
For that i have tried this ....but, if i select combo box item month then i got the error like i mentioned below 为此,我已经尝试过....但是,如果我选择组合框项目月份,那么我将收到如下所述的错误
List<String> t = Directory.GetFiles(@"C:\restore\restoredb\").ToList();
List<String> y = new List<string>();
List<String> u = new List<string>();
foreach (var zzz in t)
{
y.Add(Path.GetFileName(zzz));
}
if (comboBox1.Text == "Month")
{
u =
(from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 1) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
ToList();
}
Error: Format Exception was unhandled , String was not recognized as a valid DateTime. 错误: 未处理格式异常 ,字符串未被识别为有效的DateTime。
at this line 在这条线
(DateTime.Now.Month - (DateTime.Parse(s.Substring(8, 10)).Month) < 1) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0)
would any pls help on this...... 请问对此有什么帮助...
Many thanks..... 非常感谢.....
I think you've made a mistake in indexes. 我认为您在索引中犯了一个错误。
Try s.Substring(7, 10) instead. 尝试使用s.Substring(7,10)。
It seems to me your index is not correct. 在我看来,您的索引不正确。 You are taking 011-10-12T 您正在011-10-12T
s.Substring(7, 10) s.Substring(7,10)
s.Substring(8, 10) is the string "011-10-12T" based on your inputs, that won't be parsed as a date or a portion of a date. s.Substring(8,10)是基于您输入的字符串“ 011-10-12T”,不会被解析为日期或日期的一部分。
Try to construct a datetime from the inputstring, like: 尝试从输入字符串构造日期时间,例如:
string input = "backup-2011-10-12T17-16-51.zip";
string[] splitInputs = input.Split('-');
DateTime inputDate = new DateTime(
int.Parse(splitInputs[1]), //Year
int.Parse(splitInputs[2]), //Month
int.Parse(splitInputs[3].Split('T')[0]), //Day left of the T
int.Parse(splitInputs[3].Split('T')[1]), //Hour, right of the T
int.Parse(splitInputs[4]), //Minutes
int.Parse(splitInputs[5].Split('.')[0])); //Seconds, left of the .zip
And use that constreucted DateTime to perform your comparisons. 并使用构造好的DateTime进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.