asp.net 日期比较代码
asp教程.net 日期比较代码
下面我们有二种方法对日期进行对比,并且算出两个日期之前的相差天数,与相差小时等。
//简单易懂日期比较
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;
//完整的封装类
public enum enumdatecompare
{
year = 1,
month = 2,
day = 3,
hour = 4,
minute = 5,
second = 6
}/// <summary>
/// 日期比较函数
/// </summary>
/// <param name="howtocompare"></param>
/// <param name="startdate">www.zzzyk.com</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) * 12);
break;
case enumdatecompare.day:
diff = convert.todouble(ts.totaldays);
break;
case enumdatecompare.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;
}
//封装一下用起来比较方便 如:
datediff(enumdatecompare.hour,starttime,stoptime)
补充:asp.net教程,.Net开发