Call the method as below whenever you need it for current month based on current date.
console.log(this.getMonthLastWeekendDate());
//methods for it..
getMonthLastWeekendDate(): Date {
const date = new Date();
const y = date.getFullYear();
const m = date.getMonth();
const lastDay = new Date(y, m + 1, 0);
const weekendingDate = this.getLastWeekDay(lastDay);
return weekendingDate;
}
getNextDayOfWeek(date: Date, dayOfWeek: number): Date {
const resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
getLastWeekDay(lastDay: Date): Date {
let weekendingDateTemp;
const dayOfWeek = lastDay.getDay();
switch (dayOfWeek) {
case 5:
weekendingDateTemp = lastDay;
break;
case 6:
weekendingDateTemp = this.getDateBeforeDays(lastDay, 1);
break;
case 0:
weekendingDateTemp = this.getDateBeforeDays(lastDay, 2);
break;
default:
weekendingDateTemp = this.getNextDayOfWeek(lastDay, 5);
}
return weekendingDateTemp;
}
getDateBeforeDays(inputDate: Date, numberOfDays: number): Date {
const finaltempDate = new Date(inputDate.setDate(inputDate.getDate() - numberOfDays ));
return finaltempDate;
}
console.log(this.getMonthLastWeekendDate());
//methods for it..
getMonthLastWeekendDate(): Date {
const date = new Date();
const y = date.getFullYear();
const m = date.getMonth();
const lastDay = new Date(y, m + 1, 0);
const weekendingDate = this.getLastWeekDay(lastDay);
return weekendingDate;
}
getNextDayOfWeek(date: Date, dayOfWeek: number): Date {
const resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
getLastWeekDay(lastDay: Date): Date {
let weekendingDateTemp;
const dayOfWeek = lastDay.getDay();
switch (dayOfWeek) {
case 5:
weekendingDateTemp = lastDay;
break;
case 6:
weekendingDateTemp = this.getDateBeforeDays(lastDay, 1);
break;
case 0:
weekendingDateTemp = this.getDateBeforeDays(lastDay, 2);
break;
default:
weekendingDateTemp = this.getNextDayOfWeek(lastDay, 5);
}
return weekendingDateTemp;
}
getDateBeforeDays(inputDate: Date, numberOfDays: number): Date {
const finaltempDate = new Date(inputDate.setDate(inputDate.getDate() - numberOfDays ));
return finaltempDate;
}
No comments:
Post a Comment