fbpx

Write a JavaScript program to display the current day and time in the following
format.

0
(0)

Write a JavaScript program to display the current day and time in the following format.
Sample Output:Today is: Tuesday.
Current time is: 10 PM: 30:38

<!DOCTYPE html>
<html>
  <head>
    <title>Current Day and Time</title>
  </head>
  <body>
    <h1>Current Day and Time</h1>
    <p id="time"></p>

    <script>
      function displayTime() {
        const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        const currentDate = new Date();
        const day = days[currentDate.getDay()];
        let hours = currentDate.getHours();
        let minutes = currentDate.getMinutes();
        let seconds = currentDate.getSeconds();
        let ampm = 'AM';
        let timeString = `Today is: ${day}.\nCurrent time is: ${hours} ${ampm}: ${minutes}:${seconds}`;

        if (hours >= 12) {
          hours -= 12;
          ampm = 'PM';
        }

        if (hours === 0) {
          hours = 12;
        }

        document.getElementById('time').innerHTML = timeString;
      }

      setInterval(displayTime, 1000);
    </script>
  </body>
</html>

Output:

Current Day and Time

Current Day and Time

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply