Check valid date range using c#

Posted on 2007-10-07
Last Modified: 2008-01-09
I am new to C# and have a web site which gets a couple dates from the user.  I would like to be able to compare the dates to make sure that the second date is within one day prior or same as the first date.
For example, date 1 is 02/15/2007 date 2 02/14/2007 or 02/15/2007 would be acceptable

Need fast help.
Question by:mbart
    LVL 29

    Expert Comment

    C# doesn't have a DateDiff function like vb so you will need to choose one of the options listed here

    Using day as the interval you can pass date1 and date2, if the result of the function is > 1 then - error!
    LVL 22

    Accepted Solution

    Assuming the first date is in a DateTime structure called dateOne, and the second in a DateTime structure called dateTwo:
    DateTime start = dateOne.Date;
    DateTime end = dateTwo.Date;
    TimeSpan ts = end.Subtract(start);
    if ((ts.Days == 1) || (ts.Days == 0))
       // It is valid
       // It is invalid


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now