javascript local date code example

Example 1: js get local date

var now = new Date();
console.log(now.toLocaleDateString()); // expected output: 2/26/2021 (MM/DD/YYYY)

Example 2: js datetime local

var date = new Date();
var dd = String(date.getDate()).padStart(2, '0');
var mm = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = date.getFullYear();

today = mm + '/' + dd + '/' + yyyy;
var time = date.toLocaleTimeString();

var dateTime = "Date: " + today + " and Time: " + time;

Tags:

Misc Example