Date Objects in JavaScript Properties and Methods with Examples
Date Objects in JavaScript:
date objects is used to work with dates and times. it consists of properties (or constants) and methods. All these properties and methods can be called by directly date object.
The syntax for creating Date objects is:
var dd= new Date()
Where dd represents the variable of type Date. The Date object will automatically hold the current data and time its initial value.
Methods of Date objects:
The important and commonly used methods of Date object are as follows:
Date() Objects:
This method of date objects returns the current date and time of the computer system. The general syntax to use this method is:
Date();
The following statement displays the current date & time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<html> <head> <script type="text/javaScript"> document.write(Date()); </script> </head> <body> </body> </html> |
getDate() objects:
this method of date objects returns the date of the month. The value returned by this function is between 1 and 31. This method is always used in conjunction with a Date object. The general syntax to use this method is:
dateobject.getDate();
the follow statements display the day of current month:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <head> <script type="text/javaScript"> var d=new Date(); document.write(d.getDate()); </script> </head> <body> </body> </html> |
getDay():
this method of date objects returns the day of week. The value returned by this method is between 0 and 6. Value for Sunday is 0, Monday is 1 and so on. This method is always used in conjunction with a date object. The general syntax to use this method is:
dateObject.getDay();
getMonth():
this method of date objects returns the month of a year. The value returned by this function is between 0 and 11. The general syntax to use this method is:
dateobject.getMonth();
getFullYear():
this method of date objects returns a four-digit number that represents a year. The general syntax to use this method is:
dateobject.getFullYear();
getHours():
this method of date objects returns the hour of a time. the general syntax to use this method is:
dateobject.getHours();
getMinutes():
this method of date objects returns the minutes of a time. the general syntax to use this method is:
dateObject.getMinutes();
getSeconds():
this method of date objects returns the seconds of a time. the general syntax to use this method is:
dateobject.getSeconds();
setDate():
this method of date objects is used to set the day of the month. The general syntax to use this method is:
dateobject.setDate(day);
where day represents a numeric value (from 1 to 31) of the month. The following statement set the day of month to 10
var d= new Date();
d.setDate(10);
setHours():
this method is used to set the hour of specified time. the general syntax t use this method is:
dataobject.setHours(hour);
where hour represents a numeric value for hour. Its value may be from 0 to 23. The following statement set the hour of current time to 12:
var d= new Date();
d.setHours(12);
setMinutes():
this method of date objects is used to set the minutes of specified time.
the general syntax to use this method is:
dateobject.setMinutes(min);
where min represents a numeric value for minutes. Its value may be from 0 to 59. The following statement set the minutes of current time to 30:
var d= new Date();
d.setMinutes(30);
SetSeconds():
This method is used to set the seconds of specified time.
The general syntax to use this method is:
Dateobject.setSeconds(sec);
Where sec represents a numeric value for seconds. Its value may be from 0 to 59. The following statement set the seconds of current time to 15:
Var d= new Date();
d.setSeconds(15);
setFullYear():
this setFullYear() method is used to set the full year, indicating year, month and day. The general syntax to use this method is
dateobject.setFullYear(year,month,day);
setMonth():
this setMonth method is used to set the month. The general syntax to use this method is:
dateobject.setMonth(month);
setYear():
the setYear() method is used to set the year. The general syntax to use this method is:
dateobject.setYear(year);
example write JavaScript code t display four images randomly in a table cell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<html> <head> <script type="text/javaScript"> images = new Array(); images[1]= "image/test.jpg"; images[2]= "image/test2.jpg"; images[3]= "image/test3.jpg"; images[4]= "image/test4.jpg"; function abc() { var imagenumber=4; var randomnumber=Math.random(); var rand1=Math.round((imagenumber-1)*randomnumber)+1; var image = images[rand1]; document.getElementById("pic").src=image; } </script> </head> <body> <script type= "text/javascript"> var x = setInterval("abc()",2000); </script> <table border="2"> <tr> <th align= "center">Moving Pictures</th></tr> <tr> <td> <img id="pic" src="" border="0" width="180" height="150"></td> </tr> </table> </body> </html> |
Example write JavaScript code to create a scrolling message in the browser’s status bar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<html> <head> <script type="text/javaScript"> var msg = "programming digest JavaScript"; var delay = 100; var startPos= 120; var pos=0; function StartScrolling() { for(var i =0; i< startPos; i++) msg=" " + msg; scroll(); } function scroll() { if (pos< msg.length) window.status= msg.substring(pos,msg.length); else pos=-1; pos++ setTimeout("scroll()", delay); } </script> </head> <body onload="StartScrolling()"> </body> </html> |
Related Article:
https://programmingdigest.com/string-objects-in-javascript-and-substring-objects-properties-and-methods/