DateTimeHelper

本文参考文章:

http://geekswithblogs.net/mnf/articles/my-datetimehelper-class.aspx

https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/DateTimeHelper.cs,183

https://searchcode.com/codesearch/view/12636011/

解决方案下载地址:

链接:http://pan.baidu.com/s/1dEEs54t 密码:stmd

DateTimeHelepr 提供功能简介:

            获取年的开始时间
            获取年的结束时间
            获取当前年的结束时间
            判断年是否是闰年
            判断当前年是否是闰年
            获取某年的所有天数
            获取某年的所有天数
            获取指定年指定季度的开始时间
            获取指定年指定季度的结束时间
            获取月份对应的季度
            获取当前季度的开始时间
            获取当前季度的开始时间
            获取某年某月的开始日期
            根据指定日期所在的月份的第一天日期
            获取某年某月的结束日期
            根据指定日期所在的月份的最后一天日期
            获取当前日期所在的月份的最后一天日期
            获取给定日期的所在月的总天数
            获取指定年,指定月的上旬的开始日期
            获取指定年,指定月的中旬的开始日期
            获取指定年,指定月的下旬的开始日期
            获取指定年,指定月的上旬的结束日期
            获取指定年,指定月的下旬的结束日期
            获取指定年,指定月的中旬的结束日期
            获取指定日期所在旬的开始日期
            获取指定日期所在的旬
            获取某月第几天所在的旬
            获取当前周的开始日期
            获取当前周的结束日期
            获取指定日期的开始时刻
            获取指定日期的结束时刻
            获取当前日期的开始时刻
            获取当前日期的结束时刻
            获取指定日期是周几
            返回指定时间的标准时间格式syyyy-MM-dd
            返回指定时间的标准时间格式yyyy-MM-dd HH:mm:ss
            返回指定时间的标准全时间格式yyyy-MM-dd HH:mm:ss:fffffff
            获取多个日期的最大值(支持可变参数)
            获取多个日期的最小值(支持可变参数)
            获取两个日期的差值
            获取2个日期相差的总天数(double)
            获取2个日期相差的总分钟数(double)
            获取2个日期相差的总秒数(double)
            获取2个日期相差的总毫秒(double)
            解析日期扩展, 支持以下几种解析(通用的使用datetime.parse解析)

 

DateTimeHelper.cs

/*====================================================================
 *-- QQ:1072892917
 *====================================================================
 * 文件名称:DateTimeHelper.cs//
 * 项目名称:常用方法实用工具集
 * 创建时间:2016年11月15日14时13分
 * 创建人员:赵杰迪
 * 负 责 人:赵杰迪
 ===================================================================
*/

using System;
using System.Data.SqlTypes;
using System.Globalization;
using System.Linq;
using System.Runtime.Remoting.Messaging;

namespace DateTimeHelperTest
{
    public static class DateTimeHelper
    {
        #region 基础的私有方法
        /// <summary>
        /// 是否是有效的年
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private static bool IsValidYear(int year)
        {
            return year >= 0 && year <= 9999;
        }
        /// <summary>
        /// 是否是有效的季度
        /// </summary>
        /// <param name="quarter"></param>
        /// <returns></returns>
        private static bool IsValidQuarter(int quarter)
        {
            return quarter >= 1 && quarter <= 4;
        }
       /// <summary>
       /// 是否是有效的月
       /// </summary>
        /// <param name="month"></param>
       /// <returns></returns>
        private static bool IsValidMonth(int month)
        {
            return month >= 1 && month <= 12;
        }
        /// <summary>
        /// 是否是有效的旬
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        private static bool IsValidTen(int ten)
        {
            return ten >= 1 && ten <= 3;
        }
        #endregion
        #region 年级别的函数

        /// <summary>
        /// 获取年的开始时间
        /// </summary>
        /// <param name="year"></param>
        /// <exception cref="ArgumentOutOfRangeException">year无效值</exception>
        /// <returns>日期</returns>
        public static DateTime GetStartOfYear(int year)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
           return new DateTime(year,1,1);
        }
        /// <summary>
        /// 获取年的开始时间
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfYear(DateTime dt)
        {
            return new DateTime(dt.Year,1,1);
        }

