1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { getDateNextMonth, getDatePreviousMonth } from "./dateUtils";
|
17 | export class MonthAndYear {
|
18 | static fromDate(date) {
|
19 | return date == null ? undefined : new MonthAndYear(date.getMonth(), date.getFullYear());
|
20 | }
|
21 | date;
|
22 | constructor(month, year) {
|
23 | if (month !== null && year !== null) {
|
24 | this.date = new Date(year, month);
|
25 | }
|
26 | else {
|
27 | this.date = new Date();
|
28 | }
|
29 | }
|
30 | clone() {
|
31 | return new MonthAndYear(this.getMonth(), this.getYear());
|
32 | }
|
33 | getFullDate() {
|
34 | return this.date;
|
35 | }
|
36 | getMonth() {
|
37 | return this.date.getMonth();
|
38 | }
|
39 | getYear() {
|
40 | return this.date.getFullYear();
|
41 | }
|
42 | getPreviousMonth() {
|
43 | const previousMonthDate = getDatePreviousMonth(this.date);
|
44 | return new MonthAndYear(previousMonthDate.getMonth(), previousMonthDate.getFullYear());
|
45 | }
|
46 | getNextMonth() {
|
47 | const nextMonthDate = getDateNextMonth(this.date);
|
48 | return new MonthAndYear(nextMonthDate.getMonth(), nextMonthDate.getFullYear());
|
49 | }
|
50 | isBefore(monthAndYear) {
|
51 | return compareMonthAndYear(this, monthAndYear) < 0;
|
52 | }
|
53 | isAfter(monthAndYear) {
|
54 | return compareMonthAndYear(this, monthAndYear) > 0;
|
55 | }
|
56 | isSame(monthAndYear) {
|
57 | return compareMonthAndYear(this, monthAndYear) === 0;
|
58 | }
|
59 | isSameMonth(monthAndYear) {
|
60 | return this.getMonth() === monthAndYear.getMonth();
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | function compareMonthAndYear(firstMonthAndYear, secondMonthAndYear) {
|
67 | const firstMonth = firstMonthAndYear.getMonth();
|
68 | const firstYear = firstMonthAndYear.getYear();
|
69 | const secondMonth = secondMonthAndYear.getMonth();
|
70 | const secondYear = secondMonthAndYear.getYear();
|
71 | if (firstYear === secondYear) {
|
72 | return firstMonth - secondMonth;
|
73 | }
|
74 | else {
|
75 | return firstYear - secondYear;
|
76 | }
|
77 | }
|
78 |
|
\ | No newline at end of file |