首页 > 开发 > 综合 > 正文

DateDiff 方法在C#中的实现

2024-07-21 02:29:06
字体:
来源:转载
供稿:网友

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


samples:
public class datediffcs
{
    public static void main (string[] cmdlineargs)
    {
        system.datetime date1 = system.datetime.now;
        system.datetime date2 = system.datetime.now.addyears(2);

        system.console.out.writeline("days : " + datetimemanger.datediff
        (datetimemanger.dateinterval.day,date1,date2).tostring());
        system.console.out.writeline("hours : " + datetimemanger.datediff
        (datetimemanger.dateinterval.hour,date1,date2).tostring());
        system.console.out.writeline("minutes : " + datetimemanger.datediff
        (datetimemanger.dateinterval.minute,date1,date2).tostring());
        system.console.out.writeline("month : " + datetimemanger.datediff
        (datetimemanger.dateinterval.month,date1,date2).tostring());
        system.console.out.writeline("quarter : " + datetimemanger.datediff
        (datetimemanger.dateinterval.quarter,date1,date2).tostring());
        system.console.out.writeline("seconds : " + datetimemanger.datediff
        (datetimemanger.dateinterval.second,date1,date2).tostring());
        system.console.out.writeline("week : " + datetimemanger.datediff
        (datetimemanger.dateinterval.week,date1,date2).tostring());
        system.console.out.writeline("year : " + datetimemanger.datediff
        (datetimemanger.dateinterval.year,date1,date2).tostring());

        system.console.in.readline();

    }

}//end of the class
from:
hp.mahesh.utilitymanager.libraries.datetimeutility

注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表