        /// <summary>
        ///  获取年的结束时间
        /// </summary>
        /// <param name="year"></param>
        /// <exception cref="ArgumentOutOfRangeException">year无效</exception>
        /// <returns>日期</returns>
        public static DateTime GetEndOfYear(int year)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-4, and your transfer value is" + year);
            }
            return new DateTime(year, 12, DateTime.DaysInMonth(year, 12), 23, 59, 59, 999);
        }
        /// <summary>
        /// 获取年的结束时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetEndOfYear(DateTime dt)
        {
            return new DateTime(dt.Year, 12, DateTime.DaysInMonth(dt.Year, 12), 23, 59, 59, 999);
        }
      
        /// <summary>
        /// 获取当前年的开始时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetStartOfCurrentYear()
        {
            return GetStartOfYear(DateTime.Now);
        }
        /// <summary>
        /// 获取当前年的结束时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetEndOfCurrentYear()
        {
            return GetEndOfYear(DateTime.Now);
        }
        /// <summary>
        /// 判断年是否是闰年
        /// </summary>
        /// <param name="year"></param>
        /// <returns>布尔值</returns>
       public static bool IsLeapOfYear(int year)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
           return DateTime.IsLeapYear(year);
         }
       /// <summary>
       /// 判断年是否是闰年
       /// </summary>
       /// <param name="dt">日期</param>
       /// <returns>布尔值</returns>
       public static bool IsLeapOfYear(DateTime dt )
       {
           return DateTime.IsLeapYear(dt.Year);
       }
       /// <summary>
       /// 判断当前年是否是闰年
       /// </summary>
       /// <returns>布尔值</returns>
       public static bool IsLeapOfCurrentYear()
       {
           return IsLeapOfYear(DateTime.Now);
       }
       /// <summary>
       /// 获取某年的所有天数
       /// </summary>
       /// <param name="year"></param>
       /// <returns>天数</returns>
       public static int GetTotalDaysOfYear(int year)
       {
           if (!IsValidYear(year))
           {
               throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
           }
           return IsLeapOfYear(new DateTime(year,1,1)) ? 366 : 365;
       }
        /// <summary>
        /// 获取某年的所有天数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>天数</returns>
        public static int GetTotalDaysOfYear(DateTime dt)
        {
            return IsLeapOfYear(dt) ? 366 : 365;
        }
        /// <summary>
        /// 获取某年的所有天数
        /// </summary>
        /// <returns>天数</returns>
        public static int GetTotalDaysOfCurrentYear()
        {
            return GetTotalDaysOfYear(DateTime.Now);
        }
        #endregion
        #region 季度级别的函数
        /// <summary>
        /// 获取指定年指定季度的开始时间
        /// </summary>
        /// <param name="year">年有效值(0-9999)</param>
        /// <param name="quarter">季度(有效值1,2,3,4)</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfQuarter(int year, int quarter)
        {
            if (!IsValidYear(year) )
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
           if( !IsValidQuarter(quarter))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-4, and your transfer value is" + quarter);
            }
            switch (quarter)
            {
                case 1:
                    return new DateTime(year, 1, 1, 0, 0, 0, 0);
                case 2:
                    return new DateTime(year, 4, 1, 0, 0, 0, 0);
                case 3:
                    return new DateTime(year, 7, 1, 0, 0, 0, 0);
                case 4:
                    return new DateTime(year, 10, 1, 0, 0, 0, 0);
                default:
                     throw new ArgumentOutOfRangeException("Valid values are 1-4, and your transfer value is" + quarter);
            }
        }

        /// <summary>
        /// 获取指定年指定季度的结束时间
        /// </summary>
        /// <param name="year"></param>
        /// <param name="quarter"></param>
        /// <returns>可空的时间</returns>
        public static DateTime GetEndOfQuarter(int year, int quarter)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
            if (!IsValidQuarter(quarter))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-4, and your transfer value is" + quarter);
            }
            switch (quarter)
            {
                case 1:
                    return new DateTime(year, 3,
                        DateTime.DaysInMonth(year, 3), 23, 59, 59, 999);
                case 2:
                    return new DateTime(year, 6,
                        DateTime.DaysInMonth(year, 6), 23, 59, 59, 999);
                case 3:
                    return new DateTime(year, 9,
                        DateTime.DaysInMonth(year, 9), 23, 59, 59, 999);
                case 4: return new DateTime(year, 12,
                    DateTime.DaysInMonth(year, 12), 23, 59, 59, 999);
                default:
                    throw new ArgumentOutOfRangeException("Valid values are 1-4, and your transfer value is" + quarter);
            }
        }
      
        /// <summary>
        /// 获取月份对应的季度
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns>季度</returns>
        public static int GetQuarter(int month)
        {
            if (!IsValidMonth(month))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
            }
            return (month-1)/3 + 1;
        }
        public static int GetQuarter(DateTime dt)
        {
            return (dt.Month - 1) / 3 + 1;
        }
        /// <summary>
        /// 获取当前季度的开始时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetStartOfCurrentQuarter()
        {
            return GetStartOfQuarter(DateTime.Now.Year,  DateTime.Now.Month);
        }
        /// <summary>
        /// 获取当前季度的开始时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetEndOfCurrentQuarter()
        {
            return GetEndOfQuarter(DateTime.Now.Year,DateTime.Now.Month);
        }
        #endregion
        #region 月级别的函数

        /// <summary>
        /// 获取某年某月的开始日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <exception cref="ArgumentOutOfRangeException">输入无效</exception>
        /// <returns>日期</returns>
        public static DateTime GetStartOfMonth( int year,int month)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
            if (!IsValidMonth(month))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
            }
            return new DateTime(year, month, 1, 0, 0, 0, 0);
        }
       /// <summary>
        /// 根据指定日期所在的月份的第一天日期
       /// </summary>
       /// <param name="dt">指定的日期</param>
       /// <returns>日期</returns>
        public static DateTime GetStartOfMonth(DateTime dt)
        {
            return GetStartOfMonth(dt.Year, dt.Month);
        }

        /// <summary>
        /// 获取某年某月的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <exception cref="ArgumentOutOfRangeException">输入无效</exception>
        /// <returns>日期</returns>
        public static DateTime GetEndOfMonth( int year,int month)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
            if (!IsValidMonth(month))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
            }
            return new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59, 999);
        }
        /// <summary>
        /// 根据指定日期所在的月份的最后一天日期
        /// </summary>
        /// <param name="dt">指定的日期</param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfMonth(DateTime dt)
        {
            return GetEndOfMonth(dt.Year, dt.Month);
        }
        /// <summary>
        /// 获取当前日期所在的月份的第一天日期
        /// </summary>
        /// <returns>日期</returns>
        public static DateTime GetStartOfCurrentMonth()
        {
            return GetStartOfMonth(DateTime.Now);
        }
        /// <summary>
        /// 获取当前日期所在的月份的最后一天日期
        /// </summary>
        /// <returns>日期</returns>
        public static DateTime GetEndOfCurrentMonth()
        {
            return GetEndOfMonth(DateTime.Now);
        }
        /// <summary>
        /// 获取给定年月的月份总天数
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>天数</returns>
         public static int GetTotalDayOfMonth(int year, int month) 
         {
             if (!IsValidYear(year))
             {
                 throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
             }
             if (!IsValidMonth(month))
             {
                 throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
             }
          return DateTime.DaysInMonth(year, month);
       }
       /// <summary>
       /// 获取给定日期的所在月的总天数
       /// </summary>
       /// <param name="dt"></param>
       /// <returns>天数</returns>
         public static int GetTotalDayOfMonth(DateTime dt)
         {
             return DateTime.DaysInMonth(dt.Year, dt.Month);
         }
        #endregion
        #region 旬级别的函数
        /// <summary>
        /// 获取指定年,指定月的上旬的开始日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenFirst(DateTime dt)
        {
            return GetStartOfTen(dt.Year, dt.Month, 1);
        }
        /// <summary>
        /// 获取指定年,指定月的中旬的结束日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenSecond(DateTime dt)
        {
            return GetStartOfTen(dt.Year, dt.Month, 2);
        }
        /// <summary>
        /// 获取指定年,指定月的下旬的开始日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenThird(DateTime dt)
        {
            return GetStartOfTen(dt.Year, dt.Month, 3);
        }
        /// <summary>
        /// 获取指定年,指定月的上旬的结束日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenFirst(DateTime dt)
        {
            return GetEndOfTen(dt.Year, dt.Month, 1);
        }
        /// <summary>
        /// 获取指定年,指定月的中旬的结束日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenSecond(DateTime dt)
        {
            return GetEndOfTen(dt.Year, dt.Month, 2);
        }
        /// <summary>
        /// 获取指定年,指定月的下旬的结束日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenThird(DateTime dt)
        {
            return GetEndOfTen(dt.Year, dt.Month, 3);
        }
        /// <summary>
        /// 获取指定年,指定月的上旬的开始日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenFirst(int year, int month)
        {
            return GetStartOfTen(year, month, 1);
        }
        /// <summary>
        /// 获取指定年,指定月的中旬的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenSecond(int year, int month)
        {
            return GetStartOfTen(year, month, 2);
        }
        /// <summary>
        /// 获取指定年,指定月的下旬的开始日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTenThird(int year, int month)
        {
            return GetStartOfTen(year, month, 3);
        }
        /// <summary>
        /// 获取指定年,指定月的上旬的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenFirst(int year, int month)
        {
            return GetEndOfTen(year, month, 1);
        }
        /// <summary>
        /// 获取指定年,指定月的中旬的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenSecond(int year, int month)
        {
            return GetEndOfTen(year, month, 2);
        }
        /// <summary>
        /// 获取指定年,指定月的下旬的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTenThird(int year, int month)
        {
            return GetEndOfTen(year, month, 3);
        }
        public static DateTime GetEndOfCurrentTen()
        {
            DateTime dt = DateTime.Now.Date;
            int ten = GetTen(dt);
            return GetEndOfTen(dt.Year, dt.Month, ten);
        }
        public static DateTime GetStartOfCurrentTen()
        {
            DateTime dt = DateTime.Now.Date;
            int ten = GetTen(dt);
            return GetStartOfTen(dt.Year, dt.Month, ten);
        }
        /// <summary>
        /// 获取指定年,指定月, 指定旬的开始日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="ten"></param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTen(int year, int month, int ten)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
            if (!IsValidMonth(month))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
            }
            if (!IsValidTen(ten))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-3, and your transfer value is" + ten);
            }
            if (ten == 1)
            {
                return new DateTime(year,month,1,0,0,0,0);
            }
            else if (ten == 2)
            {
                return new DateTime(year, month, 11, 0, 0, 0, 0);
            }
            else
            {
                return new DateTime(year, month, 21, 0, 0, 0, 0);
            }
        }
        /// <summary>
        /// 获取指定年,指定月, 指定旬的结束日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="ten"></param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTen(int year,int month,int ten)
        {
            if (!IsValidYear(year))
            {
                throw new ArgumentOutOfRangeException("Valid values are 0-9999, and your transfer value is" + year);
            }
            if (!IsValidMonth(month))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-12, and your transfer value is" + month);
            }
            if (!IsValidTen(ten))
            {
                throw new ArgumentOutOfRangeException("Valid values are 1-3, and your transfer value is" + ten);
            }
            if (ten == 1)
            {
                return new DateTime(year,month,10,23,59,59,999);
            }
            else if (ten == 2)
            {
                return new DateTime(year, month, 20, 23, 59, 59, 999);
            }
            else
            {
                return new DateTime(year, month,DateTime.DaysInMonth(year,month), 23, 59, 59, 999);
            }
        }
        /// <summary>
        /// 获取指定日期所在旬的开始日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfTen(DateTime dt)
        {
            return GetStartOfTen(dt.Year, dt.Month, GetTen(dt));
        }
        /// <summary>
        /// 获取指定日期所在旬的结束日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfTen(DateTime dt)
        {
            return GetEndOfTen(dt.Year, dt.Month, GetTen(dt));
        }
        /// <summary>
        /// 获取指定日期所在的旬
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns></returns>
        public static int GetTen(DateTime dt)
        {
          return GetTen(dt.Day);
        }
        /// <summary>
        /// 获取某月第几天所在的旬
        /// </summary>
        /// <param name="day">某个日期所在月的第几天</param>
        /// <returns></returns>
        public static int GetTen(int day)
        {
            int ten = (DateTime.Now.Day - 1) / 10 + 1;
            return ten > 3 ? 3 : ten;
        }
        #endregion
        #region 周级别的函数
        public static DateTime GetStartOfWeek(DateTime dt)
        {
            int daysToSubtract = (int)dt.DayOfWeek;
            DateTime resultDateTime = DateTime.Now.Subtract(System.TimeSpan.FromDays(daysToSubtract));
            return resultDateTime.Date;
        }
        public static DateTime GetEndOfWeek(DateTime dt)
        {
            DateTime resultDateTime = GetStartOfWeek(dt).AddDays(6);
            return new DateTime(resultDateTime.Year, resultDateTime.Month, resultDateTime.Day, 23, 59, 59, 999);
        }
        /// <summary>
        /// 获取当前周的开始日期
        /// </summary>
        /// <returns>日期</returns>
        public static DateTime GetStartOfCurrentWeek()
        {
            return GetStartOfWeek(DateTime.Now);
        }
        /// <summary>
        /// 获取当前周的结束日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetEndOfCurrentWeek()
        {
            return GetEndOfWeek(DateTime.Now);
        }
        #endregion
        #region 日级别的函数

        /// <summary>
        /// 获取指定日期的开始时刻
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetStartOfDay(DateTime dt)
        {
            return dt.Date;
        }
        /// <summary>
        /// 获取指定日期的结束时刻
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>日期</returns>
        public static DateTime GetEndOfDay(DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
        }
        /// <summary>
        /// 获取当前日期的开始时刻
        /// </summary>
        /// <returns>日期</returns>
        public static DateTime GetStartOfCurrentDay()
        {
            return GetStartOfDay(DateTime.Now);
        }
        /// <summary>
        /// 获取当前日期的结束时刻
        /// </summary>
        /// <returns>日期</returns>
        public static DateTime GetEndOfCurrentDay()
        {
            return GetEndOfDay(DateTime.Now);
        }
        /// <summary>
        /// 获取指定日期是周几
        /// </summary>
        /// <returns>日期</returns>
        public static int GetWeekdayOfCurrentDay()
        {
            return GetWeekdayOfDay(DateTime.Now);
        }
        /// <summary>
        /// 获取指定日期是周几
        /// </summary>
        /// <returns>周几</returns>
        public static int GetWeekdayOfDay(DateTime dt)
        {
            int week = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
            return week;
        }
        #endregion
        #region 格式控制的
        /// <summary>
        /// 返回指定时间的标准日期格式
        /// </summary>
        /// <returns>yyyy-MM-dd</returns>
        public static string GetDateString(DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 返回指定时间的标准时间格式string
        /// </summary>
        /// <returns>HH:mm:ss</returns>
        public static string GetTimeString(DateTime dt)
        {
            return dt.ToString("HH:mm:ss");
        }
        /// <summary>
        /// 返回指定时间的标准时间格式string
        /// </summary>
        /// <returns>yyyy-MM-dd HH:mm:ss</returns>
        public static string GetDateTimeString(DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        ///  返回指定时间的标准全时间格式string
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>yyyy-MM-dd HH:mm:ss:fffffff</returns>
        public static string GetDateTimeFullString(DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
        }
        /// <summary>
        /// 返回指定时间的标准日期格式
        /// </summary>
        /// <returns>yyyy-MM-dd</returns>
        public static string GetDateStringOfCurrentDay()
        {
            return GetDateString(DateTime.Now);
        }
        /// <summary>
        /// 返回指定时间的标准时间格式string
        /// </summary>
        /// <returns>HH:mm:ss</returns>
        public static string GetTimeStringOfCurrentDay()
        {
            return GetTimeString(DateTime.Now);
        }
        /// <summary>
        /// 返回指定时间的标准时间格式string
        /// </summary>
        /// <returns>yyyy-MM-dd HH:mm:ss</returns>
        public static string GetDateTimeStringOfCurrentDay()
        {
            return GetDateTimeString(DateTime.Now);
        }
        /// <summary>
        ///  返回指定时间的标准全时间格式string
        /// </summary>
        /// <returns>yyyy-MM-dd HH:mm:ss:fffffff</returns>
        public static string GetDateTimeFullStringOfCurrentDay()
        {
            return GetDateTimeFullString(DateTime.Now);
        }
        #endregion
        #region 日期比较的
        /// <summary>
        /// 获取多个日期的最大值(支持可变参数)
        /// </summary>
        /// <returns></returns>
        public static DateTime GetMax( params DateTime[] dts)
        {
            return dts.Max();
        }
        /// <summary>
        ///  获取多个日期的最小值(支持可变参数)
        /// </summary>
        /// <returns></returns>
        public static DateTime GetMin( params DateTime[] dts)
        {
            return dts.Min();
        }
        #endregion
        #region 日期差值
        /// <summary>
        /// 获取两个日期的差值
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>TimeSpan</returns>
        public static TimeSpan GetDifference(DateTime dt1, DateTime dt2)
        {
            return dt1 - dt2;
        }
        /// <summary>
        /// 获取2个日期相差的总天数(double)
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>总天数(double)</returns>
        public static double GetDifferenceOfDay(DateTime dt1, DateTime dt2)
        {
            return GetDifference(dt1,dt2).TotalDays;
        }
        /// <summary>
        /// 获取2个日期相差的总天数(double)
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>总天数(double)</returns>
        public static double GetDifferenceOfHours(DateTime dt1, DateTime dt2)
        {
            return GetDifference(dt1, dt2).TotalHours;
        }
        /// <summary>
        /// 获取2个日期相差的总分钟数(double)
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>总分钟数(double)</returns>
        public static double GetDifferenceOfMinutes(DateTime dt1, DateTime dt2)
        {
            return GetDifference(dt1, dt2).TotalMinutes;
        }
        /// <summary>
        /// 获取2个日期相差的总秒数(double)
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>总秒数(double)</returns>
        public static double GetDifferenceOfSeconds(DateTime dt1, DateTime dt2)
        {
            return GetDifference(dt1, dt2).TotalSeconds;
        }
        /// <summary>
        /// 获取2个日期相差的总毫秒(double)
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns>总毫秒数(double)</returns>
        public static double GetDifferenceOfMilliseconds(DateTime dt1, DateTime dt2)
        {
            return GetDifference(dt1, dt2).TotalMilliseconds;
        }
        #endregion
        #region 解析日期扩展

        /// <summary>
        /// 解析日期扩展, 支持以下几种解析(通用的使用datetime.parse解析)
        /// 20160504,2016年05月04日,2016年05月04,2016年5月04日, 2016年5月4, yyyy年05月5日, yyyy年05月4日
        /// </summary>
        /// <param name="yyyymmdd"></param>
        /// <exception cref="ArgumentException">无法解析</exception>
        /// <returns></returns>
        public static DateTime ParseExt( string yyyymmdd)
        {
            string[] format = { "yyyyMMdd", "yyyy年MM月dd日", "yyyy年MM月dd", "yyyy年M月dd日", "yyyy年M月d", "yyyy年MM月d日", "yyyy年MM月d日" };
            DateTime dt;
            if (DateTime.TryParseExact(yyyymmdd, format,CultureInfo.InvariantCulture,DateTimeStyles.None,out dt))
            {
                return dt;
            }
            throw  new ArgumentException("字符串错误");
        }
       
        #endregion
    }     
}

DateTimeHelper的测试

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data.SqlTypes;
  4 using System.Linq;
  5 using System.Runtime.InteropServices;
  6 using System.Security.Cryptography;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 
 10 namespace DateTimeHelperTest
 11 {
 12     class Program
 13     {
 14         static void Main(string[] args)
 15         {
 16             //这里先打印下我的时间吧
 17             if (true)
 18             {
 19                 Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 11:38:56:7364468
 20             }
 21 
 22             //获取年的开始时间和结束时间
 23             if (true)
 24             {
 25                 DateTime start = DateTimeHelper.GetStartOfYear(2015);
 26                 DateTime end = DateTimeHelper.GetEndOfYear(2015);
 27                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2015-01-01 00:00:00:0000000
 28                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2015-12-31 23:59:59:9990000
 29             }
 30             //获取当前年的开始日期和结束日期
 31             if (true)
 32             {
 33                 DateTime start = DateTimeHelper.GetStartOfCurrentYear();
 34                 DateTime end = DateTimeHelper.GetEndOfCurrentYear();
 35                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-01-01 00:00:00:0000000
 36                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-12-31 23:59:59:9990000
 37             }
 38             //是否是闰年和年的天数
 39             if (true)
 40             {
 41                 bool b2015 = DateTimeHelper.IsLeapOfYear(2015);
 42                 bool b2016 = DateTimeHelper.IsLeapOfCurrentYear();
 43                 Console.WriteLine("2015闰年?{0}", b2015);//2015闰年?False
 44                 Console.WriteLine("2016闰年?{0}", b2016);//2016闰年?True
 45                 int days2015 = DateTimeHelper.GetTotalDaysOfYear(new DateTime(2015, 1, 3));
 46                 int days2016 = DateTimeHelper.GetTotalDaysOfYear(2016);
 47                 Console.WriteLine("2015多少天:{0}", days2015);//2015多少天:365
 48                 Console.WriteLine("2016多少天:{0}", days2016);//2016多少天:366
 49             }
 50             //季度相关的
 51             if (true)
 52             {
 53                 DateTime start = DateTimeHelper.GetStartOfQuarter(2016, 1);
 54                 DateTime end = DateTimeHelper.GetEndOfQuarter(2016, 1);
 55                 int quarter = DateTimeHelper.GetQuarter(12);
 56                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));// 2016-01-01 00:00:00:0000000
 57                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-03-31 23:59:59:9990000
 58                 Console.WriteLine(quarter);//4
 59             }
 60             //月相关的
 61             if (true)
 62             {
 63                 DateTime start = DateTimeHelper.GetStartOfCurrentMonth();
 64                 DateTime end = DateTimeHelper.GetEndOfMonth(DateTime.Now);
 65                 int days = DateTimeHelper.GetTotalDayOfMonth(DateTime.Now);
 66                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-01 00:00:00:0000000
 67                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-30 23:59:59:9990000
 68                 Console.WriteLine(days);//30
 69             }
 70             //旬相关的
 71             if (true)
 72             {
 73                 DateTime start = DateTimeHelper.GetStartOfCurrentTen();
 74                 DateTime end = DateTimeHelper.GetEndOfTenFirst(2016, 12);
 75                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-11 00:00:00:0000000
 76                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-12-10 23:59:59:9990000
 77             }
 78             //周相关
 79             if (true)
 80             {
 81                 DateTime start = DateTimeHelper.GetStartOfWeek(DateTime.Now);
 82                 DateTime end = DateTimeHelper.GetEndOfWeek(DateTime.Now);
 83                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-13 00:00:00:0000000
 84                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-19 23:59:59:9990000
 85             }
 86             //日相关
 87             if (true)
 88             {
 89                 DateTime start = DateTimeHelper.GetStartOfCurrentDay();
 90                 DateTime end = DateTimeHelper.GetEndOfCurrentDay();
 91                 Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 00:00:00:0000000
 92                 Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 23:59:59:9990000
 93             }
 94             //格式控制
 95             if (true)
 96             {
 97                 DateTime dt = DateTime.Now;
 98                 string s1 = DateTimeHelper.GetDateString(dt);
 99                 string s2 = DateTimeHelper.GetDateTimeString(dt);
100                 string s3 = DateTimeHelper.GetDateTimeFullString(dt);
101                 Console.WriteLine(s1);//2016-11-15
102                 Console.WriteLine(s2);//2016-11-15 13:13:12
103                 Console.WriteLine(s3);//2016-11-15 13:13:12:9846102
104             }
105             //日期比较的
106             if (true)
107             {
108                 DateTime dt1 = DateTime.Now;
109                 DateTime dt2 = new DateTime(2016, 2, 3);
110                 DateTime dt3 = DateTime.Now.AddDays(-1).AddMinutes(2).AddSeconds(3).AddMilliseconds(200);
111                 DateTime dt4 = DateTime.Parse("2016-07-12");
112                 List<DateTime> list = new List<DateTime>() { dt1, dt2, dt3, dt4 };
113                 DateTime max = DateTimeHelper.GetMax(list.ToArray());
114                 DateTime min = DateTimeHelper.GetMin(list.ToArray());
115                 Console.WriteLine("dt1={0}\ndt2={1}\ndt3={2}\ndt4={3}\n",
116                     DateTimeHelper.GetDateTimeString(dt1),
117                     DateTimeHelper.GetDateTimeString(dt2),
118                 DateTimeHelper.GetDateTimeString(dt3),
119                 DateTimeHelper.GetDateTimeString(dt4));
120                 Console.WriteLine("max=" + DateTimeHelper.GetDateTimeString(max));
121                 Console.WriteLine("min=" + DateTimeHelper.GetDateTimeString(min));
122                 //dt1=2016-11-15 13:26:14
123                 //dt2=2016-02-03 00:00:00
124                 //dt3=2016-11-14 13:28:18
125                 //dt4=2016-07-12 00:00:00
126                 //
127                 //max=2016-11-15 13:26:14
128                 //min=2016-02-03 00:00:00
129 
130                 max = DateTimeHelper.GetMax(new DateTime(2016, 1, 2), new DateTime(2016, 1, 3));
131                 Console.WriteLine("max=" + DateTimeHelper.GetDateTimeString(max));//max=2016-01-03 00:00:00
132             }
133             //获取日期的差值  
134             if (true)
135             {
136                 DateTime dt1 = new DateTime(2016, 12, 3, 20, 18, 19);
137                 DateTime dt2 = new DateTime(2016, 12, 2, 8, 10, 10);
138                 TimeSpan timeSpan = DateTimeHelper.GetDifference(dt1, dt2);
139                 Console.WriteLine("dt1-dt2={0}天{1}小时{2}分钟{3}秒{4}毫秒", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
140                 //dt1-dt2=1天12小时8分钟9秒0毫秒
141                 double days = DateTimeHelper.GetDifferenceOfDay(dt1, dt2);
142                 double hours = DateTimeHelper.GetDifferenceOfHours(dt1, dt2);
143                 double minutes = DateTimeHelper.GetDifferenceOfMinutes(dt1, dt2);
144                 double seconds = DateTimeHelper.GetDifferenceOfSeconds(dt1, dt2);
145 
146                 Console.WriteLine("dt1-dt2={0}days", days);
147                 Console.WriteLine("dt1-dt2={0}hours", hours);
148                 Console.WriteLine("dt1-dt2={0}minutes", minutes);
149                 Console.WriteLine("dt1-dt2={0}seconds", seconds);
150                 //dt1-dt2=1.50565972222222days
151                 //dt1-dt2=36.1358333333333hours
152                 //dt1-dt2=2168.15minutes
153                 //dt1-dt2=130089seconds
154             }
155             //解析日期扩展
156             if (true)
157             {
158                 DateTime dt1 = DateTime.Parse("2016-10-12");
159                 DateTime dt2= DateTimeHelper.ParseExt("2016年10月12日");
160                 Console.WriteLine(dt1.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-10-12 00:00:00:0000000
161                 Console.WriteLine(dt2.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-10-12 00:00:00:0000000
162             }
163             Console.ReadKey();
164         }
165     }
166 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值