Yet another JavaScript digital clock widget comes with day, date, and AM/PM time format. It displays local time with real-time updating seconds. This clock uses a “Digital-7” font family for clock digits that make it real.
You can integrate this clock to display the current date and time to users. Moreover, the clock can be highly customized with CSS according to your needs.
How to Create JavaScript Digital Clock with Date
1. First, create a div element with a unique id “clockdate” and place two div elements inside it with an id "clock"
and "date"
respectively.
<div id="clockdate"> <div class="clockdate-wrapper"> <div id="clock"></div> <div id="date"></div> </div> </div>
2. After that, define the CSS styles for the digital clock as follows:
@font-face{ font-family: 'Digital-7'; src: url('fonts/digital-7.ttf') format('woff2'), b ,g,mdrx url('digital-7.woff') format('woff'); } .clockdate-wrapper { background: #141E30; /* fallback for old browsers */ background: -webkit-linear-gradient(to right, #243B55, #141E30); /* Chrome 10-25, Safari 5.1-6 */ background: linear-gradient(to right, #243B55, #141E30); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ padding:25px; max-width:350px; width:100%; text-align:center; border-radius:5px; margin:0 auto; } #clock{ font-family: Digital-7, 'sans-serif'; font-size:60px; text-shadow:0px 0px 1px #fff; color:#fff; } #clock span { color: rgba(255, 255, 255, 0.8); text-shadow:0px 0px 1px #333; font-size:50px; position:relative; top:-5px; left:10px; } #date { letter-spacing:3px; font-size:14px; font-family:arial,sans-serif; color:#fff; }
3. Finally, add the JavaScript function (between the <script> tag) for the digital clock before closing the body tag.
function startTime() { var today = new Date(); var hr = today.getHours(); var min = today.getMinutes(); var sec = today.getSeconds(); ap = (hr < 12) ? "<span>AM</span>" : "<span>PM</span>"; hr = (hr == 0) ? 12 : hr; hr = (hr > 12) ? hr - 12 : hr; //Add a zero in front of numbers<10 hr = checkTime(hr); min = checkTime(min); sec = checkTime(sec); document.getElementById("clock").innerHTML = hr + ":" + min + ":" + sec + " " + ap; var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; var curWeekDay = days[today.getDay()]; var curDay = today.getDate(); var curMonth = months[today.getMonth()]; var curYear = today.getFullYear(); var date = curWeekDay+", "+curDay+" "+curMonth+" "+curYear; document.getElementById("date").innerHTML = date; var time = setTimeout(function(){ startTime() }, 500); } function checkTime(i) { if (i < 10) { i = "0" + i; } return i; } startTime();
That’s all! hopefully, you have successfully integrated this JavaScript digital clock widget into your project. If you have any questions or suggestions, let me know by comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.
I dont want date section and when I comment out the date div. your digital clock timer stopped working