A live and active time and date with vanilla Javascript.
Start by setting variables for the date string:
var today = new Date(); var day = today.getDate(); var month = today.getMonth() + 1;
Month is plus 1 because January is represented as 0, Febuary as 1 etc when in normal systems January is 1, Febuary is 2.
Now the current time function. This sets the HTML element #time to the browser time with toLocaleTimeString. Click the link to see formating options.
function theTime() {
var d = new Date();
document.getElementById("time").innerHTML = d.toLocaleTimeString("en-US");
} Make the days and months value a two digit string. 1 becomes 01, 5 will become 05 etc:
if (day < 10) {
day = appendZero(day);
}
if (month < 10) {
month = appendZero(month);
} The today date string as day/month/year which is then assigned to the #date element.
Then finally the time function being refreshed every seconds thanks to setInterval.
today = day + "/" + month + "/" + today.getFullYear();
document.getElementById("date").innerHTML = today;
var myVar = setInterval(function () {
theTime();
}, 1000); The HTML:
<div class="container">
<div class="row text-center">
<div class="col-12">
<div class="card">
<h1 id="time" class="time"></h1>
<h1 id="date" class="date"></h1>
</div>
</div>
</div>
</div>
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…