Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I keep on getting the error "string is not recognized as ValidDateTime" for the below code

foreach (FileInfo FI in FileList)
                    {
                        DateTime filedat = FI.CreationTime.Date;
                        DateTime filedate = Convert.ToDateTime(filedat);
                        string fidate = filedate.ToString(format) ;
                        DateTimePicker filedatepkr = new DateTimePicker();
                        //filedatepkr.CustomFormat = "dd/MM/yyyy";
                        filedatepkr.Text =Convert.ToString(fidate) ;
                        
                       if (DateTime.Compare(filedate, frmdate) >= 0 && (DateTime.Compare(filedate, todate )<= 0))
                        {
                        }
                        DateTime FileCreatDt = File.GetCreationTime(FI.Name);
                    }


Please Help!!
Posted
Updated 4-Feb-11 19:18pm
v3

The whole idea working with strings is wrong! The class DateTimePicker expose the property Value of the type DateTime. On this type comparison operators are defined, use them. Compare time values, not strings!

DateTimePicker filedatepkr = new DateTimePicker();
...
foreach (FileInfo FI in FileList)
{
    DateTime filedat = FI.CreationTime.Date;
    DateTime filedate = Convert.ToDateTime(filedat);
    if (filedate > filedatepkr) { /* whatever...  */ }
}


—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 5-Feb-11 19:23pm    
My 5. The OP is thinking far too much about this and is trying to reinvent the wheel on this one. This is my choice for the correct answer.
Sergey Alexandrovich Kryukov 5-Feb-11 19:42pm    
Thank you, Marcus.
Not quite the wheel and not "inventing" -- not at all, just old lack of understanding basic things...
--SA
Espen Harlinn 6-Feb-11 6:16am    
The answer, nice and simple, 5+
Sergey Alexandrovich Kryukov 6-Feb-11 12:11pm    
Thank you, Espen.
I was looking at OP's code and all answers in disbelieve: why?
--SA
DateTime filedate = Convert.ToDateTime(filedat);
Here you are converting datetime to again datetime which is not needed.

check this
filedatepkr.Text = DateTime.ParseExact(fidate, "dd/MM/yyyy",null);
 
Share this answer
 
Comments
Eager2Lern 5-Feb-11 1:56am    
foreach (FileInfo FI in FileList)
{
DateTime filedat = FI.CreationTime.Date;
string fidate = filedat.ToString();
DateTimePicker filedatepkr = new DateTimePicker();
//filedatepkr.CustomFormat = "dd/MM/yyyy";
filedatepkr.Text = DateTime.ParseExact(fidate, "dd/MM/yyyy", null);


After this code i get the error
cannot implicitly convert type System.DateTime to String

Please help
m@dhu 5-Feb-11 2:08am    
What does that mean you need to convert to string
filedatepkr.Text = DateTime.ParseExact(fidate, "dd/MM/yyyy", null).ToString();
I think these code something confused ,
foreach (FileInfo FI in FileList)
                    {
                        DateTime filedat = FI.CreationTime.Date;
                        DateTime filedate = Convert.ToDateTime(filedat);
                        string fidate = filedate.ToString(format) ;
                        DateTimePicker filedatepkr = new DateTimePicker();
                        //filedatepkr.CustomFormat = "dd/MM/yyyy";
                        filedatepkr.Text =Convert.ToString(fidate) ;

So i want to suggest ,following code
foreach (string F in FileList)
           {

               DateTime filedat = FI.CreationTime.Date;
               DateTime filedate = Convert.ToDateTime(filedat);
               string fidate = filedate.ToString(format);
               DateTimePicker filedatepkr = new DateTimePicker();
               //filedatepkr.CustomFormat = "dd/MM/yyyy";
               filedatepkr.Value = DateTime.Parse(fidate)

It may be helpful
Theingi Win
 
Share this answer
 
Comments
Eager2Lern 5-Feb-11 2:39am    
Hi,

I am getting the below error now
filedatepkr.Value = DateTime.Parse(fidate)

String was not recognized as a valid DateTime.

Please help
Eager2Lern 5-Feb-11 2:54am    
Please find below the full code

private void ReadButton_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Folder not selected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

return;
}
else
{

string path = textBox1.Text;
if (rad.Checked)
{
string format = "dd/M/yyyy HH:mm:ss";
DateTime frmdate = Convert.ToDateTime(FromDatePkr.Text).Date;
DateTime todate = Convert.ToDateTime(ToDatePkr.Text).Date;
DirectoryInfo Dir = new DirectoryInfo(path);
FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo FI in FileList)
{
DateTime filedat = FI.CreationTime.Date;
string fidate = filedat.ToString(format);
DateTime filedate = DateTime.Parse(fidate);

if (DateTime.Compare(filedate, frmdate) >= 0 && (DateTime.Compare(filedate, todate) <= 0))
{
}

}


}


}
}
Actually i want to convert the file creation time format inorder to work on the compare function in the code
Theingi Win 5-Feb-11 3:15am    
May I know your DateTimePicker's Datetime Format,When i testing your code i don't get error but in my code DateTimePicker's Datetime Format is Long.

Thanks your Reply
Theingi Win

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900