1.DateOf、ToDayAt、TomorrowAt
DateOf:指定年月日时分秒
public static DateTimeOffset DateOf(int hour, int minute, int second) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); DateTimeOffset c = SystemTime.Now(); DateTime dt = new DateTime(c.Year, c.Month, c.Day, hour, minute, second); return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local)); } public static DateTimeOffset DateOf(int hour, int minute, int second, int dayOfMonth, int month) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); ValidateDayOfMonth(dayOfMonth); ValidateMonth(month); DateTimeOffset c = SystemTime.Now(); DateTime dt = new DateTime(c.Year, month, dayOfMonth, hour, minute, second); return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local)); } public static DateTimeOffset DateOf(int hour, int minute, int second, int dayOfMonth, int month, int year) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); ValidateDayOfMonth(dayOfMonth); ValidateMonth(month); ValidateYear(year); DateTime dt = new DateTime(year, month, dayOfMonth, hour, minute, second); return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local)); }
TodayAt:DateOf的一个封装
public static DateTimeOffset TodayAt(int hour, int minute, int second) { return DateOf(hour, minute, second); }
TomorrowAt:AddDays的一次操作
public static DateTimeOffset TomorrowAt(int hour, int minute, int second) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); DateTimeOffset now = DateTimeOffset.Now; DateTimeOffset c = new DateTimeOffset( now.Year, now.Month, now.Day, hour, minute, second, 0, now.Offset); // advance one day c = c.AddDays(1); return c; }
2.EvenHourDate、EvenHourDateAfterNow、EvenHourDateBefore
小时四舍五入操作
EvenHourDate:AddHours(1)操作,分、秒抹零操作 比如8:12变成9:00
/// <summary> /// Returns a date that is rounded to the next even hour above the given date. /// </summary> /// <remarks> /// For example an input date with a time of 08:13:54 would result in a date /// with the time of 09:00:00. If the date's time is in the 23rd hour, the /// date's 'day' will be promoted, and the time will be set to 00:00:00. /// </remarks> /// <param name="date">the Date to round, if <see langword="null" /> the current time will /// be used</param> /// <returns>the new rounded date</returns> public static DateTimeOffset EvenHourDate(DateTimeOffset? date) { if (!date.HasValue) { date = SystemTime.Now(); } DateTimeOffset d = date.Value.AddHours(1); return new DateTimeOffset(d.Year, d.Month, d.Day, d.Hour, 0, 0, d.Offset); }
EvenHourDateAfterNow:当前时间加一小时,EvenHourDate(null)的封装
EvenHourDateBefore:分秒抹零操作
/// <summary> /// Returns a date that is rounded to the previous even hour below the given date. /// </summary> /// <remarks> /// For example an input date with a time of 08:13:54 would result in a date /// with the time of 08:00:00. /// </remarks> /// <param name="date">the Date to round, if <see langword="null" /> the current time will /// be used</param> /// <returns>the new rounded date</returns> public static DateTimeOffset EvenHourDateBefore(DateTimeOffset? date) { if (!date.HasValue) { date = SystemTime.Now(); } return new DateTimeOffset(date.Value.Year, date.Value.Month, date.Value.Day, date.Value.Hour, 0, 0, date.Value.Offset); }
3.EvenMinuteDate、EvenMinuteDateAfterNow、EvenMinuteDateBefore
分钟操作,和前面的小时操作方法差不多
EvenMinuteDate:AddMinutes(1)操作
EvenMinuteDateAfterNow:DateTimeOffset.Now.AddMinutes(1)操作
EvenMinuteDateBefore:分钟抹零操作
4.EvenSecondDate、EvenSecondDateAfterNow、EvenSecondDateBefore
分钟操作,和前面的小时和分钟操作方法差不多
EvenSecondDate:AddSeconds(1)操作
EvenSecondDateAfterNow:DateTimeOffset.Now.AddSeconds(1)操作
EvenSecondDateBefore:秒钟抹零操作
5.NextGivenMinuteDate、NextGivenSecondDate
返回一个日期,该日期四舍五入到给定分秒的下一个偶数倍。
NextGivenMinuteDate
public static DateTimeOffset NextGivenMinuteDate(DateTimeOffset? date, int minuteBase) { if (minuteBase < 0 || minuteBase > 59) { throw new ArgumentException("minuteBase must be >=0 and <= 59"); } DateTimeOffset c = date ?? SystemTime.Now(); if (minuteBase == 0) { return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, 0, 0, 0, c.Offset).AddHours(1); } int minute = c.Minute; int arItr = minute/minuteBase; int nextMinuteOccurance = minuteBase*(arItr + 1); if (nextMinuteOccurance < 60) { return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, nextMinuteOccurance, 0, 0, c.Offset); } return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, 0, 0, 0, c.Offset).AddHours(1); }
NextGivenSecondDate
public static DateTimeOffset NextGivenSecondDate(DateTimeOffset? date, int secondBase) { if (secondBase < 0 || secondBase > 59) { throw new ArgumentException("secondBase must be >=0 and <= 59"); } DateTimeOffset c = date ?? SystemTime.Now(); if (secondBase == 0) { return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, 0, 0, c.Offset).AddMinutes(1); } int second = c.Second; int arItr = second/secondBase; int nextSecondOccurance = secondBase*(arItr + 1); if (nextSecondOccurance < 60) { return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, nextSecondOccurance, 0, c.Offset); } return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, 0, 0, c.Offset).AddMinutes(1); }
6.FutureDate
指定年、月、周、日、时、分、秒加一操作
public static DateTimeOffset FutureDate(int interval, IntervalUnit unit) { return TranslatedAdd(SystemTime.Now(), unit, interval); }
private static DateTimeOffset TranslatedAdd(DateTimeOffset date, IntervalUnit unit, int amountToAdd) { switch (unit) { case IntervalUnit.Day: return date.AddDays(amountToAdd); case IntervalUnit.Hour: return date.AddHours(amountToAdd); case IntervalUnit.Minute: return date.AddMinutes(amountToAdd); case IntervalUnit.Month: return date.AddMonths(amountToAdd); case IntervalUnit.Second: return date.AddSeconds(amountToAdd); case IntervalUnit.Millisecond: return date.AddMilliseconds(amountToAdd); case IntervalUnit.Week: return date.AddDays(amountToAdd*7); case IntervalUnit.Year: return date.AddYears(amountToAdd); default: throw new ArgumentException("Unknown IntervalUnit"); } }
7.NewDate、NewDateInTimeZone、InMonth、InYear、InTimeZone、InMonthOnDay、AtMinute、AtSecond、AtHourMinuteAndSecond、OnDay
实例化DateBuilder指定年月日时分秒时区
public static DateBuilder NewDate() { return new DateBuilder(); } /// <summary> /// Create a DateBuilder, with initial settings for the current date and time in the given timezone. /// </summary> /// <param name="tz">Time zone to use.</param> /// <returns></returns> public static DateBuilder NewDateInTimeZone(TimeZoneInfo tz) { return new DateBuilder(tz); }
public class DateBuilder { private int month; private int day; private int year; private int hour; private int minute; private int second; private TimeZoneInfo tz; /// <summary> /// Create a DateBuilder, with initial settings for the current date /// and time in the system default timezone. /// </summary> private DateBuilder() { DateTime now = DateTime.Now; month = now.Month; day = now.Day; year = now.Year; hour = now.Hour; minute = now.Minute; second = now.Second; } /// <summary> /// Create a DateBuilder, with initial settings for the current date and time in the given timezone. /// </summary> /// <param name="tz"></param> private DateBuilder(TimeZoneInfo tz) { DateTime now = DateTime.Now; month = now.Month; day = now.Day; year = now.Year; hour = now.Hour; minute = now.Minute; second = now.Second; this.tz = tz; } }
8.ValidateYear、ValidateMonth、ValidateDay、ValidateHour、ValidateMinute、ValidateSecond
验证年月日时分秒
public static void ValidateHour(int hour) { if (hour < 0 || hour > 23) { throw new ArgumentException("Invalid hour (must be >= 0 and <= 23)."); } } public static void ValidateMinute(int minute) { if (minute < 0 || minute > 59) { throw new ArgumentException("Invalid minute (must be >= 0 and <= 59)."); } } public static void ValidateSecond(int second) { if (second < 0 || second > 59) { throw new ArgumentException("Invalid second (must be >= 0 and <= 59)."); } } public static void ValidateDayOfMonth(int day) { if (day < 1 || day > 31) { throw new ArgumentException("Invalid day of month."); } } public static void ValidateMonth(int month) { if (month < 1 || month > 12) { throw new ArgumentException("Invalid month (must be >= 1 and <= 12)."); } } public static void ValidateYear(int year) { if (year < 1970 || year > 2099) { throw new ArgumentException("Invalid year (must be >= 1970 and <= 2099)."); } }
9.Build
生成DateTimeOffset
public DateTimeOffset Build() { DateTime dt = new DateTime(year, month, day, hour, minute, second); TimeSpan offset = TimeZoneUtil.GetUtcOffset(dt, tz ?? TimeZoneInfo.Local); return new DateTimeOffset(dt, offset); }