let today =newDate().toLocaleDateString();//or functionfunctiongetDate(){let today =newDate();let dd =String(today.getDate()).padStart(2,'0');let mm =String(today.getMonth()+1).padStart(2,'0');//janvier = 0let yyyy = today.getFullYear();return`${yyyy}-${mm}-${dd}`;//return dd + '/' + mm + '/' + yyyy; // change form if you need}
let mdy =['month','date','year'];let hms =['hour','minute','second'];
mdy =newDate().toLocaleDateString("en-US").split("/");
hms =newDate().toLocaleTimeString("en-US").split(/:| /);console.log(mdy,hms);
var d =newDate();//1628202691394 miliseconds passed since 1970Number(d)Date("2017-06-23");// date declarationDate("2017");// is set to Jan 01Date("2017-06-23T12:00:00-09:45");// date - time YYYY-MM-DDTHH:MM:SSZDate("June 23 2017");// long date formatDate("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)");// time zone
var d=newDate();// generate today's DATEconsole.log(d);var s=newDate("2020-09-15");// generate the specific DATE:spetember 9 2020var y=newDate().getFullYear();// generate the year of specific DATEvar m=newDate().getMonth();// generate the month of specific DATEvar d=newDate().getDay();// generate the day of specific DATEvarD=newDate().getDate();// generate the specific DATE
const today =newDate()const birthday =newDate('December 17, 1995 03:24:00')// DISCOURAGED: may not work in all runtimesconst birthday2 =newDate('1995-12-17T03:24:00')// This is ISO8601-compliant and will work reliablyconst birthday3 =newDate(1995,11,17)// the month is 0-indexedconst birthday4 =newDate(1995,11,17,3,24,0)const birthday5 =newDate(628021800000)// passing epoch timestamp
<!DOCTYPE html><html><head><title>How to get current date inJavaScript?</title></head><body><h1 style="color: green">
GeeksforGeeks
</h1><b>How to get current date inJavaScript?</b><p>CurrentDateis:<span class="output"></span></p><button onclick="getCurrentDate()">Get current Date</button><script type="text/javascript">functiongetCurrentDate(){let date =newDate().toDateString();document.querySelector('.output').textContent= date;}</script></body></html>