-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (134 loc) · 4.07 KB
/
index.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var DoomsdayButton = document.getElementById("DoomsdayButton");
DoomsdayButton.addEventListener("click", function() {
calculateDoomsday();
});
var YearNum = document.getElementById("YearNum");
var MonthNum = document.getElementById("MonthNum");
var DateNum = document.getElementById("DateNum");
var DoomsdayResult = document.getElementById("DoomsdayResult");
//Doomsday 0000 = monday (adjusted for leap year)
var Doomsday0000 = 1;
function calculateDoomsday() {
//Clear output message
DoomsdayResult.innerText = "";
//Get input values
var YearNumValue = YearNum.value;
var MonthNumValue = MonthNum.value;
var DateNumValue = DateNum.value;
//Check valid inputs
var doEnd = false;
if(YearNumValue < 1) {
DoomsdayResult.innerText += "Invalid year.\n";
doEnd = true;
}
if(MonthNumValue < 1 || MonthNumValue > 12) {
DoomsdayResult.innerText += "Invalid month.\n";
doEnd = true;
}
if(DateNumValue < 1 || DateNumValue > 31) {
DoomsdayResult.innerText += "Invalid date.\n";
doEnd = true;
}
//If error, return
if(doEnd) {
return;
}
//Anchor calculation
var YearCentury = Math.floor(YearNumValue/100);
var isLeapCentury = YearCentury % 4 == 0;
//Calculate simple anchor doomsday
var AnchorDoomsday = Math.abs(Doomsday0000 + (YearCentury * 2) + 2);
if(isLeapCentury) { //If anchor century was a leap year, doomsday is 29, so +=1
AnchorDoomsday += 1;
}
AnchorDoomsday = AnchorDoomsday % 7;
//Year calculation
var YearSinceCentury = YearNumValue - (YearCentury*100);
var isLeapYear = YearSinceCentury % 4 == 0;
var YearDoomsday = AnchorDoomsday + YearSinceCentury + Math.floor(YearSinceCentury / 4);
//Month calulation
var MonthDoomsday;
switch(MonthNumValue) {
case "1": //January
if(isLeapYear) {
MonthDoomsday = 32;
} else {
MonthDoomsday = 31;
}
break;
case "2": //February
if(isLeapYear) {
MonthDoomsday = 29;
} else {
MonthDoomsday = 28;
}
break;
case"3": //March
MonthDoomsday = 7;
break;
case "4": //April
MonthDoomsday = 4;
break;
case "5": //May
MonthDoomsday = 9;
break;
case "6": //June
MonthDoomsday = 6;
break;
case "7": //July
MonthDoomsday = 11;
break;
case "8": //August
MonthDoomsday = 8;
break;
case "9": //Septempber
MonthDoomsday = 5;
break;
case "10": //October
MonthDoomsday = 10;
break;
case "11": //November
MonthDoomsday = 7;
break;
case "12": //December
MonthDoomsday = 12;
break;
default:
alert("Incorrect month: " + MonthNumValue);
break;
}
//Date calculation
var DateDifference = DateNumValue - MonthDoomsday;
var DateDoomsday = YearDoomsday + DateDifference;
while(DateDoomsday < 0) {
DateDoomsday += 7;
}
DateDoomsday = DateDoomsday % 7;
DoomsdayResult.innerText = DateNumValue + "/" + MonthNumValue + "/" + YearNumValue + " is a:";
//Output result
switch(DateDoomsday) {
case 0:
DoomsdayResult.innerText += " Sunday";
break;
case 1:
DoomsdayResult.innerText += " Monday";
break;
case 2:
DoomsdayResult.innerText += " Tuesday";
break;
case 3:
DoomsdayResult.innerText += " Wednesday";
break;
case 4:
DoomsdayResult.innerText += " Thursday";
break;
case 5:
DoomsdayResult.innerText += " Friday";
break;
case 6:
DoomsdayResult.innerText += " Saturday";
break;
default:
alert("Incorrect day of week");
}
}