JavaScript Date Object

 


JavaScript Date Output

By default, JavaScript will use the browser's time zone and display a date as a full text string:

Thu Oct 28 2021 21:53:53 GMT+0530 (India Standard Time)


new Date ()

Example 

   
    <script>

        var date = new Date();
        document.write(date);

    </script>

toDateString();
to display present date / month / and Year

        var now = new Date();
        document.write(now.toDateString());


Output with DOM (Document Object Model)


        <body>
        <p id="demo"></p>

        <script>
            const d = new Date();
            // document.getElementById("demo").innerHTML = d;
            document.write(d);
        </script>

    </body>

Get Date Methods

These methods can be used for getting information from a date object:
MethodDescription
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.
   
    <script>

        var date = new Date();
        document.write(date.getFullYear());

    </script>


Set Date Methods

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.

Set Date methods are used for setting a part of a date:

MethodDescription
setDate()Set the day as a number (1-31)
setFullYear()Set the year (optionally month and day)
setHours()Set the hour (0-23)
setMilliseconds()Set the milliseconds (0-999)
setMinutes()Set the minutes (0-59)
setMonth()Set the month (0-11)
setSeconds()Set the seconds (0-59)
setTime()Set the time (milliseconds since January 1, 1970)
   
    <script>

        var date = new Date();
        date.setFullYear(2020);
        document.write(date);

    </script>