Date Object
About Date object

Date object give the date and time. For this purpose first create date object.
Like
Var  demo=new Date();

Some useful Example for Date object.

Example 1:

This example return the day of the current month.
<html>
<head>
<title>Date object demo</title>
</head>
<body>
<script language="javascript">
var d=new Date();
document.write(d.getDate());
</script>
</body>
</html>

Some Other useful date function.

Note:- These functions pick your System Date/Time.

d.getDate()
This function returns date. like 1,2.....etc.

d.getDay()
This function returns week day.Like 0 for Sunday, 1 for Monday etc.

d.getMonth()
This function returns month. like 0 for January, 1 for february etc.

d.getFullYear()
This function returns full year of the current year. like 2010

d.getHours()
This function returns hour of the current timing.

d.getMinutes()
This function returns minutes of the current timing.

d.getSeconds()
This function returns seconds of the current timing.

d.toString()
This function convert date object to string.

Example 2:-

<html>
<head>
<title>Date object demo</title>
<script language="javascript">
function showdate()
{
var d=new Date();
var month;
if(d.getMonth()==0)
month="January";
if(d.getMonth()==1)
month="February";
if(d.getMonth()==2)
month="March";
if(d.getMonth()==3)
month="April";
if(d.getMonth()==4)
month="May";
if(d.getMonth()==5)
month="June";
if(d.getMonth()==6)
month="July";
if(d.getMonth()==7)
month="August";
if(d.getMonth()==8)
month="September";
if(d.getMonth()==9)
month="October";
if(d.getMonth()==10)
month="November";
if(d.getMonth()==11)
month="December";

var date=d.getDate();
var year=d.getFullYear();

alert(date+"-"+month+"-"+year);
}
</script>
</head>
<body>
<input name="mydate" type="button" value="Show Today Date" onClick="showdate();" >
</body>
</html>

Output:-
show on alert 30-Novermber-2010


Previous
Next