DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
DateDiff 函数语法中有下列命名参数:
部分 描述
interval 必要。字符串表达式,表示用来计算date1 和 date2 的时间差的时间间隔
Date1□date2 必要;Variant (Date)。计算中要用到的两个日期。
Firstdayofweek 可选。指定一个星期的第一天的常数。如果未予指定,则以星期日为第一天。
firstweekofyear 可选。指定一年的第一周的常数。如果未予指定,则以包含 1 月 1 日的星期为第一周。
俺在C#中想用,结果没有,嘿嘿,写了一个,共享之,可能各位还要其它方法,若有更好,可告之!
public enum DateInterval
{
Second, Minute, Hour, Day, Week, Month, Quarter, Year
}
public sealed class DateTimeManger
{
private DateTimeManger()
{ }//end of default constructor
public static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
{
long lngDateDiffValue = 0;
System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
switch (Interval)
{
case DateInterval.Second:
lngDateDiffValue = (long)TS.TotalSeconds;
break;
case DateInterval.Minute:
lngDateDiffValue = (long)TS.TotalMinutes;
break;
case DateInterval.Hour:
lngDateDiffValue = (long)TS.TotalHours;
break;
case DateInterval.Day:
lngDateDiffValue = (long)TS.Days;
break;
case DateInterval.Week:
lngDateDiffValue = (long)(TS.Days / 7);
break;
case DateInterval.Month:
lngDateDiffValue = (long)(TS.Days / 30);
break;
case DateInterval.Quarter:
lngDateDiffValue = (long)((TS.Days / 30) / 3);
break;
case DateInterval.Year:
lngDateDiffValue = (long)(TS.Days / 365);
break;
}
return (lngDateDiffValue);
}//end of DateDiff
}//end of class
{
Second, Minute, Hour, Day, Week, Month, Quarter, Year
}
public sealed class DateTimeManger
{
private DateTimeManger()
{ }//end of default constructor
public static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
{
long lngDateDiffValue = 0;
System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
switch (Interval)
{
case DateInterval.Second:
lngDateDiffValue = (long)TS.TotalSeconds;
break;
case DateInterval.Minute:
lngDateDiffValue = (long)TS.TotalMinutes;
break;
case DateInterval.Hour:
lngDateDiffValue = (long)TS.TotalHours;
break;
case DateInterval.Day:
lngDateDiffValue = (long)TS.Days;
break;
case DateInterval.Week:
lngDateDiffValue = (long)(TS.Days / 7);
break;
case DateInterval.Month:
lngDateDiffValue = (long)(TS.Days / 30);
break;
case DateInterval.Quarter:
lngDateDiffValue = (long)((TS.Days / 30) / 3);
break;
case DateInterval.Year:
lngDateDiffValue = (long)(TS.Days / 365);
break;
}
return (lngDateDiffValue);
}//end of DateDiff
}//end of class