public static DateInfo OffsetBy(this DateInfo dt, int offsetVal, DateTimeOffsetStyles styles) { return(styles switch { DateTimeOffsetStyles.Day => DateInfoCalc.OffsetByDays(dt, offsetVal), DateTimeOffsetStyles.Week => DateInfoCalc.OffsetByWeeks(dt, offsetVal), DateTimeOffsetStyles.Month => DateInfoCalc.OffsetByMonths(dt, offsetVal, DateTimeOffsetOptions.Relatively), DateTimeOffsetStyles.Quarters => DateInfoCalc.OffsetByQuarters(dt, offsetVal, DateTimeOffsetOptions.Relatively), DateTimeOffsetStyles.Year => DateInfoCalc.OffsetByYears(dt, offsetVal, DateTimeOffsetOptions.Relatively), _ => DateInfoCalc.OffsetByDays(dt, offsetVal) });
/// <summary> /// Previous Year /// </summary> /// <param name="d"></param> /// <returns></returns> public static DateInfo PreviousYear(this DateInfo d) { var year = d.Year - 1; var daysOfMonth = DateTime.DaysInMonth(year, d.Month); if (daysOfMonth == d.Day) { return(new DateInfo(year, d.Month, d.Day)); } var m = d.Day - daysOfMonth; var p = new DateInfo(year, d.Month, daysOfMonth); return(p + m.Days()); }
/// <summary> /// Create <see cref="DateInfo"/> by special year, month and offset info.<br /> /// 根据指定的年月和偏移信息创建 <see cref="DateInfo"/> /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="weekAtMonth"></param> /// <param name="dayOfWeek"></param> /// <returns></returns> public static DateInfo OffsetByWeek(int year, int month, int weekAtMonth, int dayOfWeek) { if (weekAtMonth == 0 || weekAtMonth > 5) { throw new ArgumentException("weekAtMonth is invalid.", nameof(weekAtMonth)); } var targetDay = DateTimeCalcHelper.GetTargetDays(year, month, weekAtMonth, dayOfWeek); if (targetDay > DateInfo.DaysInMonth(year, month)) { return(DateInfo.InfinitePast); } return(DateInfoFactory.Create(year, month, targetDay)); }
/// <summary> /// Add business days<br /> /// 添加指定个数量的工作日 /// </summary> /// <param name="d"></param> /// <param name="days"></param> /// <returns></returns> public static DateInfo AddBusinessDays(this DateInfo d, int days) { var sign = Math.Sign(days); var unsignedDays = Math.Abs(days); for (var i = 0; i < unsignedDays; i++) { do { d = d.AddDays(sign); } while (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday); } return(d); }
/// <summary> /// Add quarters. <br /> /// 添加一个季度。 /// </summary> /// <param name="dt"></param> /// <param name="months"></param> /// <param name="options"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static DateInfo OffsetByMonths(DateInfo dt, int months, DateTimeOffsetOptions options = DateTimeOffsetOptions.Absolute) { if (options == DateTimeOffsetOptions.Absolute) { return(dt + months.Months()); } var(year, month) = DateTimeCalcHelper.Calc(dt.Year, dt.Month, months); var firstDayOfMonth = dt.SetDate(year, month, 1); var lastDayOfMonth = firstDayOfMonth.LastDayOfMonth().Day; var day = dt.Day > lastDayOfMonth ? lastDayOfMonth : dt.Day; return(dt.SetDate(year, month, day)); }
public static bool IsSameYear(this DateInfo d, DateInfo date) => d.Year == date.Year;
public static bool IsSameMonth(this DateInfo d, DateInfo date) => d.Month == date.Month && d.Year == date.Year;
public static bool IsSameDay(this DateInfo d, DateInfo date) => d.DateTimeRef == date.DateTimeRef;
public static DateInfo AddWeeks(this DateInfo d, int weeks) => d.AddDays(weeks * 7);
/// <summary> /// Get last day of this week /// </summary> /// <param name="d"></param> /// <param name="cultureInfo"></param> /// <returns></returns> public static DateInfo GetFirstDayInfoOfWeek(this DateInfo d, CultureInfo cultureInfo) => d.DateTimeRef.GetFirstDayInfoOfWeek(cultureInfo);
public static DateInfo SetDay(this DateInfo d, int day) { d.Day = day; return(d); }
public static DateInfo SetYear(this DateInfo d, int year) { d.Year = year; return(d); }
public static bool IsBefore(this DateInfo d, DateInfo toCompareWith) => d < toCompareWith;
/// <summary> /// Is current date between <paramref name="min"/> and <paramref name="max"/> without boundary.<br /> /// 判断当前日期是否在 min 和 max 之间,开区间。 /// </summary> /// <param name="d"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> public static bool IsDateBetweenWithoutBoundary(this DateInfo d, DateInfo min, DateInfo max) { var dtRef = d.DateTimeRef; var minRef = min.DateTimeRef; var maxRef = max.DateTimeRef; return(dtRef.IsDateBetweenWithoutBoundary(minRef, maxRef)); }
/// <summary> /// Get last day of this week /// </summary> /// <param name="d"></param> /// <returns></returns> public static DateInfo GetLastDayInfoOfWeek(this DateInfo d) => d.GetLastDayInfoOfWeek(null);
/// <summary> /// Gets first day of year /// </summary> /// <param name="d"></param> /// <param name="dayOfWeek"></param> /// <returns></returns> public static DateInfo GetFirstDayInfoOfYear(this DateInfo d, DayOfWeek dayOfWeek) => d.DateTimeRef.GetFirstDayInfoOfYear(dayOfWeek);
/// <summary> /// Gets first day of year /// </summary> /// <param name="d"></param> /// <returns></returns> public static DateInfo GetFirstDayInfoOfYear(this DateInfo d) => new DateInfo(d.Year, 1, 1);
/// <summary> /// Gets first day info of quarter /// </summary> /// <param name="d"></param> /// <returns></returns> public static DateInfo GetFirstDayInfoOfQuarter(this DateInfo d) => d.DateTimeRef.GetFirstDayInfoOfQuarter();
/// <summary> /// Get first day of this month. /// </summary> /// <param name="dt"></param> /// <returns></returns> public static DateInfo GetFirstDayInfoOfMonth(this DateInfo dt) => new DateInfo(dt.Year, dt.Month, 1);
public static DateInfo SetDate(this DateInfo d, int year, int month) { d.Year = year; d.Month = month; return(d); }
public static DateInfo AddQuarters(this DateInfo d, int quarters) => d.AddMonths(quarters * 3);
/// <summary> /// To ago.<br /> /// 转换为 Ago /// </summary> /// <param name="d"></param> /// <returns></returns> #pragma warning disable 618 public static string ToAgo(this DateInfo d) => (DateTime.Now - d).ToAgo();
public static DateInfo SetMonth(this DateInfo d, int month) { d.Month = month; return(d); }
public static bool TryParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles styles, out DateInfo result) { return(DateInfoParse.TryParseExactMultiple(s, formats, DateTimeFormatInfo.GetInstance(provider), styles, out result)); }
public static bool IsAfter(this DateInfo d, DateInfo toCompareWith) => d > toCompareWith;
/// <summary> /// Get last day info of year /// </summary> /// <param name="d"></param> /// <returns></returns> public static DateInfo GetLastDayInfoOfYear(this DateInfo d) => d.DateTimeRef.GetLastDayInfoOfYear();
public static bool TryParse(string s, out DateInfo result) { return(DateInfoParse.TryParse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out result)); }
public static bool IsInTheFuture(this DateInfo d) => d > DateTime.Now;
public static bool TryParseExactMultiple(string s, string[] formats, DateTimeFormatInfo formatInfo, DateTimeStyles styles, out DateInfo result) { return(TryCreateDateInfo(DateTime.TryParseExact(s, formats, formatInfo, styles, out var time), time, out result)); }
public static bool IsInThePast(this DateInfo d) => d < DateTime.Now;