using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace JiShe.CollectBus.Common.Extensions { public static class DateTimeExtensions { /// /// Converts a DateTime to a Unix Timestamp /// /// This DateTime /// [Description("将日期时间转换为Unix时间戳")] public static double ToUnixTimestamp(this DateTime target) { var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); var diff = target - origin; return Math.Floor(diff.TotalSeconds); } /// /// Converts a Unix Timestamp in to a DateTime /// /// This Unix Timestamp /// [Description("将Unix时间戳转换为日期时间")] public static DateTime FromUnixTimestamp(this double unixTime) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); return epoch.AddSeconds(unixTime); } /// /// Gets the value of the End of the day (23:59) /// /// /// [Description("获取一天结束的值(23:59)")] public static DateTime ToDayEnd(this DateTime target) { return target.Date.AddDays(1).AddMilliseconds(-1); } /// /// Gets the First Date of the week for the specified date /// /// this DateTime /// The Start Day of the Week (ie, Sunday/Monday) /// The First Date of the week [Description("获取指定日期的星期的第一个日期")] public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { var diff = dt.DayOfWeek - startOfWeek; if (diff < 0) diff += 7; return dt.AddDays(-1 * diff).Date; } /// /// Returns all the days of a month. /// /// The year. /// The month. /// [Description("获取一个月的所有日期")] public static IEnumerable DaysOfMonth(int year, int month) { return Enumerable.Range(0, DateTime.DaysInMonth(year, month)) .Select(day => new DateTime(year, month, day + 1)); } /// /// Determines the Nth instance of a Date's DayOfWeek in a month /// /// /// 11/29/2011 would return 5, because it is the 5th Tuesday of each month [Description("获取当前日期在一个月的第几个星期")] public static int WeekDayInstanceOfMonth(this DateTime dateTime) { var y = 0; return DaysOfMonth(dateTime.Year, dateTime.Month) .Where(date => dateTime.DayOfWeek.Equals(date.DayOfWeek)) .Select(x => new { n = ++y, date = x }) .Where(x => x.date.Equals(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day))) .Select(x => x.n).FirstOrDefault(); } /// /// Gets the total days in a month /// /// The date time. /// [Description("获取一个月内的总天数")] public static int TotalDaysInMonth(this DateTime dateTime) { return DaysOfMonth(dateTime.Year, dateTime.Month).Count(); } /// /// Get the first day in a month /// /// The date time. /// [Description("获取一个月内的第一天")] public static DateTime FirstInMonth(this DateTime dateTime) { return DateTime.Now.AddDays(1 - DateTime.Now.Day); } /// /// Get the Last Day in a Month 23:59:59 /// /// /// [Description("获取一个月内的最后一天 23:59:59")] public static DateTime LastInMonth(this DateTime dateTime) { return DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1); } /// /// Takes any date and returns it's value as an Unspecified DateTime /// /// /// [Description("获取Unspecified日期")] public static DateTime ToDateTimeUnspecified(this DateTime date) { if (date.Kind == DateTimeKind.Unspecified) { return date; } return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, DateTimeKind.Unspecified); } /// /// Trims the milliseconds off of a datetime /// /// /// [Description("将日期时间缩短毫秒")] public static DateTime TrimMilliseconds(this DateTime date) { return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind); } /// /// Clears the time. /// /// The date time. /// [Description("清除时间")] public static DateTime ClearTime(this DateTime dateTime) { return dateTime.Subtract( new TimeSpan( 0, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond ) ); } } }