static void Main()
    {
        LocalDate     date = new LocalDate(2012, 1, 1);
        IWeekYearRule rule = WeekYearRules.Iso;

        Console.WriteLine($"WeekYear: {rule.GetWeekYear(date)}");             // 2011
        Console.WriteLine($"WeekOfWeekYear: {rule.GetWeekOfWeekYear(date)}"); // 52
    }
        public DateInterval Convert(PublicationPeriod publicationPeriod)
        {
            IWeekYearRule rule       = WeekYearRules.Iso;
            var           today      = LocalDate.FromDateTime(DateTime.Today);
            var           weekNumber = rule.GetWeekOfWeekYear(today);

            switch (publicationPeriod)
            {
            case PublicationPeriod.ThisWeek:
                LocalDate startOfThisWeek = LocalDate.FromWeekYearWeekAndDay(today.Year, weekNumber, IsoDayOfWeek.Monday);
                return(new DateInterval(startOfThisWeek, LocalDate.FromDateTime(DateTime.Today)));

            case PublicationPeriod.LastWeek:
                LocalDate startOfLastWeek = LocalDate.FromWeekYearWeekAndDay(today.Year, rule.GetWeekOfWeekYear(today.PlusWeeks(-1)), IsoDayOfWeek.Monday);
                return(new DateInterval(startOfLastWeek, startOfLastWeek.PlusWeeks(1).PlusDays(-1)));

            case PublicationPeriod.ThisMonth:
                var startOfThisMonth = LocalDate.FromDateTime(new DateTime(today.Year, today.Month, 1));
                return(new DateInterval(startOfThisMonth, today));

            case PublicationPeriod.LastMonth:
                var startOfLastMonth = LocalDate.FromDateTime(new DateTime(today.Year, today.Month, 1)).PlusMonths(-1);
                var endOfLastMonth   = new LocalDate(startOfLastMonth.Year, startOfLastMonth.Month, startOfLastMonth.Calendar.GetDaysInMonth(startOfLastMonth.Year, startOfLastMonth.Month));
                return(new DateInterval(startOfLastMonth, endOfLastMonth));

            case PublicationPeriod.ThisYear:
                var startOfThisYear = LocalDate.FromDateTime(new DateTime(today.Year, 1, 1));
                return(new DateInterval(startOfThisYear, today));

            case PublicationPeriod.LastYear:
                var startOfLastYear = LocalDate.FromDateTime(new DateTime(today.Year, 1, 1)).PlusYears(-1);
                var endOfLastYear   = LocalDate.FromDateTime(new DateTime(today.Year, 12, 31)).PlusYears(-1);
                return(new DateInterval(startOfLastYear, endOfLastYear));

            default:
                throw new ArgumentOutOfRangeException(nameof(publicationPeriod), publicationPeriod, null);
            }
        }
示例#3
0
 /// <summary>
 /// Convenience overload to call <see cref="IWeekYearRule.GetWeeksInWeekYear(int, CalendarSystem)"/> with
 /// the ISO calendar system.
 /// </summary>
 /// <param name="rule">The rule to delegate the call to.</param>
 /// <param name="weekYear">The week year to calculate the number of contained weeks.</param>
 /// <returns>The number of weeks in the given week year.</returns>
 public static int GetWeeksInWeekYear([NotNull] this IWeekYearRule rule, int weekYear) =>
 Preconditions.CheckNotNull(rule, nameof(rule)).GetWeeksInWeekYear(weekYear, CalendarSystem.Iso);
示例#4
0
 /// <summary>
 /// Convenience method to call <see cref="IWeekYearRule.GetLocalDate(int, int, IsoDayOfWeek, CalendarSystem)"/>
 /// passing in the ISO calendar system.
 /// </summary>
 /// <param name="rule">The rule to delegate the call to.</param>
 /// <param name="weekYear">The week-year of the new date. Implementations provided by Noda Time allow any
 /// year which is a valid calendar year, and sometimes one less than the minimum calendar year
 /// and/or one more than the maximum calendar year, to allow for dates near the start of a calendar
 /// year to fall in the previous week year, and similarly for dates near the end of a calendar year.</param>
 /// <param name="weekOfWeekYear">The week of week-year of the new date. Valid values for this parameter
 /// may vary depending on <paramref name="weekYear"/>, as the length of a year in weeks varies.</param>
 /// <param name="dayOfWeek">The day-of-week of the new date. Valid values for this parameter may vary
 /// depending on <paramref name="weekYear"/> and <paramref name="weekOfWeekYear"/>.</param>
 /// <exception cref="ArgumentOutOfRangeException">The parameters do not combine to form a valid date.</exception>
 /// <returns>A <see cref="LocalDate"/> corresponding to the specified values.</returns>
 public static LocalDate GetLocalDate([NotNull] this IWeekYearRule rule, int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) =>
 Preconditions.CheckNotNull(rule, nameof(rule)).GetLocalDate(weekYear, weekOfWeekYear, dayOfWeek, CalendarSystem.Iso);