new Date()
new Date(year, month, day, hour, minute, seconds, milliseconds)
// it takes 7 arguments
new Date(milliseconds)
// we cannot avoid month section
new Date(date string)
let currDate = new Date();
console.log(currDate); // Date Sun Apr 30 2023 23:28:35 GMT+0530 (India Standard Time)
console.log(currDate.toLocaleString()); // 30/04/2023, 23:28:35
console.log(currDate.toString()); // "Sun Apr 30 2023 23:28:35 GMT+0530 (India Standard Time)"
console.log(Date.now()); // 1682878116057
var d = new Date(2023, 4, 30, 10, 33, 0);
console.log(d.toLocaleString()); // 30/05/2023, 10:33:00
// minimum there should be two arguments as 2nd arguemnt (months) is compulary to get correct result.
var d = new Date("October 13, 2014 11:13:00");
console.log(d); // Date Mon Oct 13 2014 11:13:00 GMT+0530 (India Standard Time)
console.log(new Date(0)); Date Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
console.log(new Date(1682879036895)); // Date Sun Apr 30 2023 23:53:56 GMT+0530 (India Standard Time)
console.log(new Date(86400000 * 2).toLocaleString()); // 03/01/1970, 05:30:00
const curDate = new Date();
// how to get individual date
console.log(curDate.toLocaleString()); // 01/05/2023, 09:51:16
console.log(curDate.getFullYear()); // 2023
console.log(curDate.getMonth()); // 4
console.log(curDate.getDate()); // 1
console.log(curDate.getDay()); // 1
console.log(curDate.setFullYear(2024));
// The setFullYear() method can optionally set month and day
console.log(curDate.setFullYear(2022, 10, 5));
let setMonth = curDate.setMonth(10);
console.log(setMonth);
console.log(curDate.setDate(5)); // output in milliseconds
console.log(curDate.toLocaleString());
const curTime = new Date();
// how to get individual time
console.log(curTime.getTime()); // 1682916211507
// The getTime() method returns the number of milliseconds since January 1, 1970
console.log(curTime.getHours()); // 10
// The getHours() method returns the hours of a date as a number (0 - 23)
console.log(curTime.getMinutes());
console.log(curTime.getSeconds());
console.log(curTime.getMilliseconds());
let curTime = new Date();
console.log(curTime.setTime());
console.log(curTime.setHours(5));
console.log(curTime.setMinutes(5));
console.log(curTime.setSeconds(5));
console.log(curTime.setMilliseconds(5));
Click the button to display a date after changing the hours.
Source code ↓
<!-- HTML CODE -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Click the button to display a date after changing the hours.</p>
<button onClick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
// JavaScript
function myFunction() {
let t = new Date();
t.setHours(6);
document.getElementById("demo").innerHTML = t;
}
Source code
<!-- HTML CODE -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p class="wh" id="demo2"></p>
</body>
</html>
// JavaScript
(function(){
setInterval(()=> {
document.getElementById("demo2").innerHTML = new Date().toLocaleString();
}, 1000)
})();