Javascript date object tutorial

Javascript date object:

A JavaScript Date object represents a date. We can get or set year, month and day, hour, minute, second, and millisecond fields of the object by Date methods.

How to create javascript date object:

1. new Date( )
2. new Date(milliseconds)
3. new Date(datestring)
4. new Date(year,month,date[,hour,minute,second,millisecond ])

Example:

<html>
<body>
Current Time: <span id="timeId"></span>
<script>
var date=new Date();
var hours=date.getHours();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
document.getElementById('timeId').innerHTML=
               hours+":"+minutes+":"+seconds;
</script>
</body>
</html>

Try it:

JS Bin on jsbin.com
 

JavaScript Date Object Properties:

Property Description
constructor It specifies the function that creates an object’s prototype.
prototype It allows us to add properties and methods to an object

 

JavaScript Date Object Methods:

Method Description
Date() It returns today’s date and time
getDate() It returns the day of the month for the specified date according to local time.
getDay() It returns the day of the week for the specified date according to local time.
getFullYear() It returns the year of the specified date according to local time.
getHours() It returns the hour in the specified date according to local time.
getMilliseconds() It returns the milliseconds in the specified date according to local time.
getMinutes() It returns the minutes in the specified date according to local time.
getMonth() It returns the month in the specified date according to local time.
getSeconds() It returns the seconds in the specified date according to local time.
getTime() It returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
getTimezoneOffset() It returns the time-zone offset in minutes for the current locale.
getUTCDate() It returns the day (date) of the month in the specified date according to universal time.
getUTCDay() It returns the day of the week in the specified date according to universal time.
getUTCFullYear() It returns the year in the specified date according to universal time.
getUTCHours() It returns the hours in the specified date according to universal time.
getUTCMilliseconds() It returns the milliseconds in the specified date according to universal time.
getUTCMinutes() It returns the minutes in the specified date according to universal time.
getUTCMonth() It returns the month in the specified date according to universal time.
getUTCSeconds() It returns the seconds in the specified date according to universal time.
getYear() It returns the year in the specified date according to local time. Use getFullYear instead.
setDate() It sets the day of the month for a specified date according to local time.
setFullYear() It sets the full year for a specified date according to local time.
setHours() It sets the hours for a specified date according to local time.
setMilliseconds() It sets the milliseconds for a specified date according to local time.
setMinutes() It sets the minutes for a specified date according to local time.
setMonth() It sets the month for a specified date according to local time.
setSeconds() It sets the seconds for a specified date according to local time.
setTime() It sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
setUTCDate() It sets the day of the month for a specified date according to universal time.
setUTCFullYear() It sets the full year for a specified date according to universal time.
setUTCHours() It sets the hour for a specified date according to universal time.
setUTCMilliseconds() It sets the milliseconds for a specified date according to universal time.
setUTCMinutes() It sets the minutes for a specified date according to universal time.
setUTCMonth() It sets the month for a specified date according to universal time.
setUTCSeconds() It sets the seconds for a specified date according to universal time.
setYear() It sets the year for a specified date according to local time. Use setFullYear instead.
toDateString() It returns the “date” portion of the Date as a human-readable string.
toGMTString() It converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
toLocaleDateString() It returns the “date” portion of the Date as a string, using the current locale’s conventions.
toLocaleFormat() It converts a date to a string, using a format string.
toLocaleString() It converts a date to a string, using the current locale’s conventions.
toLocaleTimeString() It returns the “time” portion of the Date as a string, using the current locale’s conventions.
toSource() It returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
toString() It returns a string representing the specified Date object.
toTimeString() It returns the “time” portion of the Date as a human-readable string.
toUTCString() It converts a date to a string, using the universal time convention.
valueOf() It returns the primitive value of a Date object.

JavaScript Date Static Methods:

Method Description
Date.parse() Parses a string representation of a date and time and returns the internal millisecond representation of that date.
Date.UTC() It returns the millisecond representation of the specified UTC date and time.