Link to home
Start Free TrialLog in
Avatar of brendanlefavre
brendanlefavreFlag for United States of America

asked on

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

Remove the ToString call in line 4.
Also, a shortcut:

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

Open in new window

Avatar of brendanlefavre

ASKER

I'm getting a notification that operator '>' cannot be applied to operands of type string and type System.Date.Time
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;
                        }
                    }
Have you removed the ToString call as I demonstrated above?
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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
worked perfectly!

thanks for the help,
Jason