How to Display Current Date and Time in Javascript
In this tutorial, you will learn how to display current date and time in javascript. If you are in web development, then it is pretty much common that here and there you will encounter a situation where you have to display the date and time to your website visitors. A good example can be displaying the date and time when a certain post was published.
The Date
constructor is specifically used for this purpose because it contains a lot of built-in methods that make it easy to get the accurate date and time in terms of minutes, hours, seconds, or even milliseconds.
In the following example, we have one button and upon click, we want to display the current date or time on the screen. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 2 elements in the HTML file and that includes
button
andh1
. - The inner text for the
button
element is“Show”
and upon click of this button, we will display either date or time. - The inner text for the
h1
element is“Result”
and that will be replaced later by either date or time using javascript. We are using thestyle
attribute here just to center align text content. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button>Show</button> <h1 style="text-align: center;">Result</h1> <script src="script.js"></script> </body> </html>
Javascript
- We have selected
button
element andh1
element usingdocument.querySelector()
and stored them inbtnShow
andoutput
variables respectively. - We have attached a
click
event listener to thebutton
element. - In the event handler function, we are using the
Date
constructor to create theDate
object and storing it in thetoday
variable. - We are using
getMonth()
,getFullYear()
andgetDate()
methods to get currentmonth
,year
, anddate
respectively. We are adding 1 to themonth
because by defaultgetMonth()
method returns a value between 0 and 11 where 0 stands for January. - We are formatting the date and storing it in the
current_date
variable. We are displaying that date in theh1
element using theinnerText
property. - We are using
getHours()
,getMinutes()
andgetSeconds()
methods to get currenthours
,minutes
, andseconds
respectively. Each method returns a value and we are passing that value toaddZero()
method. TheaddZero()
method simply prepend zero if the passed value is less than 10 and returns it. - We are formatting the time and storing it in the
current_time
variable. We are displaying that time in theh1
element using theinnerText
property.
let btnShow = document.querySelector('button'); let output = document.querySelector('h1'); btnShow.addEventListener('click', () => { let today = new Date(); let month = today.getMonth() + 1; let year = today.getFullYear(); let date = today.getDate(); let current_date = `${month}/${date}/${year}`; // output.innerText = current_date; let hours = addZero(today.getHours()); let minutes = addZero(today.getMinutes()); let seconds = addZero(today.getSeconds()); let current_time = `${hours}:${minutes}:${seconds}`; output.innerText = current_time; }); function addZero(num){ return num < 10 ? `0${num}`:num; }