//Date, Time, Timestamp
var today = new Date();
var DD = String(today.getDate()).padStart(2, '0');
var MM = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var YYYY = today.getFullYear();
var hh = today.getHours();
var mm = today.getMinutes();
var ss = today.getSeconds();
today = YYYY + MM + DD + hh + mm + ss;
console.log('Date-Time: ', today);
var date = new Date(); //Will use computers date by default.
//parameters will specify date you put to input
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
let mdy = ['month', 'date', 'year'];
let hms = ['hour', 'minute', 'second'];
mdy = new Date().toLocaleDateString("en-US").split("/");
hms = new Date().toLocaleTimeString("en-US").split(/:| /);
console.log(mdy,hms);
var d = new Date();
//1628202691394 miliseconds passed since 1970
Number(d)
Date("2017-06-23"); // date declaration
Date("2017"); // is set to Jan 01
Date("2017-06-23T12:00:00-09:45"); // date - time YYYY-MM-DDTHH:MM:SSZ
Date("June 23 2017"); // long date format
Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); // time zone
// To test a function and get back its return
function printElapsedTime(fTest) {
let nStartTime = Date.now(),
vReturn = fTest(),
nEndTime = Date.now()
console.log(`Elapsed time: ${ String(nEndTime - nStartTime) } milliseconds`)
return vReturn
}
let yourFunctionReturn = printElapsedTime(yourFunction)
var d= new Date(); // generate today's DATE
console.log (d);
var s= new Date ("2020-09-15"); // generate the specific DATE:spetember 9 2020
var y=new Date().getFullYear(); // generate the year of specific DATE
var m=new Date().getMonth(); // generate the month of specific DATE
var d=new Date().getDay(); // generate the day of specific DATE
var D=new Date().getDate(); // generate the specific DATE
var date1 = new Date();
var date2 = new Date();
if (date1.getTime() === date2.getTime()) { // 1605815698393 === 1605815698393
console.log("Dates are equal");
}
else {
console.log("Dates are Not equal");
}
<!DOCTYPE html>
<html>
<head>
<title>How to get current date in JavaScript?</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>How to get current date in JavaScript?</b>
<p> Current Date is: <span class="output"></span></p>
<button onclick="getCurrentDate()">Get current Date</button>
<script type="text/javascript">
function getCurrentDate() {
let date = new Date().toDateString();
document.querySelector('.output').textContent = date;
}
</script>
</body>
</html>