The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

C# 3.0 初體驗:Extension Method

昨天寫了一篇如何取得兩個 DateTime 之間的方法,今天將昨天的東西寫成一組 Extension Method,感覺十分的好用,分享出來給大家參考!

我基本上是參照在 SQL Server 中的 DATEDIFF 函數的用法,讓熟悉 SQL 的人很容易的就能夠上手使用。

[code:c#]

using System;

namespace ExtensionMethods
{
    static class DateTimeExtensionMethod
    {
        public static double DateDiff(this DateTime startdate, string datepart, DateTime enddate)
        {
            // 參考 SQL Server 2005 線上叢書:http://technet.microsoft.com/zh-tw/library/ms189794.aspx

            double result = 0;

            TimeSpan tsDiff = new TimeSpan(enddate.Ticks - startdate.Ticks);

            switch (datepart.ToLower())
            {
                case "year":
                case "yyyy":
                case "yy":
                    result = enddate.Year - startdate.Year;
                    break;

                case "quarter":
                case "qq":
                case "q":
                    // 每一季的平均天數
                    const double AvgQuarterDays = 365 / 4;
                    result = Math.Floor(tsDiff.TotalDays / AvgQuarterDays);
                    break;

                case "month":
                case "mm":
                case "m":
                    // 每一個月的平均天數
                    const double AvgMonthDays = 365 / 12;
                    result = Math.Floor(tsDiff.TotalDays / AvgMonthDays);
                    break;

                case "day":
                case "dd":
                case "d":
                    result = tsDiff.TotalDays;
                    break;

                case "week":
                case "wk":
                case "ww":
                    result = Math.Floor(tsDiff.TotalDays / 7);
                    break;

                case "hour":
                case "hh":
                    result = tsDiff.TotalHours;
                    break;

                case "minute":
                case "mi":
                case "n":
                    result = tsDiff.TotalMinutes;
                    break;

                case "second":
                case "ss":
                case "s":
                    result = tsDiff.TotalSeconds;
                    break;

                case "millisecond":
                case "ms":
                    result = tsDiff.TotalMilliseconds;
                    break;

                default:
                    throw new ArgumentException("You input a invalid datepart parameter.");
            }

            return result;
        }
    }
}
[/code]

至於使用上有 2 點要注意:

  1. 必須在 .NET Framework 3.5 的環境下才能執行
  2. 必須先引用 ExtensionMethods 命名空間
範例如下:

[code:c#]

using System;

using ExtensionMethods;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            DateTime date1 = DateTime.Now.AddHours(0);

            DateTime date2 = DateTime.Now.AddHours(24).AddMonths(3);

            double DiffDays = date1.DateDiff("day", date2);

            Console.Write(DiffDays);

            Console.ReadKey();
        }
    }
}
[/code]