import { getDateNextMonth , getDatePreviousMonth } from "./dateUtils" ; export class MonthAndYear { public static fromDate ( date ) { return date == null ? undefined : new MonthAndYear ( date . getMonth ( ) , date . getFullYear ( ) ) ; } private date : Date ; constructor ( month ? , year ? ) { if ( month !== null && year !== null ) { this . date = new Date ( year , month ) ; } else { this . date = new Date ( ) ; } } public clone ( ) { return new MonthAndYear ( this . getMonth ( ) , this . getYear ( ) ) ; } public getFullDate ( ) { return this . date ; } public getMonth ( ) { return this . date . getMonth ( ) ; } public getYear ( ) { return this . date . getFullYear ( ) ; } public getPreviousMonth ( ) { const previousMonthDate = getDatePreviousMonth ( this . date ) ; return new MonthAndYear ( previousMonthDate . getMonth ( ) , previousMonthDate . getFullYear ( ) ) ; } public getNextMonth ( ) { const nextMonthDate = getDateNextMonth ( this . date ) ; return new MonthAndYear ( nextMonthDate . getMonth ( ) , nextMonthDate . getFullYear ( ) ) ; } public isBefore ( monthAndYear ) { return compareMonthAndYear ( this , monthAndYear ) < 0 ; } public isAfter ( monthAndYear ) { return compareMonthAndYear ( this , monthAndYear ) > 0 ; } public isSame ( monthAndYear ) { return compareMonthAndYear ( this , monthAndYear ) === 0 ; } public isSameMonth ( monthAndYear ) { return this . getMonth ( ) === monthAndYear . getMonth ( ) ; } } function compareMonthAndYear ( firstMonthAndYear , secondMonthAndYear ) { const firstMonth = firstMonthAndYear . getMonth ( ) ; const firstYear = firstMonthAndYear . getYear ( ) ; const secondMonth = secondMonthAndYear . getMonth ( ) ; const secondYear = secondMonthAndYear . getYear ( ) ; if ( firstYear === secondYear ) { return firstMonth - secondMonth ; } else { return firstYear - secondYear ; } }