How to Get the Current Date and Time in JavaScript

The JavaScript Date prototype object creates a new method that will return the current date and time. In this snippet, we will explain how you can accomplish your task step by step.

How to Get the Current Date

The first thing is using the Date() function to create an object in JavaScript:

let currentDate = new Date()

Then you should use the following script to get the current date in the “m-d-y” format. You can change the format.

Javascript current date
let currentDate = new Date(); let cDay = currentDate.getDate() let cMonth = currentDate.getMonth() + 1 let cYear = currentDate.getFullYear() console.log(cDay); console.log(cMonth); console.log(cYear);
  • getDate() – Provides the day of the month values 1-31.
  • getMonth() – Provides the current month with 0-11 values (0 for Jan and 11 for Dec). You should add +1 to get result.
  • getFullYear() – Provides the current year.

Here’s the full code:

Javascript current date
let currentDate = new Date(); let cDay = currentDate.getDate(); let cMonth = currentDate.getMonth() + 1; let cYear = currentDate.getFullYear(); console.log("<b>" + cDay + "/" + cMonth + "/" + cYear + "</b>");

How to Get the Current Time

The Date object is used to get the current time in JavaScript.

Here is how you can get the time in “h:i:s” format. You can change the format whatever you wish.

Javascript get current time
let currentDate = new Date(); let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds(); console.log(time);
  • getHours() – Provides the current hour from 0 to 23.
  • getMinutes() – Provides the current minutes from 0 to 59.
  • getSeconds() – Provides the current seconds from 0 to 59.

To combine date and time in a single variable run the following:

Javascript combine date and time
let current = new Date(); let cDate = current.getFullYear() + '-' + (current.getMonth() + 1) + '-' + current.getDate(); let cTime = current.getHours() + ":" + current.getMinutes() + ":" + current.getSeconds(); let dateTime = cDate + ' ' + cTime; console.log(dateTime);

Display the current time in HTML

To display the current time on the HTML page we should use the setInterval javascript built-in function to update time.

<html>
  <head>
    <title>Show current time</title>
  </head>
  <body>
    <div id="currentTime"></div>
    <script>
      const element = document.getElementById('currentTime');
      
      setInterval(function () {
        const currentDate = new Date();
        element.innerText = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
      }, 1000);
    </script>
  </body>
</html>

Date and Time

JavaScript provides a built-in object Date for handling operations related to all the date and time. You can use it for displaying the current date and time, creating a calendar, building a timer, etc.

When a Date object is created, it allows a number of methods to work on it. Most of them will enable you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object.