Which Are The Get Date Methods Used In JavaScript

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

In Java script there different get date method which can use to display date so today we are going to discuss about get date methods

Which Are The Get Date Methods Used In JavaScript

These methods can be used for getting information from a date object:
 

Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
Date.now() Get the time. ECMAScript 5.


 

The getTime() Method

The getTime() method returns the number of milliseconds since January 1, 1970:
Example(1)

var d1 = new Date();
document.getElementById("demo1").innerHTML = d1.getTime();// get the current time
 

The getFullYear() Method

The getFullYear() method returns the year of a date as a four digit number:
Example(2)

var d2 = new Date();
document.getElementById("demo2").innerHTML = d2.getFullYear();//return current year
 

The getMonth() Method

The getMonth() method returns the month of a date as a number (0-11):
Example(3)

var d3 = new Date();
document.getElementById("demo3").innerHTML = d3.getMonth();//In JavaScript, the first month (January) is month number 0, so December returns month number 11.


You can use an array of names, and getMonth() to return the month as a name:
Example(4)

var d4 = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
document.getElementById("demo4").innerHTML = months[d4.getMonth()];

 

The getDate() Method

The getDate() method returns the day of a date as a number (1-31):

Example(5)

var d5 = new Date();
document.getElementById("demo5").innerHTML = d5.getDate();// returns the day of a date as a number (1-31)
 

The getHours() Method

The getHours() method returns the hours of a date as a number (0-23):
Example(6)

var d6 = new Date();
document.getElementById("demo6").innerHTML = d6.getHours();//returns the hours of a date as a number (0-23)
 

The getMinutes() Method

The getMinutes() method returns the minutes of a date as a number (0-59):
Example(7)

var d7 = new Date();
document.getElementById("demo7").innerHTML = d7.getMinutes();// returns the minutes of a date as a number (0-59)
 

The getSeconds() Method

The getSeconds() method returns the seconds of a date as a number (0-59):

Example(8)
var d8 = new Date();
document.getElementById("demo8").innerHTML = d8.getSeconds();//returns the seconds of a date as a number (0-59)


 

The getMilliseconds() Method

The getMilliseconds() method returns the milliseconds of a date as a number (0-999):

Example(9)
var d9 = new Date();
document.getElementById("demo9").innerHTML = d9.getMilliseconds();// returns the milliseconds of a date as a number (0-999)
 

The getDay() Method

The getDay() method returns the weekday of a date as a number (0-6):

Example(10)
var d10 = new Date();
document.getElementById("demo10").innerHTML = d10.getDay();//returns the weekday of a date as a number 0 to 6


Example(11)

In JavaScript, the first day of the week (0) means "Sunday", even if some countries in the world consider the first day of the week to be "Monday"

You can use an array of names, and getDay() to return the weekday as a name:

var d = new Date();
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
document.getElementById("demo").innerHTML = days[d.getDay()];


Complete code for get date methods in java script
<!DOCTYPE html>
<html>
<head>
    <title>Get Date Methods Used In JavaScript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
   h2{
       color:red;
   }
</style>
<body>
<div class="container">
    <div class="text-center">
        <h1>Get Date Methods Used In JavaScript</h1>
    </div>
    <br>
    <div class="well">
        <h2>The getDate() Method</h2>
        <h3 id="demo1"></h3>
        <h2>The getFullYear() Method</h2>
        <h3 id="demo2"></h3>
        <h2>The getMonth() Method</h2>
        <h3 id="demo3"></h3>
        <h2>The getMonth() Method will return month name</h2>
        <h3 id="demo4"></h3>
        <h2>The getDate() Method</h2>
        <h3 id="demo5"></h3>
        <h2>The getHours() Method</h2>
        <h3 id="demo6"></h3>
        <h2>The getMinutes() Method</h2>
        <h3 id="demo7"></h3>
        <h2>The getSeconds() Method</h2>
        <h3 id="demo8"></h3>
        <h2>The getMilliseconds() Method</h2>
        <h3 id="demo9"></h3>
        <h2>The getDay() Method</h2>
        <h3 id="demo10"></h3>
        <h2>The getDay() Method will return days name</h2>
        <h3 id="demo"></h3>
    </div>
    <br>
</div>
</body>
</html>

<script>
    var d1 = new Date();
    document.getElementById("demo1").innerHTML = d1.getTime();//return time

    var d2 = new Date();
    document.getElementById("demo2").innerHTML = d2.getFullYear();//return year

    var d3 = new Date();

    document.getElementById("demo3").innerHTML = d3.getMonth();//In JavaScript, the first month (January) is month number 0, 
                                        so December returns month number 11.

    var d4 = new Date();
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
                                   "November", "December"];
    document.getElementById("demo4").innerHTML = months[d4.getMonth()];

    var d5 = new Date();
    document.getElementById("demo5").innerHTML = d5.getDate();// returns the day of a date as a number 1 to 31

    var d6 = new Date();
    document.getElementById("demo6").innerHTML = d6.getHours();// returns the hours of a date as a number 0 to 23

    var d7 = new Date();
    document.getElementById("demo7").innerHTML = d7.getMinutes();// returns the minutes of a date as a number 0 to 59

    var d8 = new Date();
    document.getElementById("demo8").innerHTML = d8.getSeconds();//returns the seconds of a date as a number 0 to 59

    var d9 = new Date();
    document.getElementById("demo9").innerHTML = d9.getMilliseconds();// returns the milliseconds of a date as a number 0 to 999

    var d10 = new Date();
    document.getElementById("demo10").innerHTML = d10.getDay();//returns the weekday of a date as a number 0 to 6

    var d = new Date();
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    document.getElementById("demo").innerHTML = days[d.getDay()];
</script>

Related Post