Solved

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

Posted on 2013-06-20
8
370 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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