Tutorials
JavaScriptIn this JavaScript tutorial, you will learn about date object and methods of date object explained along with syntax and example.
Usage of Date Object:
Date object of Java Script is used to work with date and times.
General syntax for defining Date object in Java Script is as follows:
var variablename=new Date( )
In the above new is a keyword which creates an instance of object and Date() defines variablename as Date Object.
For example:
var exf=new Date( )
In the above example, variable exf is defined as Date object which has current date and time as its initial value.
Some of the methods available with Date object are:
setSeconds()- Sets the seconds for a specified date according to local time.
setMinutes() - Sets the minutes for a specified date according to local time.
setHours() - Sets the hours for a specified date according to local time.
setDate() - Sets the day of the month for a specified date according to local time.
setMonth() - Sets the month for a specified date according to local time.
setYear() - Sets the year (deprecated) for a specified date according to local time.
setFullYear() - Sets the full year for a specified date according to local time.
toString() - Returns a string representing the specified Date object.
getSeconds() - Returns seconds in the specified date according to local time.
getMinutes() - Returns minutes in the specified date according to local time.
getHours() - Returns hour in the specified date according to local time.
getDay() - Returns day of the week for a specified date according to local time
getDate() - Returns day of the month for a specified date according to local time.
getMonth() - Returns month in the specified date according to local time.
getYear() - Returns year (deprecated) in the specified date according to local time.
getFullYear() - Returns year of the specified date according to local time.
Example for usage of Date Object methods mentioned above:
var exf=new Date()
exf.setFullYear(2020,0,20)
As we have seen setFullYear() is used for Setting the full year for a specified date according to local time. In the above example the Date object exf is set to the specific date and year 20th January 2020
Example for using methods of Date Object
|
Output of the above program is:
11/15/2006
In the above example: