Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5343
  • Last Modified:

Check valid date range using c#

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.
0
mbart
Asked:
mbart
1 Solution
 
QPRCommented:
C# doesn't have a DateDiff function like vb so you will need to choose one of the options listed here http://www.aspcode.net/C-Datediff.aspx

Using day as the interval you can pass date1 and date2, if the result of the function is > 1 then - error!
0
 
JimBrandleyCommented:
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
}
else
{
   // It is invalid
}

Jim
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now