Skip to content

Instantly share code, notes, and snippets.

@sguzunov
Last active May 14, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sguzunov/7d37fd46bf6d3fa64211b13edd64e3ad to your computer and use it in GitHub Desktop.
Save sguzunov/7d37fd46bf6d3fa64211b13edd64e3ad to your computer and use it in GitHub Desktop.
import * as moment from 'moment';
import { RecurrenceType, Recurrence } from '../../models/report/recurrence.model';
export class RecurrencePatternCalculator {
public static calculateNextRecurrenceDate(recurrenceOptions: Recurrence): Date {
switch (recurrenceOptions.recurrenceType) {
case RecurrenceType.Daily:
return RecurrencePatternCalculator.getDailyNextReccurenceOccurenceDate(recurrenceOptions);
case RecurrenceType.Monthly:
return RecurrencePatternCalculator.getMonthlyNextReccurenceOccurenceDate(recurrenceOptions);
case RecurrenceType.Yearly:
return RecurrencePatternCalculator.getYearlyNextReccurenceOccurenceDate(recurrenceOptions);
default:
throw new Error();
}
}
private static getDailyNextReccurenceOccurenceDate(recurrenceOptions: Recurrence): Date {
const startDate: Date = recurrenceOptions.startDate;
const now: number = moment.now();
let nextRecurrenceDate: any = startDate;
const interval: number = recurrenceOptions.interval;
do {
nextRecurrenceDate = moment(nextRecurrenceDate).add(interval, 'days');
} while (!moment(nextRecurrenceDate).isAfter(now) && interval > 0);
return nextRecurrenceDate.toDate();
}
private static getMonthlyNextReccurenceOccurenceDate(recurrenceOptions: Recurrence): Date {
const startDate: Date = recurrenceOptions.startDate;
const startDateMoment: moment.Moment = moment(startDate);
const now: number = moment.now();
const dayOfMonth: number = recurrenceOptions.dayOfMonth;
const interval: number = recurrenceOptions.interval;
let nextDate: moment.Moment = moment(new Date(startDateMoment.year(), startDateMoment.month(), dayOfMonth));
if (nextDate.date() > startDateMoment.date() && moment(nextDate).isAfter(now)) {
return nextDate.toDate();
}
do {
nextDate = moment(nextDate).add(interval, 'months');
} while (!moment(nextDate).isAfter(now) && interval > 0);
return nextDate.toDate();
}
private static getYearlyNextReccurenceOccurenceDate(recurrenceOptions: Recurrence): Date {
const startDate: Date = recurrenceOptions.startDate;
const startDateMoment: moment.Moment = moment(startDate);
const now: number = moment.now();
const dayOfMonth: number = recurrenceOptions.dayOfMonth;
const interval: number = recurrenceOptions.interval;
let nextDate: moment.Moment = moment(new Date(startDateMoment.year(), recurrenceOptions.month, dayOfMonth));
if (moment(nextDate).isAfter(now)) {
if (moment(nextDate).isAfter(startDateMoment)) {
return nextDate.toDate();
} else {
nextDate = nextDate.add(1, 'years');
return nextDate.toDate();
}
}
do {
nextDate = moment(nextDate).add(interval, 'years');
} while (!moment(nextDate).isAfter(now) && interval > 0);
return nextDate.toDate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment