C# Syntax help - Set bool if date is greater than 12 months

I'm trying to set employee.HireDateAdjusted to true if the date from employee.HireDate is greater that 12 months. I think I am close, but i cannot seem to get the boolean syntax right

                    if (dt.Rows.Count > 0)
                    {
                        employee.HireDate = Convert.ToDateTime(dt.Rows[0]["HireDate"]).ToShortDateString();
                        if (employee.HireDate > DateTime.Today.AddMonths(12).ToString())
                        {
                            employee.HireDateAdjusted = true;
                        }
                        else
                        {
                            employee.HireDateAdjusted = false;
                        }
                    }

Open in new window

brendanlefavreAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Ah. I did not scroll far enough to the right. I did not see your ToString call on the converted date. You will need to change line 4 to:

if (Convert.ToDateTime(dt.Rows[0]["HireDate"]) > DateTime.Today.AddMonths(12))

Open in new window

0
 
käµfm³d 👽Commented:
Remove the ToString call in line 4.
0
 
käµfm³d 👽Commented:
Also, a shortcut:

employee.HireDateAdjusted = (employee.HireDate > DateTime.Today.AddMonths(12));

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
brendanlefavreAuthor Commented:
I'm getting a notification that operator '>' cannot be applied to operands of type string and type System.Date.Time
0
 
David Johnson, CD, MVPOwnerCommented:
are you trying to find out if the employee was hired more than 12 months ago?
if so then

if (employee.HireDate.Addmonths(-12) >= DateTime.Today)
                        {
                          // Hired more than 12 months ago
                            employee.HireDateAdjusted = true;
                        }
                        else
                        {
                            // hired less than 12 months ago
                          employee.HireDateAdjusted = false;
                        }
                    }
0
 
käµfm³d 👽Commented:
Have you removed the ToString call as I demonstrated above?
0
 
brendanlefavreAuthor Commented:
yes, the ToString was removed

The HireDate is of type string, and it's value is coming from a WCF service that is retreiving it's values from a query to an Oracle Linked server.

        [DataMember]
        public string HireDate;

if i change the HireDate type to DateTime, the above methods will compile properly, but the values from the linked server get set to 1/1/1900
0
 
brendanlefavreAuthor Commented:
worked perfectly!

thanks for the help,
Jason
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.