articles

Home / DeveloperSection / Articles / JavaScript Date Object

JavaScript Date Object

JavaScript Date Object

Danish Khan 4232 11-Oct-2012
Introduction:

In this article I am going to explain you about the date object which is used in Java Script. It is a Data type built into the Java Script language.

The date object in Java Script is basically used to work with date and time. A Date object is useful when we want to display a date or we have to perform some sort of calculation on timestamp then we need a date object.  The Java Script simply accesses the system clock to get the date.                                               

Date objects are created with the new Date(), once a date object is created then we can operate a number of methods on it they are simply used to get and set the year, month, day, time and so on.

Date Object Methods

 

Date Object Methods

 

 

 

getDate()

 It returns the day of the month.

getDay()

It returns the day of the week.

getFullYear()

It returns the year.

getHours()

It will return the hours.

getMilliseconds()

It will return the milliseconds from 0-999.

getMinutes()

It will return the minutes from 0-59.

getMonth()

It will return the current month from 0-11.

getSeconds()

It returns the seconds from 0-59.

setDate()

It sets the day of the month to the date object.

setFullYear()

It sets the year of a date object.

setMonth()

It set the month of a date object.

setHours()

It set the hours to the date object.

setMinutes

It will set the minutes to the date object.

setSeconds()

It will set the seconds of a date object.

toDateString()

It returns the Date portion of the date as human readable form.

toTimeString()

It returns the time portion of a date as human readable form.

toString()

It returns the string representation of a date object.

valueOf()

It returns the primitive value of a Date object.

Date Static Methods

 

 

Date.parse()

Parses a string representation of a date and time and returns the internal millisecond representation of that date.

Date.UTC()

Returns the millisecond representation of the specified UTC date and time.

Get current Date:
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <script type="text/javascript">
        function getTodayDate() {
            var date = new Date();
            var day = date.getDate();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            document.write("Date is: " + date + "<br />");
            document.write("Day is: " + day + "<br />");
            document.write("Year is: " + year + "<br />");
            document.write("Month is: " + month + "<br />");
        }
    </script>
</head>

<body onload="getTodayDate()">
</body>
</html>

  Output :

JavaScript Date Object

In this example we have added 1 to month because we have to correct the problem of being January 0 and December 11.

Get Current Time:  
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <script type="text/javascript">
        function getTime() {
            var date = new Date();
            var hrs = date.getHours();
            var min = date.getMinutes();
            var sec = date.getSeconds();
            document.write("Hours is: " + hrs + "<br />");
            document.write("Minutes: " + min + "<br />");
            document.write("Sec is: " + sec + "<br />");
        }
    </script>
</head>
<body onload="getTime()">
</body>
</html>
 
 Output:
JavaScript Date Object 
Conclusion:
Through this article we came to know about the date object and how to use it in Java Script.

Updated 25-Nov-2019

Leave Comment

Comments

Liked By