UNPKG

3.09 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";
17var MonthAndYear = /** @class */ (function () {
18 function MonthAndYear(month, year) {
19 if (month !== null && year !== null) {
20 this.date = new Date(year, month);
21 }
22 else {
23 this.date = new Date();
24 }
25 }
26 MonthAndYear.fromDate = function (date) {
27 return date == null ? undefined : new MonthAndYear(date.getMonth(), date.getFullYear());
28 };
29 MonthAndYear.prototype.clone = function () {
30 return new MonthAndYear(this.getMonth(), this.getYear());
31 };
32 MonthAndYear.prototype.getFullDate = function () {
33 return this.date;
34 };
35 MonthAndYear.prototype.getMonth = function () {
36 return this.date.getMonth();
37 };
38 MonthAndYear.prototype.getYear = function () {
39 return this.date.getFullYear();
40 };
41 MonthAndYear.prototype.getPreviousMonth = function () {
42 var previousMonthDate = getDatePreviousMonth(this.date);
43 return new MonthAndYear(previousMonthDate.getMonth(), previousMonthDate.getFullYear());
44 };
45 MonthAndYear.prototype.getNextMonth = function () {
46 var nextMonthDate = getDateNextMonth(this.date);
47 return new MonthAndYear(nextMonthDate.getMonth(), nextMonthDate.getFullYear());
48 };
49 MonthAndYear.prototype.isBefore = function (monthAndYear) {
50 return compareMonthAndYear(this, monthAndYear) < 0;
51 };
52 MonthAndYear.prototype.isAfter = function (monthAndYear) {
53 return compareMonthAndYear(this, monthAndYear) > 0;
54 };
55 MonthAndYear.prototype.isSame = function (monthAndYear) {
56 return compareMonthAndYear(this, monthAndYear) === 0;
57 };
58 MonthAndYear.prototype.isSameMonth = function (monthAndYear) {
59 return this.getMonth() === monthAndYear.getMonth();
60 };
61 return MonthAndYear;
62}());
63export { MonthAndYear };
64// returns negative if left < right
65// returns positive if left > right
66// returns 0 if left === right
67function compareMonthAndYear(firstMonthAndYear, secondMonthAndYear) {
68 var firstMonth = firstMonthAndYear.getMonth();
69 var firstYear = firstMonthAndYear.getYear();
70 var secondMonth = secondMonthAndYear.getMonth();
71 var secondYear = secondMonthAndYear.getYear();
72 if (firstYear === secondYear) {
73 return firstMonth - secondMonth;
74 }
75 else {
76 return firstYear - secondYear;
77 }
78}
79//# sourceMappingURL=monthAndYear.js.map
\No newline at end of file