Solved

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

Posted on 2013-06-20
8
369 Views
Last Modified: 2013-06-20
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

0
Comment
Question by:brendanlefavre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39264585
Remove the ToString call in line 4.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39264588
Also, a shortcut:

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

Open in new window

0
 

Author Comment

by:brendanlefavre
ID: 39264627
I'm getting a notification that operator '>' cannot be applied to operands of type string and type System.Date.Time
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 39264633
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39264692
Have you removed the ToString call as I demonstrated above?
0
 

Author Comment

by:brendanlefavre
ID: 39264794
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39264815
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
 

Author Closing Comment

by:brendanlefavre
ID: 39264892
worked perfectly!

thanks for the help,
Jason
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question