JavaScript - Get Date Methods

JavascriptWeb DevelopmentObject Oriented Programming

Following are the date methods −

MethodDescription
getFullYear()Returns the year as a four digit number.
getMonth()Returns the month as a number.
getDate()Returns the day as a number.
getHours()Return the hour.
getMinutes()Returns the minutes.
getSeconds()Returns the seconds.
getMilliseconds()Returns the millisecond.
getTime()Returns the time in milliseconds since January 1,1970.
getDay()Returns the weekday as a number.
Date.now()Returns the number of milliseconds since January 1,1970 00:00:00 UTC.

Following is the code implementing some of the date methods in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript Date Methods</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above buttons to get some date Methods
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   let date = new Date();
   document.querySelector(".Btn").addEventListener("click", () => {
      sampleEle.innerHTML += "getFullYear() = " + date.getFullYear() + "<br>";
      sampleEle.innerHTML += "getMinutes() = " + date.getMinutes() + "<br>";
      sampleEle.innerHTML += "Date.now() = " + Date.now() + "<br>";
      sampleEle.innerHTML += "getMonth() = " + date.getMonth() + "<br>";
   });
</script>
</body>
</html>

Output

On clicking the ‘CLICK HERE’ button −

raja
Published on 11-May-2020 14:20:56
Advertisements