Continued playing and found error in post #5 above where leap-years were not accounted for. This has the modified correction.
Note: This is NOT a replacement for a full-blown Google calendar. Intended as an exercise in simplified calendar creation and date picks.
<!DOCTYPE HTML>
<html lang="en">
<!-- Highly modified from: https://www.sitepoint.com/community/t/my-calendar-help/313555/2 -->
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Calendar Pick</title>
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid black; padding: 0 0.25em; height: 1em; min-width: 1.5em; }
thead > tr > th { background-color: lightgreen; }
tbody > tr > td { text-align: center; }
tbody > tr > td:hover { background-color: lightgreen; }
tfoot > tr { text-align: center; }
caption { font-weight: bold; }
input[type='button'] { font-weight: bold; }
</style>
</head>
<body>
<table id="calendar">
<caption id="anno"> </caption>
<thead>
<tr>
<th id="Sun"> Sun </th>
<th id="Mon"> Mon </th>
<th id="Tue"> Tue </th>
<th id="Wed"> Wed </th>
<th id="Thu"> Thu </th>
<th id="Fri"> Fri </th>
<th id="Sat"> Sat </th>
</tr>
</thead>
<tbody id="calBody">
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>
</tbody>
<tfoot>
<tr>
<td colspan="7">
<input type="button" id="PY" value="<y">
<input type="button" id="PM" value="<m">
<input type="button" id="CMY" value="N">
<input type="button" id="NM" value="m>">
<input type="button" id="NY" value="y>">
</td>
</tr>
</tfoot>
</table>
<p>
<button onclick="doc('debug').innerHTML = ''"> Clear </button>
<pre id="debug"></pre>
<script>
function doc(IDS) { return document.getElementById(IDS); }
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"],
d, m, y;
function getDaysInMonth() { return new Date(d.getFullYear(), m+1, 0).getDate(); }
function get1stDayInMonth(MM, YYYY) { return new Date(YYYY,MM,1).getDay(); }
function Next(amt) {
if (amt == null) { amt = this.getAttribute('data-dir'); }
if ((m + amt) < 0) { y--; }
if ((m + amt) > 11) { y++; }
m = (m + amt) % 12;
if (m < 0) { m = 11; }
doc("anno").innerHTML = month[m]+' '+y;
setCal(m, y);
return false;
}
function NextYr(amt) { y += amt; Next(0); }
function isLeap(year) { return new Date(year, 1, 29).getDate() === 29; }
function showPick() {
var yr = this.getAttribute('data-yr');
var mo = this.getAttribute('data-mo');
var da = this.getAttribute('data-da');
doc('debug').innerHTML += mo+'/'+da+'/'+yr+'\n';
return // alert( mo+'/'+da+'/'+yr );
}
function setCal(MM,YYYY) {
var fd = get1stDayInMonth(MM,YYYY), ld = getDaysInMonth();
if ( (MM == 1) && isLeap(YYYY) ) { ld++; }
var sel = doc('calBody').querySelectorAll('td'), k = 1;
for (var i=0; i<sel.length; i++) { sel[i].removeEventListener('click', showPick); }
for (var i=0; i<sel.length; i++) {
if ( (i >= fd) && (k <= ld) ) {
sel[i].setAttribute('data-yr', YYYY);
sel[i].setAttribute('data-mo', (MM+1));
sel[i].setAttribute('data-da', k);
sel[i].addEventListener('click', showPick);
sel[i].innerHTML = k; k++;
} else { sel[i].innerHTML = ''; }
}
}
function now() {
d = new Date(),
m = d.getMonth(),
y = d.getFullYear();
Next(0);
}
function init() {
now();
doc('PY').addEventListener("click", function(ndx) { return function() { NextYr(ndx); } }(-1) );
doc('PM').addEventListener("click", function(ndx) { return function() { Next(ndx); } }(-1) );
doc('CMY').addEventListener("click", now);
doc('NM').addEventListener("click", function(ndx) { return function() { Next(ndx); } }(1) );
doc('NY').addEventListener("click", function(ndx) { return function() { NextYr(ndx); } }(1) );
} init();
</script>
</body>
</html>