Last active
November 25, 2019 10:52
-
-
Save ayy-em/ae11a5a17e40a416512cf653741485e3 to your computer and use it in GitHub Desktop.
A simple JS code snippet that gets the current time and date and displays it in an HTML textfield while updating every second.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = setInterval(function() { | |
// get the date first | |
var myDate = new Date(); | |
var myDay = myDate.getDay(); | |
// then create a list of days of the week | |
var weekday = ['Sunday', 'Monday', 'Tuesday', | |
'Wednesday', 'Thursday', 'Friday', 'Saturday' | |
]; | |
// then make strings that add "0" in front of 1-digit hrs/mins | |
var dayNum = String(myDate.getDate()).padStart(2, '0'); | |
var monthNum = String(myDate.getMonth() + 1).padStart(2, '0'); | |
var yearFullNum = myDate.getFullYear(); | |
// then the same with time | |
var hours = String(myDate.getHours()).padStart(2, '0'); | |
var minutes = String(myDate.getMinutes()).padStart(2, '0'); | |
var seconds = String(myDate.getSeconds()).padStart(2, '0'); | |
// compile two strings | |
var dateString = weekday[myDay] + ", " + dayNum + "." + monthNum + "." + yearFullNum | |
var timeString = hours + " : " + minutes + " : " + seconds | |
// and put the string into the "titletext" HTML element of your website | |
document.getElementById("titletext").innerHTML = (dateString + "<br>" + timeString); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment