Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/components/Table/utils/getMonthWeeks.ts 1.33 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  import {
    addWeeks,
    endOfMonth,
    getWeeksInMonth,
    Locale,
    startOfMonth
  } from 'date-fns';
  
  import { daysToMonthWeeks } from './daysToMonthWeeks';
  
  /** Represents a week in the month.*/
  export type MonthWeek = {
    /** The week number from the start of the year. */
    weekNumber: number;
    /** The dates in the week. */
    dates: Date[];
  };
  
  /**
   * Return the weeks belonging to the given month, adding the "outside days" to
   * the first and last week.
   */
  export function getMonthWeeks(
    month: Date,
    options: {
      locale: Locale;
      useFixedWeeks?: boolean;
      weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
      firstWeekContainsDate?: 1 | 4;
      ISOWeek?: boolean;
    }
  ): MonthWeek[] {
    const weeksInMonth: MonthWeek[] = daysToMonthWeeks(
      startOfMonth(month),
      endOfMonth(month),
      options
    );
  
    if (options?.useFixedWeeks) {
      // Add extra weeks to the month, up to 6 weeks
      const nrOfMonthWeeks = getWeeksInMonth(month, options);
      if (nrOfMonthWeeks < 6) {
        const lastWeek = weeksInMonth[weeksInMonth.length - 1];
        const lastDate = lastWeek.dates[lastWeek.dates.length - 1];
        const toDate = addWeeks(lastDate, 6 - nrOfMonthWeeks);
        const extraWeeks = daysToMonthWeeks(
          addWeeks(lastDate, 1),
          toDate,
          options
        );
        weeksInMonth.push(...extraWeeks);
      }
    }
    return weeksInMonth;
  }