Solved

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

Posted on 2013-06-20
8
365 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
  • 4
  • 3
8 Comments
 
LVL 74

Expert Comment

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

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
 
LVL 78

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now