How to get current time from browser in JavaScript

Current Time From Browser

The current time is:

We can get the current time with built- in JavaScript object Date

To get the time we have to create the Date object first simply like this

let currentDateTime = new Date();

As we created the date object

we can console.log to get the result

console.log(currentDateTime);

We get the result like this.

Sun Jun 16 2024 10:26:41 GMT+0600 (Bangladesh Standard Time)

we can separate the date in hour minute and seconds

let currentDateTime = new Date();

// Extract hours, minutes, and seconds from the current date and time


let currentHours = currentDateTime.getHours();
let currentMinutes = currentDateTime.getMinutes();
let currentSeconds = currentDateTime.getSeconds();

example code

// Step 1: Create a Date object
let currentDateTime = new Date();

// Step 2: Display the current date and time
console.log(currentDateTime);

// Step 3: Get the current hour, minute, and second
let currentHours = currentDateTime.getHours();
let currentMinutes = currentDateTime.getMinutes();
let currentSeconds = currentDateTime.getSeconds();

// Step 4: Display the hour, minute, and second
console.log('Current Hours: ' + currentHours);
console.log('Current Minutes: ' + currentMinutes);
console.log('Current Seconds: ' + currentSeconds);

A complete example where time gets from browser and update every second



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Real-Time Current Time Display Example</title>
    <script>
        // Function to retrieve and display the current time
        function getCurrentTimeAndDisplay() {
            // Create a new Date object to get the current date and time
            let currentDateTime = new Date();

            // Extract hours, minutes, and seconds from the current date and time
            let currentHours = currentDateTime.getHours();
            let currentMinutes = currentDateTime.getMinutes();
            let currentSeconds = currentDateTime.getSeconds();

            // Format hours, minutes, and seconds to ensure they are always two digits
            currentHours = currentHours < 10 ? '0' + currentHours : currentHours;
            currentMinutes = currentMinutes < 10 ? '0' + currentMinutes : currentMinutes;
            currentSeconds = currentSeconds < 10 ? '0' + currentSeconds : currentSeconds;

            // Construct the time string in HH:MM:SS format
            let formattedTimeString = currentHours + ':' + currentMinutes + ':' + currentSeconds;

            // Display the formatted time string in an HTML element with id "currentTimeDisplay"
            document.getElementById('currentTimeDisplay').textContent = formattedTimeString;
        }

        // Update the displayed time every second (1000 milliseconds)
        setInterval(getCurrentTimeAndDisplay, 1000);
    </script>
</head>
<body>
    <h1>Real-Time Current Time Display</h1>
    <p>The current time is: <span id="currentTimeDisplay"></span></p>
</body>
</html>


    
    
    

You can Copy the example code and practice the code directly here

Related Posts

How to Make Analog Clock with HTML, CSS, and JavaScript
April 25, 2024

Simple Clock 12 3 6 9 The structure and the essentials So we just add this logic in the clock that’s it. We get the time from browser const now = new Date(); const hours = now.getHours(); const minutes = now.getMinutes(); const seconds = now.getSeconds(); Copy Code Then access the clock hand from JavaScript const […]

Maximize web Performance: Overcome JavaScript Threading Limitations with Web Workers
March 26, 2024

Why? Web Workers: A solution for overcoming JavaScript’s single-threaded limitations. Running JavaScript tasks in background threads, separate from the main browser thread which is responsible for rendering the UI and handling user interactions. So Its possible to run heavy task in multiple process and at the same time keep the ui responsive. How? The structure […]