Get Date and Time in Javascript

thumbnail

In this tutorial, you will learn to get date and time in javascript. We will create four variables to show several format date and time in javascript. The code is show as follow.

Javascript to get date and time

var datetime1 = new Date().toLocaleString();
console.log("datetime1: "+datetime1);
"datetime1: 1/11/2020, 11:20:58 AM"

Javascript to get date

var datetime2 = new Date().toLocaleDateString();
console.log("datetime2: "+datetime2);
"datetime2: 1/11/2020"

Javascript to get time

var datetime3 = new Date().toLocaleTimeString();
console.log("datetime3: "+datetime3);
"datetime3: 11:20:58 AM"

Javascript to get time without AM/PM for US English

var datetime4 = new Date().toLocaleTimeString('en-US', { hour12: false, hour: "numeric", minute: "numeric"});
console.log("datetime4: "+datetime4);
"datetime4: 11:20"

Javascript to get time without AM/PM for British English

var datetime5 = new Date().toLocaleTimeString('en-GB', { hour: "numeric", minute: "numeric"});
console.log("datetime4: "+datetime5);
console.log("datetime5: "+datetime5);
Back To Top