Question

Can you help me in removing the time in my code or rather correct my code for possible errors. Thanks. Here's my code and ill state the error later.

else if (this.dateTimePicker1.Value != DateTime.Now)
                    {
                        this.chkBxLessNinety.Enabled = false;
                        string dateInString = Convert.ToString(Convert.ToDateTime(_dr[4]));
                        DateTime startdate = DateTime.Parse(dateInString);
                        DateTime datelimit = startdate.AddDays(90);
                        //string date = Convert.ToString(Convert.ToDateTime(datelimit.Date).ToString("mm/dd/yyyy"));

                        string mydate1 = this.dateTimePicker1.Value.ToShortDateString();
                        if (mydate1 > datelimit)
                        {
                            MessageBox.Show("Cannot Sync data more or equal to 90 days");
                        }
                        else
                        {
                        }

the line if (mydate1 > datelimit) shows an error which says > cannot be applied as operand of type string an datetime.

Please help. Thanks in advance.

Was it helpful?

Solution

You want to compare DateTimes with each other. Since you want to exclude the time portion then the Date property will make both dates at midnight hour.

 DateTime mydate1 = this.dateTimePicker1.Value;
 if (mydate1.Date > datelimit.Date)
  {
          MessageBox.Show("Cannot Sync data more or equal to 90 days");
  }

OTHER TIPS

Just remove .ToShortDateString()

And also:

string dateInString = Convert.ToString(Convert.ToDateTime(_dr[4]));
DateTime startdate = DateTime.Parse(dateInString);

Don't convert from DateTime to string and then back to DateTime, it's pointless

You can't use the > to compare a string and a DateTime. Instead, you should replace

string mydate1 = this.dateTimePicker1.Value.ToShortDateString();

with

DateTime mydate1 = this.dateTimePicker1.Value;

This way, you'll be comparing things of the same type (DateTime).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top