UNPKG

2.65 kBJavaScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { getDateNextMonth, getDatePreviousMonth } from "./dateUtils";
17export 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// returns negative if left < right
64// returns positive if left > right
65// returns 0 if left === right
66function 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//# sourceMappingURL=monthAndYear.js.map
\No newline at end of file