forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMonthCalendar.js
More file actions
Latest commit
119 lines (111 loc) · 3.31 KB
/
Copy pathFindMonthCalendar.js
File metadata and controls
119 lines (111 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* This algorithm accepts a month in the format mm/yyyy.
* And prints out the month's calendar.
* It uses an epoch of 1/1/1900, Monday.
*/
import{isLeapYear}from'../Maths/LeapYear'
classMonth{
constructor(){
this.Days=['M','T','W','Th','F','S','Su']
this.BDays=['M','Su','S','F','Th','W','T']
this.epoch={month: 1,year: 1900}
this.monthDays=[31,31,28,31,30,31,30,31,31,30,31,30,31]
this.monthDaysLeap=[31,31,29,31,30,31,30,31,31,30,31,30,31]
}
printCal(days,startDay,output=(value)=>console.log(value)){
output('M T W Th F S Su')
constdates=[]
leti
for(i=1;i<=days;i++){
dates.push(i)
}
for(i=0;i<this.Days.indexOf(startDay);i++){
dates.unshift(' ')
}
while(true){
letrow=''
for(i=0;i<7&&dates.length!==0;i++){
row+=dates.shift()
while(row.length%4!==0){
row+=' '
}
}
output(row)
if(dates.length===0)break
}
}
parseDate(date){
constdateAr=[]
letblock=''
leti
for(i=0;i<date.length;i++){
if(date[i]==='/'){
dateAr.push(parseInt(block))
block=''
continue
}
block+=date[i]
}
dateAr.push(parseInt(block))
if(dateAr.length!==2)thrownewError('Improper string encoding')
constdateOb={month: dateAr[0],year: dateAr[1]}
returndateOb
}
isGreater(startDate,endDate){
if(startDate.year>endDate.year){
returntrue
}elseif(startDate.year<endDate.year){
returnfalse
}elseif(startDate.month>endDate.month){
returntrue
}elseif(startDate.month<endDate.month){
returnfalse
}
returntrue
}
getDayDiff(startDate,endDate){
if(this.isGreater(startDate,endDate)===null){
return0
}elseif(this.isGreater(startDate,endDate)===true){
constmidDate=startDate
startDate=endDate
endDate=midDate
}
letdiff=0
while(startDate.year!==endDate.year){
diff+=isLeapYear(startDate.year) ? 366 : 365
startDate.year=startDate.year+1
}
while(startDate.month!==endDate.month){
if(startDate.month<endDate.month){
if(isLeapYear(startDate.year))
diff+=this.monthDaysLeap[startDate.month]
elsediff+=this.monthDays[startDate.month]
startDate.month=startDate.month+1
}else{
if(isLeapYear(startDate.year))
diff-=this.monthDaysLeap[startDate.month-1]
elsediff-=this.monthDays[startDate.month-1]
startDate.month=startDate.month-1
}
}
returndiff
}
generateMonthCal(date){
constMonth=this.parseDate(date)
letday=''
letdifference=this.getDayDiff(this.epoch,Month)
difference=difference%7
letMonth2=this.parseDate(date)
day=this.isGreater(Month2,this.epoch)
? this.Days[difference]
: this.BDays[difference]
Month2=this.parseDate(date)
if(isLeapYear(Month2.year))
this.printCal(this.monthDaysLeap[Month2.month],day)
elsethis.printCal(this.monthDays[Month2.month],day)
}
}
export{Month}
// const x = new Month()
// x.generateMonthCal('1/2021')