Date Object In JavaScript

 
Date is a built-in object of JavaScript used to deal with date in a JavaScript program. A date is composed of day, month, year, hour, minute, seconds. We can access or change any of these attributes using suitable method. These all methods provide great help to access & maintain the date. The different functions of this object are:
 
setDate( ) This function is used to set desire date to date object
getDate( ) This function is used to get date from date object
getDay( ) This function is used to get day of date from date object
getMonth ( ) This function is used to get Month of date from date object
getYear( ) This function is used to get Year of date from date object
SetHour() This function is used to set Hours to a date object
SetMinutes() This function is used to set Minutes to a date object
SetSeconds() This function is used to set Seconds to a date object
getHour() This function is used to get Hours from a date object
getMinutes() This function is used to get minutes from a date object
getseconds() This function is used to get Seconds from a date object
 
 
Date
 
The different date functions are used in the below given program. Like function to set desire date to date object, function get date from date object, function to get day of date from date object, function to get Month of date from date object, function to get Year of date from date object, function to set Hours to a date object , function to set Minutes to a date object, function to set Seconds to a date object, function to get Hours from a date object, function to get minutes from a date object, function to get Seconds from a date object etc.
 
Example:
 
<html>

<head><title> setDate & getDate </title>

</head>

<body>

<script>

var birthday = new Date(1985,10,5);

var day= birthday.getDate();

document.write(“<br> Birth Date is : “+day)

birthday.setDate(25);

day= birthday.getDate();

document.write(“<br> After Setting Birth Date to 25 date is : “+day)

</script>

</body>

</html>

Click here to view the result of this program in browser
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var birthday = new Date(1985,10,5);

var day= birthday.getDay();

var month=birthday.getMonth();

var y = birthday.getYear();

document.write(“<br> Birth Date is : “+day)

document.write(“<br> Birth Month is : “+month)

document.write(“<br> Birth Year is : “+y)

</script>

</body>

</html>

Click here to view the result of this program in browser
 
Example:
 
<html>

<head><title> setHour & getHour </title>

</head>

<body>

<script>

var calltime = new Date(1985,10,25,5,12,30);

var h= calltime.getHours()

var m = calltime.getMinutes()

var s = calltime.getSeconds()

document.write(“<br>Call Time Hour is : “+h)

document.write(“<br>Call Time Minutes are : “+m)

document.write(“<br>Call Time Seconds are : “+s)

calltime.setHours(9)

calltime.setMinutes(29)

calltime.setSeconds(14)

var h= calltime.getHours()

var m = calltime.getMinutes()

var s = calltime.getSeconds()

document.write(“<br>After Setting Call Time Hour is : “+h)

document.write(“<br>After Setting Call Time Minutes are : “+m)

document.write(“<br>After Setting Call Time Seconds are : “+s)

</script>

</body>

</html>

Scroll to Top