Projects

Create Messenger Pop Up ChatBox Type Writer ChatGpt Basic Copy Text Using Tolltip Digital Clock CountDown Timer Create StopWatch FB login from Tab Button Accordion Auto Image Gallery DropDown Menu Modal Box Modal Image Box Scroll Indicator To Do List Autocomplete Remember Search History Toggle Switch Calender Layout Dynamic Calender Alarm Set Password Validation Password Generator Age Calculator Multi Step Form Weather API

⏱️Create A Digital Clock⌚

Note: If you are a beginner, this is something you need to know. Scroll the below section or Click here
  • Date() object in JS
  • toString() method in JS
  • paddStart() method in JS
  • setInteval() method in JS
  • setTimeout() method in JS
  • JS date Gate method
  • For design purposes, you need to add some CSS.

This is our digital Clock.

HTML


<h2>This is our digital Clock.</h2>
<p id="digitalclock"></p>

JavaScript


clockfun();
function clockfun(){
    let date=new Date();

    let hours=date.getHours();

    let mediterian="AM";
    if(hours>12){
        mediterian="PM";
    }

    if(hours>12){
        hours=hours%12;
    }
    else{
        hours=12;
    }
    hours=hours.toString().padStart(2,'0');
    let minites=date.getMinutes().toString().padStart(2,'0');
    let seconds=date.getSeconds().toString().padStart(2,'0');

    document.getElementById("digitalclock").innerHTML=`${hours}:${minites}:${seconds} ${mediterian}`;


}
setInterval(clockfun,1000);

You can use alternative JS


function clockfun() {
    const date = new Date();

    let hours = date.getHours();
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');

    const period = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12 || 12;  // Converts 0 to 12 for 12 AM/PM
    const formattedHours = hours.toString().padStart(2, '0');

    document.getElementById("digitalclock").innerHTML = `${formattedHours}:${minutes}:${seconds} ${period}`;

    // Call clockfun again after 1 second (1000 milliseconds)
    setTimeout(clockfun, 1000);
}

// Initialize the clock immediately on page load
clockfun();

CSS


#digitalclock{
    background-color:#333;
    display: flex;
    align-items: center;
    color: white;
    border-radius: 8px;
    justify-content: center;
    padding: 15px 10px;
    font-size: 60px;
    font-family: monospace;
    font-weight: bold;
}

new Date()

  • Returns a Date object with the current date and time based on the system's clock.
  • This create a date object represent the current date and time.

toString()

  • The toString() method converts any data type into a string. Syntax: varname.toString(),

padStart()

  • It is used to JS to pad the current string with another string untill the resulting string reaches the given length.
    Example: str.padStart(len,char or string);

setInterval()

  • This method in JS repeatedly calls a function or executes a code with a fixed time delay between each call.

setTimeout()

  • This method is used to execute a function or code snippet after a specified delay in milliseconds.

date.get....()

  • getDate(): Returns the day of the month(1-31)
  • getMonth(): Returns the month(0-11)
  • getFullYear(): Returns the year(4 digit year)
  • getDay(): Returns the day of the week(0-6)
  • getHours(): Returns the hours(0-23)
  • getMinutes(): Returns the minites(0-59)
  • getSeconds(): Returns the seconds(0-59)
  • getMilliseconds(): Returns the milliseconds(0-999)
  • getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC