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
Solved

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

Posted on 2013-06-20
8
368 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 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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