UNPKG

remaining-period-calculator

Version:

It calculates between the two periods

86 lines (85 loc) 3.48 kB
/** * @author Abdulrahman AL Saad * @classdesc It calculates between the two periods */ class remainingPeriodCalculator { /** * * @param {{timeZone:number,year:number,month:number,day:number}} options */ constructor(options) { /**@private */ this.year; /**@private */ this.month; /**@private */ this.day; /**@private */ this.timeZone = 0; if(!options) options ={}; else options = options; if(options.year) this.year = options.year; if(options.month) this.month = options.month; if(options.day) this.day = options.day; if(options.timeZone) this.timeZone = options.timeZone; } /** * * @param {number} year */ remainingYears(year) { if(!year) year = this.getDate().year; return Math.abs(this.getDate().year - year); } /** * * @param {number} month * @param {number} year */ remainingMonths(month,year) { if(!month) month = this.getDate().month; if(!year) year = this.getDate().year; var calMonth = Math.abs(this.getDate().month - month); var calYear = this.remainingYears(year); if(this.getDate().month >= month && year > this.getDate().year) calMonth = month + (12 - this.getDate().month) ; else if(this.getDate().month <= month && year < this.getDate().year) calMonth = this.getDate().month + (12 - month) ; if((this.getDate().month >= month && year > this.getDate().year) || (this.getDate().month <= month && year < this.getDate().year)) calYear -=1; return calMonth + (12 * calYear); } /** * * @param {number} day * @param {number} month * @param {number} year */ remainingDays(day,month,year) { if(!day) day = this.getDate().day; if(!month) month = this.getDate().month; if(!year) year = this.getDate().year; var calMonth = this.remainingMonths(month,year); var calDay = Math.abs(this.getDate().day - day); if((this.getDate().day < day && this.getDate().month > month && year == this.getDate().year) || (this.getDate().day <= day && year < this.getDate().year)) calDay = this.getDate().day + (30 - day); else if((this.getDate().day > day && this.getDate().month < month && year == this.getDate().year) || (this.getDate().day >= day && year > this.getDate().year)) calDay = day + (30 - this.getDate().day); if((this.getDate().day > day && this.getDate().month < month && year == this.getDate().year) || (this.getDate().day < day && this.getDate().month > month && year == this.getDate().year)|| (this.getDate().day >= day && year > this.getDate().year)|| (this.getDate().day <= day && year < this.getDate().year)) calMonth -=1; return calDay + (30 * calMonth); } /**@private */ getDate() { var time = new Date(); time = time.getTime() + (time.getTimezoneOffset() * 60000); time = new Date(time + (3600000 * this.timeZone)); var toDate = time.toLocaleDateString().split("/"); var year = this.year || parseInt(toDate[2]); var month = this.month || parseInt(toDate[0]); var day = this.day || parseInt(toDate[1]); return {day,month,year} } } module.exports = {remainingPeriodCalculator};