本文参考文章:
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 >= && year <= ;
}
/// <summary>
/// 是否是有效的季度
/// </summary>
/// <param name="quarter"></param>
/// <returns></returns>
private static bool IsValidQuarter(int quarter)
{
return quarter >= && quarter <= ;
}
/// <summary>
/// 是否是有效的月
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
private static bool IsValidMonth(int month)
{
return month >= && month <= ;
}
/// <summary>
/// 是否是有效的旬
/// </summary>
/// <param name="ten"></param>
/// <returns></returns>
private static bool IsValidTen(int ten)
{
return ten >= && ten <= ;
}
#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,,);
}
/// <summary>
/// 获取年的开始时间
/// </summary>
/// <param name="dt">日期</param>
/// <returns>日期</returns>
public static DateTime GetStartOfYear(DateTime dt)
{
return new DateTime(dt.Year,,);
} /// <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, , DateTime.DaysInMonth(year, ), , , , );
}
/// <summary>
/// 获取年的结束时间
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime GetEndOfYear(DateTime dt)
{
return new DateTime(dt.Year, , DateTime.DaysInMonth(dt.Year, ), , , , );
} /// <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,,)) ? : ;
}
/// <summary>
/// 获取某年的所有天数
/// </summary>
/// <param name="dt">年</param>
/// <returns>天数</returns>
public static int GetTotalDaysOfYear(DateTime dt)
{
return IsLeapOfYear(dt) ? : ;
}
/// <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 :
return new DateTime(year, , , , , , );
case :
return new DateTime(year, , , , , , );
case :
return new DateTime(year, , , , , , );
case :
return new DateTime(year, , , , , , );
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 :
return new DateTime(year, ,
DateTime.DaysInMonth(year, ), , , , );
case :
return new DateTime(year, ,
DateTime.DaysInMonth(year, ), , , , );
case :
return new DateTime(year, ,
DateTime.DaysInMonth(year, ), , , , );
case : return new DateTime(year, ,
DateTime.DaysInMonth(year, ), , , , );
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-)/ + ;
}
public static int GetQuarter(DateTime dt)
{
return (dt.Month - ) / + ;
}
/// <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, , , , , );
}
/// <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), , , , );
}
/// <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, );
}
/// <summary>
/// 获取指定年,指定月的中旬的结束日期
/// </summary>
/// <param name="dt">日期</param>
/// <returns>日期</returns>
public static DateTime GetStartOfTenSecond(DateTime dt)
{
return GetStartOfTen(dt.Year, dt.Month, );
}
/// <summary>
/// 获取指定年,指定月的下旬的开始日期
/// </summary>
/// <param name="dt">日期</param>
/// <returns>日期</returns>
public static DateTime GetStartOfTenThird(DateTime dt)
{
return GetStartOfTen(dt.Year, dt.Month, );
}
/// <summary>
/// 获取指定年,指定月的上旬的结束日期
/// </summary>
/// <param name="dt">年</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenFirst(DateTime dt)
{
return GetEndOfTen(dt.Year, dt.Month, );
}
/// <summary>
/// 获取指定年,指定月的中旬的结束日期
/// </summary>
/// <param name="dt">年</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenSecond(DateTime dt)
{
return GetEndOfTen(dt.Year, dt.Month, );
}
/// <summary>
/// 获取指定年,指定月的下旬的结束日期
/// </summary>
/// <param name="dt">年</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenThird(DateTime dt)
{
return GetEndOfTen(dt.Year, dt.Month, );
}
/// <summary>
/// 获取指定年,指定月的上旬的开始日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetStartOfTenFirst(int year, int month)
{
return GetStartOfTen(year, month, );
}
/// <summary>
/// 获取指定年,指定月的中旬的结束日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetStartOfTenSecond(int year, int month)
{
return GetStartOfTen(year, month, );
}
/// <summary>
/// 获取指定年,指定月的下旬的开始日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetStartOfTenThird(int year, int month)
{
return GetStartOfTen(year, month, );
}
/// <summary>
/// 获取指定年,指定月的上旬的结束日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenFirst(int year, int month)
{
return GetEndOfTen(year, month, );
}
/// <summary>
/// 获取指定年,指定月的中旬的结束日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenSecond(int year, int month)
{
return GetEndOfTen(year, month, );
}
/// <summary>
/// 获取指定年,指定月的下旬的结束日期
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>日期</returns>
public static DateTime GetEndOfTenThird(int year, int month)
{
return GetEndOfTen(year, month, );
}
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 == )
{
return new DateTime(year,month,,,,,);
}
else if (ten == )
{
return new DateTime(year, month, , , , , );
}
else
{
return new DateTime(year, month, , , , , );
}
}
/// <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 == )
{
return new DateTime(year,month,,,,,);
}
else if (ten == )
{
return new DateTime(year, month, , , , , );
}
else
{
return new DateTime(year, month,DateTime.DaysInMonth(year,month), , , , );
}
}
/// <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 - ) / + ;
return ten > ? : 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();
return new DateTime(resultDateTime.Year, resultDateTime.Month, resultDateTime.Day, , , , );
}
/// <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, , , , );
}
/// <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的测试
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace DateTimeHelperTest
{
class Program
{
static void Main(string[] args)
{
//这里先打印下我的时间吧
if (true)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 11:38:56:7364468
} //获取年的开始时间和结束时间
if (true)
{
DateTime start = DateTimeHelper.GetStartOfYear();
DateTime end = DateTimeHelper.GetEndOfYear();
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2015-01-01 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2015-12-31 23:59:59:9990000
}
//获取当前年的开始日期和结束日期
if (true)
{
DateTime start = DateTimeHelper.GetStartOfCurrentYear();
DateTime end = DateTimeHelper.GetEndOfCurrentYear();
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-01-01 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-12-31 23:59:59:9990000
}
//是否是闰年和年的天数
if (true)
{
bool b2015 = DateTimeHelper.IsLeapOfYear();
bool b2016 = DateTimeHelper.IsLeapOfCurrentYear();
Console.WriteLine("2015闰年?{0}", b2015);//2015闰年?False
Console.WriteLine("2016闰年?{0}", b2016);//2016闰年?True
int days2015 = DateTimeHelper.GetTotalDaysOfYear(new DateTime(, , ));
int days2016 = DateTimeHelper.GetTotalDaysOfYear();
Console.WriteLine("2015多少天:{0}", days2015);//2015多少天:365
Console.WriteLine("2016多少天:{0}", days2016);//2016多少天:366
}
//季度相关的
if (true)
{
DateTime start = DateTimeHelper.GetStartOfQuarter(, );
DateTime end = DateTimeHelper.GetEndOfQuarter(, );
int quarter = DateTimeHelper.GetQuarter();
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));// 2016-01-01 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-03-31 23:59:59:9990000
Console.WriteLine(quarter);//
}
//月相关的
if (true)
{
DateTime start = DateTimeHelper.GetStartOfCurrentMonth();
DateTime end = DateTimeHelper.GetEndOfMonth(DateTime.Now);
int days = DateTimeHelper.GetTotalDayOfMonth(DateTime.Now);
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-01 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-30 23:59:59:9990000
Console.WriteLine(days);//
}
//旬相关的
if (true)
{
DateTime start = DateTimeHelper.GetStartOfCurrentTen();
DateTime end = DateTimeHelper.GetEndOfTenFirst(, );
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-11 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-12-10 23:59:59:9990000
}
//周相关
if (true)
{
DateTime start = DateTimeHelper.GetStartOfWeek(DateTime.Now);
DateTime end = DateTimeHelper.GetEndOfWeek(DateTime.Now);
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-13 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-19 23:59:59:9990000
}
//日相关
if (true)
{
DateTime start = DateTimeHelper.GetStartOfCurrentDay();
DateTime end = DateTimeHelper.GetEndOfCurrentDay();
Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 00:00:00:0000000
Console.WriteLine(end.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-11-15 23:59:59:9990000
}
//格式控制
if (true)
{
DateTime dt = DateTime.Now;
string s1 = DateTimeHelper.GetDateString(dt);
string s2 = DateTimeHelper.GetDateTimeString(dt);
string s3 = DateTimeHelper.GetDateTimeFullString(dt);
Console.WriteLine(s1);//2016-11-15
Console.WriteLine(s2);//2016-11-15 13:13:12
Console.WriteLine(s3);//2016-11-15 13:13:12:9846102
}
//日期比较的
if (true)
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(, , );
DateTime dt3 = DateTime.Now.AddDays(-).AddMinutes().AddSeconds().AddMilliseconds();
DateTime dt4 = DateTime.Parse("2016-07-12");
List<DateTime> list = new List<DateTime>() { dt1, dt2, dt3, dt4 };
DateTime max = DateTimeHelper.GetMax(list.ToArray());
DateTime min = DateTimeHelper.GetMin(list.ToArray());
Console.WriteLine("dt1={0}\ndt2={1}\ndt3={2}\ndt4={3}\n",
DateTimeHelper.GetDateTimeString(dt1),
DateTimeHelper.GetDateTimeString(dt2),
DateTimeHelper.GetDateTimeString(dt3),
DateTimeHelper.GetDateTimeString(dt4));
Console.WriteLine("max=" + DateTimeHelper.GetDateTimeString(max));
Console.WriteLine("min=" + DateTimeHelper.GetDateTimeString(min));
//dt1=2016-11-15 13:26:14
//dt2=2016-02-03 00:00:00
//dt3=2016-11-14 13:28:18
//dt4=2016-07-12 00:00:00
//
//max=2016-11-15 13:26:14
//min=2016-02-03 00:00:00 max = DateTimeHelper.GetMax(new DateTime(, , ), new DateTime(, , ));
Console.WriteLine("max=" + DateTimeHelper.GetDateTimeString(max));//max=2016-01-03 00:00:00
}
//获取日期的差值
if (true)
{
DateTime dt1 = new DateTime(, , , , , );
DateTime dt2 = new DateTime(, , , , , );
TimeSpan timeSpan = DateTimeHelper.GetDifference(dt1, dt2);
Console.WriteLine("dt1-dt2={0}天{1}小时{2}分钟{3}秒{4}毫秒", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
//dt1-dt2=1天12小时8分钟9秒0毫秒
double days = DateTimeHelper.GetDifferenceOfDay(dt1, dt2);
double hours = DateTimeHelper.GetDifferenceOfHours(dt1, dt2);
double minutes = DateTimeHelper.GetDifferenceOfMinutes(dt1, dt2);
double seconds = DateTimeHelper.GetDifferenceOfSeconds(dt1, dt2); Console.WriteLine("dt1-dt2={0}days", days);
Console.WriteLine("dt1-dt2={0}hours", hours);
Console.WriteLine("dt1-dt2={0}minutes", minutes);
Console.WriteLine("dt1-dt2={0}seconds", seconds);
//dt1-dt2=1.50565972222222days
//dt1-dt2=36.1358333333333hours
//dt1-dt2=2168.15minutes
//dt1-dt2=130089seconds
}
//解析日期扩展
if (true)
{
DateTime dt1 = DateTime.Parse("2016-10-12");
DateTime dt2= DateTimeHelper.ParseExt("2016年10月12日");
Console.WriteLine(dt1.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-10-12 00:00:00:0000000
Console.WriteLine(dt2.ToString("yyyy-MM-dd HH:mm:ss:fffffff"));//2016-10-12 00:00:00:0000000
}
Console.ReadKey();
}
}
}