JSFiddle - React, Tailwind, and code Playground
by Luke Marlow
HTML
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<span class="game-timer" id="game-timer" data-timestring="2019-07-08 20:55:03"></span>
JavaScript
date_time = function(id) {
let months = date_time_get_months();
let days = date_time_get_days();
let date = new Date($('.'));
let year = date.getFullYear();
let month = date.getMonth();
d = date.getDate();
day = date.getDay();
h = date.getHours();
if (h < 10) {
h = "0" + h;
}
m = date.getMinutes();
if (m < 10) {
m = "0" + m;
}
s = date.getSeconds();
if (s < 10) {
s = "0" + s;
}
a = h > 12 ? 'pm' : 'am';
result = months[month] + ' ' + d + ', ' + days[day] + ' ' + year + ' ' + h + ':' + m + ':' + s + ' ' + a;
document.getElementById(id).innerHTML = result;
setTimeout(function() {
date_time(id);
}, 1000);
};
date_time_get_months = function() {
return {
0: 'January',
1: 'February',
2: 'March',
3: 'April',
4: 'May',
5: 'June',
6: 'July',
7: 'August',
8: 'September',
9: 'October',
10: 'November',
11: 'December'
};
};
date_time_get_days = function() {
return {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday'
};
};
date_time('game-timer');