/// <summary> /// First day of week /// </summary> /// <param name="ldt"></param> /// <returns></returns> public static LocalDateTime FirstDayOfWeek(this LocalDateTime ldt) { var currentCulture = CultureInfo.CurrentCulture; var firstDayOfWeek = (int)currentCulture.DateTimeFormat.FirstDayOfWeek; var currentDayOfWeek = (int)ldt.DayOfWeek; var offset = currentDayOfWeek - firstDayOfWeek < 0 ? 7 : 0; var numberOfDaysSinceBeginningOfTheWeek = currentDayOfWeek + offset - firstDayOfWeek; return(ldt.AddDays(-numberOfDaysSinceBeginningOfTheWeek)); }
/// <summary> /// Previous Year /// </summary> /// <param name="ld"></param> /// <returns></returns> public static LocalDateTime PreviousYear(this LocalDateTime ld) { var year = ld.Year - 1; var daysOfMonth = DateTime.DaysInMonth(year, ld.Month); if (daysOfMonth == ld.Day) { return(new LocalDateTime(year, ld.Month, ld.Day, ld.Hour, ld.Minute, ld.Second, ld.Millisecond, ld.Calendar)); } var d = ld.Day - daysOfMonth; var p = new LocalDateTime(year, ld.Month, daysOfMonth, ld.Hour, ld.Minute, ld.Second, ld.Millisecond, ld.Calendar); return(p.AddDays(d)); }