asp.net date comparison code

Source: Internet
Author: User

ASP tutorial. NET Date Comparison code
Here are two ways to compare dates, and calculate the number of days before two, and the difference between hours.

Easy to understand date comparisons

datetime startdate = DateTime.Parse ("2010-7-21 00:00:00");
DateTime enddate = DateTime.Parse ("2010-7-22 00:00:00");
TimeSpan ts = enddate.subtract (startdate);
Double days = ts.totaldays;
Double hours = ts.totalhours;


Complete Package Class

public enum Enumdatecompare
{
Year = 1,
month = 2,
Day = 3,
hour = 4,
minute = 5,
Second = 6
}

 ///<summary>
 ///Date comparison function
 ///</summary>
 ///<param name= "Howtoco Mpare "></param>
 ///<param name=" StartDate ">WWW.111CN.NET</PARAM>
 ///< param name= "EndDate" ></PARAM>
 ///<returns></returns>
  public double DateDiff (Enumdatecompare howtocompare, System.DateTime startdate, System.DateTime enddate)
  {
  Double diff = 0;
  System.TimeSpan ts = new System.TimeSpan (enddate.ticks-startdate.ticks);

  switch (howtocompare)
  {
  case enumdatecompare.year:
  diff = convert.todouble (ts. TOTALDAYS/365);
  break;
  Case Enumdatecompare.month:
  diff = convert.todouble (ts.totaldays/365) *;
  break;   Case Enumdatecompare.day:
  diff = convert.todouble (ts.totaldays);
  break;
  case en Umdatecompare.hour:
  diff = convert.todouble (ts.totalhours);
  break;
  case Enumdatecompare.minute:
  diff = convert.todouble (ts.totalminutes);
  break;
  case Enumdatecompare.second:
  diff = convert.todouble (ts.totalseconds);
  break;
 }
  return diff;
 }
//encapsulation for ease of use such as:
DateDiff (enumdatecompare.hour,starttime,stoptime)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.