1. 程式人生 > >C#時間幫助類

C#時間幫助類

using System;

using System.Collections.Generic;
using System.Text;


namespace TestProject
{
    #region 時間日期幫助類 by 李大亮 2011/11/16
    /// <summary>
    /// 時間日期幫助類 by 李大亮 2011/11/16
    /// </summary>
    #endregion
    public class DateTimeHelper
    {
        #region 獲取該時間所在周的週一和週末1(注:這個週一個週末時間不是準確的0:00,可以是週一中的任意一個時間點,具體由傳入的時間決定)
        /// <summary>
        /// 獲取本週開始和結束
        /// </summary>
        /// <param name="dataTime">時間</param>
        /// <returns></returns>
        public static DateTime[] getMondayAndSundayOfTheTime_1(DateTime dataTime)
        {
            //週一時間
            DateTime first = dataTime;
            switch (dataTime.DayOfWeek)
            {
                case System.DayOfWeek.Monday:
                    first = dataTime;
                    break;
                case System.DayOfWeek.Tuesday:
                    first = dataTime.AddDays(-1);
                    break;
                case System.DayOfWeek.Wednesday:
                    first = dataTime.AddDays(-2);
                    break;
                case System.DayOfWeek.Thursday:
                    first = dataTime.AddDays(-3);
                    break;
                case System.DayOfWeek.Friday:
                    first = dataTime.AddDays(-4);
                    break;
                case System.DayOfWeek.Saturday:
                    first = dataTime.AddDays(-5);
                    break;
                case System.DayOfWeek.Sunday:
                    first = dataTime.AddDays(-6);
                    break;
            }
            return new DateTime[] { first, first.AddDays(6) };
        }
        #endregion


        #region 獲取該時間所在周的週一和週末2
        /// <summary>
        /// 獲取本週週一和週日時間
        /// </summary>
        /// <param name="dt">傳入時間</param>
        /// <returns></returns>
        public static DateTime[] getMondayAndSundayOfTheTime_2(DateTime dt)
        {
            //獲取改時間是本週的第幾天
            int thedayoftheweek = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
            //本週週一
            DateTime startWeek = dt.AddDays(1 - (thedayoftheweek == 0 ? 7 : thedayoftheweek));
            return new DateTime[] { startWeek, startWeek.AddDays(6) };
        }
        #endregion


        #region 獲取時間所在當天的開始和結束時間(注:在這裡當天的結束時間是在明天的開始的基礎上加上-1的Tick值)
        /// <summary>
        /// 獲取時間所在當天的開始和結束時間
        /// </summary>
        /// <param name="dt">傳入時間</param>
        /// <returns></returns>
        public static DateTime[] getBeginAndEndTimeOfTheDay(DateTime dt)
        {
            DateTime beginTime = DateTime.Parse(dt.ToString("yyyy-MM-dd"));
            DateTime endTime = DateTime.Parse(dt.ToString("yyyy-MM-dd")).AddDays(1).AddTicks(-1);
            return new DateTime[] { beginTime, endTime };
        }
        #endregion


        #region 獲取時間所在月的開始和結束時間
        /// <summary>
        /// 獲取時間所在月的開始和結束時間
        /// </summary>
        /// <param name="dt">傳入時間</param>
        /// <returns></returns>
        public static DateTime[] getBeginAndEndTimeOfTheMonth(DateTime dt)
        {
            DateTime startMonth = dt.AddDays(1 - dt.Day);  //本月月初
            DateTime endMonth = startMonth.AddMonths(1).AddDays(-1);  //本月月末
            return new DateTime[] { startMonth, endMonth };
        }
        #endregion


        #region 獲取時間所在季度的開始和結束時間
        /// <summary>
        /// 獲取時間所在季度的開始和結束時間
        /// </summary>
        /// <param name="dt">傳入時間</param>
        /// <returns></returns>
        public static DateTime[] getBeginAndEndTimeOfQuarter(DateTime dt)
        {
            //本季度初
            DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);
            //本季度末
            DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);
            return new DateTime[] { startQuarter, endQuarter };
        }
        #endregion


        #region 獲取本年年初和年末
        /// <summary>
        /// 獲取本年年初和年末
        /// </summary>
        /// <param name="dt">傳入時間</param>
        /// <returns></returns>
        public static DateTime[] getBeginAndEndTimeOfTheYear(DateTime dt)
        {
            DateTime startYear = new DateTime(dt.Year, 1, 1);  //本年年初
            DateTime endYear = new DateTime(dt.Year, 12, 31);  //本年年末
            return new DateTime[] { startYear, endYear };
        }
        #endregion


        #region 獲取某一年某一月有多少天
        /// <summary>
        /// 獲取某一年某一月有多少天
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns></returns>
        public static int getMonthDays(int year, int month)
        {
            return new DateTime(year, month, 1).AddMonths(1).AddDays(-1).Day;
        }
        #endregion


        #region 獲取兩個時間段的相隔天數
        /// <summary>
        /// 獲取兩個時間段的相隔天數
        /// </summary>
        /// <param name="begin">開始時間(小)</param>
        /// <param name="end">結束時間(大)</param>
        /// <returns></returns>
        public static int getIntervalDaysOfTwoDateTime(DateTime begin, DateTime end)
        {
            //如果開始時間比結束時間大,交換時間
            if (begin.CompareTo(end) > 0)
            {
                DateTime temp = begin;
                begin = end;
                end = temp;
            }
            int days = 0;
            //計算年的相差天數
            for (int i = begin.Year; i < end.Year; i++)
            {
                days += (DateTime.IsLeapYear(i) == true ? 366 : 365);
            }
            //計算月的相差天數
            days += end.DayOfYear - begin.DayOfYear;


            return days;
        }
        #endregion
    }
}