-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (153 loc) · 6.5 KB
/
index.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Conway's Doomsday Algorithm</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h1>John Conway's Doomsday Algorithm</h1>
<p>See <a href="https://en.wikipedia.org/wiki/Doomsday_rule" target="_blank" rel="noopener noreferrer">Wikipedia</a>
to learn about this method to mentally
compute the weekday of any date.</p>
<p>Check out the <a href="https://github.com/Yann-J/doomsday" target="_blank" rel="noopener noreferrer">source code</a>.</p>
<form class="m-2">
<div class="form-row align-items-center">
<input type="text" id="date" data-format="DD-MM-YYYY" data-template="D MMM YYYY" />
<span> </span>
<div class="btn-group" role="group">
<button type="button" id="today" class="btn btn-sm btn-warning" title="Today">Today</button>
<button type="button" id="random" class="btn btn-sm btn-warning" title="Random">Random</button>
</div>
</div>
</form>
<div class="row" id="results"></div>
<iframe width="560" height="315" src="https://www.youtube.com/embed/714LTMNJy5M" title="YouTube video player"
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.js"></script>
<script src="https://vitalets.github.io/combodate/combodate.js"></script>
<script>
// constants
const WEEKDAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
// The century code, mod4
const CENTURY_CODE = [2, 0, 5, 3];
// The doomsday of every month (Jan = 0)
const DOOMSDAYS = [3, 28, 14, 4, 9, 6, 11, 8, 5, 10, 7, 12];
const START_DATE = new Date(-1000, 0, 1);
const END_DATE = moment().add(100, 'year').toDate();
const RESULT_LABELS = [
{ name: 'Century', format: result => result.century },
{ name: 'Century Offset [C]', format: result => result.century_code },
{ name: 'Year in Century [yy]', format: result => result.year_in_century },
{ name: 'Dozens in Century [D = yy / 12]', format: result => result.dozens },
{ name: 'Years in current dozen [R = yy % 12]', format: result => result.remainder },
{ name: 'Leap Years in current dozen [L = R / 4]', format: result => result.leaps },
{ name: 'Doomsday on that year [C + D + R + L % 7]', format: result => `${WEEKDAYS[result.doomsday_code]} (${result.doomsday_code})` },
{ name: 'Is leap year?', format: result => result.is_leap },
{ name: 'Doomsday for that month', format: result => `${result.y}-${result.m + 1}-${result.doomsday_selected_month}` },
{ name: 'Offset from Doomsday on that month', format: result => result.doomsday_offset },
{ name: 'Weekday', format: result => `${WEEKDAYS[result.day_code]} (${result.day_code})` },
];
// Helpers
function mod(a, n) {
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder
return ((a % n) + n) % n;
}
function isLeapYear(year) {
//three conditions to find out the leap year
if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) {
return true;
}
return false;
}
// init date picker
$(function () {
selectedDate = new Date();
if (location.hash) {
selectedDate = moment(location.hash.substr(1)) || selectedDate;
}
$('#date').combodate({
value: selectedDate,
minYear: START_DATE.getFullYear(),
maxYear: END_DATE.getFullYear(),
errorClass: 'invalid',
customClass: '',
smartDays: true,
});
$('#today').click(function () {
$('#date').combodate('setValue', new Date());
});
$('#random').click(function () {
$('#date').combodate('setValue', new Date(START_DATE.getTime() + Math.random() * (END_DATE.getTime() - START_DATE.getTime())));
});
function compute() {
// Get selected date
const date = $('#date').combodate('getValue', null);
// Update URL
location.hash = date.format('YYYY-MM-DD');
// Compute Weekday
const result = computeWeekday(date);
// Refresh display
const container = $('#results');
container.html('');
RESULT_LABELS.forEach(label => {
container.append(`
<div class="col-md-6">
<h3>${label.name}</h3>
</div>
<div class="col-md-6">
<h3 class="results">${result && result.d ? label.format(result) : 'N/A'}</h3>
</div>`);
});
}
// Run on first load
compute();
// Run on selections
$('#date').change(compute);
});
// compute day
function computeWeekday(mydate) {
if (!(mydate && mydate.isValid && mydate.isValid())) {
return {};
}
const d = mydate.date(); //1-31
const m = mydate.month(); //0-11
const y = mydate.year();
const year_in_century = mod(y, 100);
const century = Math.floor(y / 100);
const century_code = CENTURY_CODE[mod(century, 4)];
const dozens = Math.floor(year_in_century / 12);
const remainder = year_in_century % 12;
const leaps = Math.floor(year_in_century % 12 / 4);
const doomsday_code = (century_code + dozens + remainder + leaps) % 7;
const is_leap = isLeapYear(y);
const doomsday_selected_month = DOOMSDAYS[m] + (is_leap && m <= 1 ? 1 : 0);
const doomsday_offset = (d - doomsday_selected_month) % 7; // can be negative!
const day_code = mod(doomsday_offset + doomsday_code, 7);
return {
d,
m,
y,
century,
year_in_century,
century_code,
dozens,
remainder,
leaps,
doomsday_code,
is_leap,
doomsday_selected_month,
doomsday_offset,
day_code,
};
}
</script>
</body>