문제

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.

도움이 되었습니까?

해결책

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");
  }

다른 팁

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top