I have created a digital clock while learning to work with date and time in javascript. Here I wanted to share what I learned and what I used to build this awesome javascript clock.

My Solution

By default, JavaScript will use the browser's / system time zone and display a date as a full text string if u use the easy solution.

  • Every second we are going to update the time in the view. To run something in X interval, I used setInterval() function
  • I used Date() function to take the system time and then set it to the view
var x = setInterval(time, 1000);  // here 1000 == 1 second

function time(){
   document.getElementById("id").innerHTML =  Date();

More Time Formats in Javascript

If you want to custom the format of your date and time, then you should check these below methods that javascript provides:

Method	            Description
getFullYear()	    Get the year as a four digit number (yyyy)
getMonth()	        Get the month as a number (0-11)
getDate()	        Get the day as a number (1-31)
getHours()   	    Get the hour (0-23)
getMinutes()	    Get the minute (0-59)
getSeconds()	    Get the second (0-59)
getMilliseconds()	Get the millisecond (0-999)
getTime()	        Get the time (milliseconds since January 1, 1970)
getDay()	        Get the weekday as a number (0-6)
Date.now()	        Get the time. ECMAScript 5.

For more knowledge you can check : w3schools

Hope this will help you <3