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: | ||||||||||||||||||||||
|
||||||||||||||||||||||
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> |