In JavaScript, date objects are created with new Date().
new Date() returns a date object with the current date
and time.
const date = new Date();
| Method | Description |
|---|---|
| getFullYear() | Get year as a four digit number (yyyy) |
| getMonth() | Get month as a number (0-11) |
| getDate() | Get day as a number (1-31) |
| getDay() | Get weekday as a number (0-6) |
| getHours() | Get hour (0-23) |
| getMinutes() | Get minute (0-59) |
| getSeconds() | Get second (0-59) |
| getMilliseconds() | Get millisecond (0-999) |
| getTime() | Get time (milliseconds since January 1, 1970) |
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script>
var now = new Date();
/* ToDateString Method Example */
document.write(now.toDateString() + "<br><br>");
/* GetDate Method */
document.write(now.getDate() + "<br><br>");
/* GetFullYear Method */
document.write(now.getFullYear() + "<br><br>");
/* GetMonth Method */
document.write(now.getMonth() + "<br><br>");
/* GetDay Method */
document.write(now.getDay() + "<br><br>");
/* GetHours Method */
document.write(now.getHours() + "<br><br>");
/* GetMinutes Method */
document.write(now.getMinutes() + "<br><br>");
/* GetSeconds Method */
document.write(now.getSeconds() + "<br><br>");
/* GetMilliseconds Method */
document.write(now.getMilliseconds() + "<br><br>");
/*setDate Method */
now.setDate(20);
/*setFullYear Method */
now.setFullYear(2020);
/*setFullYear Method */
now.setMonth(4);
/*setHours Method */
now.setHours(14);
document.write(now.getDate() + "/" + now.getMonth() + "/" + now.getFullYear());
</script>
</head>
<body>
</body>
</html>