|
| 1 | +/* |
| 2 | +* This algorithm accepts a month in the format mm/yyyy. |
| 3 | +* And prints out the month's calendar. |
| 4 | +* It uses an epoch of 1/1/1900, Monday. |
| 5 | +*/ |
| 6 | + |
| 7 | +class Month { |
| 8 | + constructor () { |
| 9 | + this.Days = ['M', 'T', 'W', 'Th', 'F', 'S', 'Su'] |
| 10 | + this.BDays = ['M', 'Su', 'S', 'F', 'Th', 'W', 'T'] |
| 11 | + this.epoch = { month: 1, year: 1900 } |
| 12 | + this.monthDays = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 13 | + this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 14 | + } |
| 15 | + |
| 16 | + printCal (days, startDay) { |
| 17 | + console.log('M T W Th F S Su') |
| 18 | + const dates = []; let i |
| 19 | + for (i = 1; i <= days; i++) { |
| 20 | + dates.push(i) |
| 21 | + } |
| 22 | + for (i = 0; i < this.Days.indexOf(startDay); i++) { |
| 23 | + dates.unshift(' ') |
| 24 | + } |
| 25 | + while (true) { |
| 26 | + let row = '' |
| 27 | + for (i = 0; (i < 7) && (dates.length !== 0); i++) { |
| 28 | + row += dates.shift() |
| 29 | + while ((row.length % 4) !== 0) { |
| 30 | + row += ' ' |
| 31 | + } |
| 32 | + } |
| 33 | + console.log(row) |
| 34 | + if (dates.length === 0) break |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + parseDate (date) { |
| 39 | + const dateAr = []; let block = ''; let i |
| 40 | + for (i = 0; i < date.length; i++) { |
| 41 | + if (date[i] === '/') { |
| 42 | + dateAr.push(parseInt(block)) |
| 43 | + block = '' |
| 44 | + continue |
| 45 | + } |
| 46 | + block += date[i] |
| 47 | + } |
| 48 | + dateAr.push(parseInt(block)) |
| 49 | + if (dateAr.length !== 2) throw new Error('Improper string encoding') |
| 50 | + const dateOb = { month: dateAr[0], year: dateAr[1] } |
| 51 | + return dateOb |
| 52 | + } |
| 53 | + |
| 54 | + isLeapYear (year) { |
| 55 | + if (((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))) return true |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + isGreater (startDate, endDate) { |
| 60 | + if (startDate.year > endDate.year) { |
| 61 | + return true |
| 62 | + } else if (startDate.year < endDate.year) { |
| 63 | + return false |
| 64 | + } else if (startDate.month > endDate.month) { |
| 65 | + return true |
| 66 | + } else if (startDate.month < endDate.month) { |
| 67 | + return false |
| 68 | + } |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + getDayDiff (startDate, endDate) { |
| 73 | + if (this.isGreater(startDate, endDate) === null) { |
| 74 | + return 0 |
| 75 | + } else if ((this.isGreater(startDate, endDate) === true)) { |
| 76 | + const midDate = startDate |
| 77 | + startDate = endDate |
| 78 | + endDate = midDate |
| 79 | + } |
| 80 | + let diff = 0 |
| 81 | + while (startDate.year !== endDate.year) { |
| 82 | + diff += (this.isLeapYear(startDate.year)) ? 366 : 365 |
| 83 | + startDate.year = startDate.year + 1 |
| 84 | + } |
| 85 | + while (startDate.month !== endDate.month) { |
| 86 | + if (startDate.month < endDate.month) { |
| 87 | + if (this.isLeapYear(startDate.year)) diff += this.monthDaysLeap[startDate.month] |
| 88 | + else diff += this.monthDays[startDate.month] |
| 89 | + startDate.month = startDate.month + 1 |
| 90 | + } else { |
| 91 | + if (this.isLeapYear(startDate.year)) diff -= this.monthDaysLeap[startDate.month - 1] |
| 92 | + else diff -= this.monthDays[startDate.month - 1] |
| 93 | + startDate.month = startDate.month - 1 |
| 94 | + } |
| 95 | + } |
| 96 | + return diff |
| 97 | + } |
| 98 | + |
| 99 | + generateMonthCal (date) { |
| 100 | + const Month = this.parseDate(date); let day = '' |
| 101 | + let difference = this.getDayDiff(this.epoch, Month) |
| 102 | + difference = difference % 7 |
| 103 | + let Month2 = this.parseDate(date) |
| 104 | + day = (this.isGreater(Month2, this.epoch)) ? this.Days[difference] : this.BDays[difference] |
| 105 | + Month2 = this.parseDate(date) |
| 106 | + if (this.isLeapYear(Month2.year)) this.printCal(this.monthDaysLeap[Month2.month], day) |
| 107 | + else this.printCal(this.monthDays[Month2.month], day) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// testing |
| 112 | +const x = new Month() |
| 113 | +x.generateMonthCal('1/2021') |
0 commit comments