-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (139 loc) · 5.82 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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Anniversaries Calculator</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">
<link rel="icon" href="favicon.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h1>Anniversary Calculator</h1>
<p>This tool will compute various significant anniversaries in some unusual time units, e.g. months or hours...</p>
<p>See the <a href="https://github.com/Yann-J/anniversaries-calculator">source code</a> and README for more details about how this works.</p>
<form class="m-2">
<div class="form-row align-items-center">
<input type="date" id="date" />
<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>
</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://cdn.jsdelivr.net/npm/luxon@3.3.0/build/global/luxon.min.js"></script>
<script>
const DateTime = luxon.DateTime;
const Duration = luxon.Duration;
// constants
const SUPPORTED_UNITS = ['months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
const ONE_YEAR = Duration.fromObject({ years: 1});
const START_DATE = DateTime.fromISO('0000-01-01');
const END_DATE = DateTime.fromISO('2999-12-31');
const MAX_PAST_ANNIVERSARIES = 1;
const MAX_FUTURE_ANNIVERSARIES = 3;
const MAJOR_BIRTHDAY_MULTIPLIER = 5;
const NOW = DateTime.now();
const numberFormatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 });
// Helpers
function formatNumber(n) {
return numberFormatter.format(n);
}
function formatResult(anniversary) {
return `
<div class="col-md-3 results-${anniversary.style}">
<h5>${anniversary.name}</h5>
<ul>
<li>${anniversary.value.toLocaleString(DateTime.DATE_MED)}</li>
<li>${anniversary.style == 'past' ? '' : 'In '}${formatNumber(Math.abs(anniversary.value.diff(NOW).as('days')))} days${anniversary.style == 'past' ? ' ago' : ''}</li>
</ul>
</div>
`;
}
// Core logic
function significantDurationForUnit(unit) {
// return the power of 10 that is closest to 1 year for that unit
const durationInUnit = ONE_YEAR.as(unit);
// Compute corresponding upper power of 10
return 10**(Math.ceil(Math.log10(durationInUnit)));
}
function significantDatesForUnit(date, unit) {
const significantDuration = significantDurationForUnit(unit);
const currentAgeInUnit = NOW.diff(date).as(unit);
const currentAgeBracket = Math.floor(currentAgeInUnit / significantDuration);
let results = [];
for(let i = MAX_PAST_ANNIVERSARIES-1; i>=0; i--) {
let milestone = (currentAgeBracket - i) * significantDuration;
// Only show positive ages
if(milestone >= 0) {
results.push({
name: `${formatNumber(milestone)} ${unit}`,
value: date.plus({[unit]: milestone}),
style: 'past',
});
}
}
for (let i = 1; i <= MAX_FUTURE_ANNIVERSARIES; i++) {
let milestone = (currentAgeBracket + i) * significantDuration;
results.push({
name: `${formatNumber(milestone)} ${unit}`,
value: date.plus({[unit]: milestone}),
style: (currentAgeBracket + i) % 5 ? 'future' : 'major',
});
}
return results;
}
$(function () {
let selectedDate;
if (location.hash) {
selectedDate = DateTime.fromISO(location.hash.substr(1));
}
if(!(selectedDate && selectedDate.isValid)) {
selectedDate = DateTime.now();
}
$('#date').val(selectedDate.toISODate());
$('#today').click(function () {
$('#date').val(DateTime.now().toISODate());
compute(); //for some reason setting the value this way won't trigger a change() event
});
$('#random').click(function () {
$('#date').val(DateTime.fromSeconds(START_DATE.toUnixInteger() + Math.random() * (END_DATE.toUnixInteger() - START_DATE.toUnixInteger())).toISODate());
compute(); //for some reason setting the value this way won't trigger a change() event
});
function compute() {
// Get selected date
const date = DateTime.fromISO($('#date').val());
// Update URL
location.hash = date.toISODate('YYYY-MM-DD');
// Refresh results section
const container = $('#results');
container.html('');
SUPPORTED_UNITS.forEach(unit => {
const anniversaries = significantDatesForUnit(date, unit);
let anniversaryList = '';
anniversaries.forEach(anniversary => {
anniversaryList += formatResult(anniversary);
});
container.append(`
<div class="col-md-12">
<h2 style="display: inline-block">${unit}</h2>
<h5 style="display: inline-block">(current age: ${formatNumber(NOW.diff(date).as(unit))})</h5>
</div>
${anniversaryList}
`);
});
}
// Run on first load
compute();
// Run on selections
$('#date').change(compute);
});
</script>
</body>