Methods

The Date Methods

It is defined as a collection of methods which are used to perform certain operations when a date object is created.

 

Date objects created with new Date().

getMethods() are used to getting the date information.

setMethods() is used to set up the date values.

 

Method

Description

getFullYear()

Returns the year in four digits.

getMonth()

Returns the month as a number from 0 to 11.

getDate()

Returns the day of the month as a number from 1 to 31.

getDay()

Returns the day of the week as a number from 0 to 6.

getHours()

Returns the hours.

getMinutes()

Returns the minutes.

getSeconds()

Returns the seconds.

getMilliseconds()

Returns the milliseconds.

getTime()

Returns the time.

setFullYear()

Sets the year.

setMonth()

Sets the month as a number.

setDate()

Sets the day of the month as a number.

setHours()

Sets the hours.

setMinutes()

Sets the minutes.

setSeconds()

Sets the seconds.

setMilliseconds()

Sets the milliseconds.

setTime()

Sets the time.

toString()

Converts data object into a string.

 

Current Date

 

Code:

<html>
<body>
<h2>JavaScript Date Method</h2>
<script>
var x = new Date();
var date = x.getDate();
var month = x.getMonth()+1;
var year = x.getFullYear();
document.write("Current Date is "+date +"-"+ month+ "-"+ year );
</script>
</body>
</html>

 

Output:

 

 

 

Current Time

 

Code:

<html>
<body>
<h2>JavaScript Date Method</h2>
<script>
var x = new Date();
var hours = x.getHours();
var minutes = x.getMinutes();
var seconds = x.getSeconds();
document.write("Current Time is "+hours +":"+ minutes+ ":"+ seconds );
</script>
</body>
</html>

 

Output: