function cal(chm,chy,month,year){
month=month + chm;
year=year + chy;
dt=new Date(year, month, 01);// Date object with change in year and month
...........
From this date object we get the year and month. Here to display full month name we use another function show_month() . You can read more displaying month name here .
var str1="<td><a href=# onclick=show_cal(0,-1," + display_month + "," + year +");><< </a> </td><td> <a href=# onclick=show_cal(-1,0," + display_month + "," + year + ");><</a> </td><td> "+ display_month_name +"</td><td> " + year + " </td><td align=right><a href=# onclick=show_cal(1,0," + display_month + "," + year + ");>></a> </td><td> <a href=# onclick=show_cal(0,1," + display_month + "," + year + ");>>></a></td>";
Now we can display each days with a link to post back the date value to the text area. After posting the date value we can call close_call() function to hide the calendar.
str = str + "<td><a href=# onclick=return_value(" + return_month + "," + dy + ","+ year + ");> "+ dy +"</a></td>";
Demo of Calendar with year month link
<html>
<head>
<title>Demo of Date Transferring from Calendar to text field</title>
<style type="text/css">
div {
display: none;
}
table.main {
width: 100%;
}
table.main td {
border-width: 1px 1px 1px 1px;
padding: 1px 1px 1px 1px;
background-color: #ffffcc;
font-family: verdana,arial, helvetica, sans-serif;
font-size: 11px;
}
table.main th {
border-width: 1px 1px 1px 1px;
padding: 0px 0px 0px 0px;
background-color: #ffccff;
}
table.main a{TEXT-DECORATION: none;}
</style>
<script language="javascript" src="date-project-calendar4-demo.js"></script>
<script language="JavaScript">
//// Collect all date variables /////////////
function cal(chm,chy,month,year){
month=month + chm;
year=year + chy;
dt=new Date(year, month, 01);// Date object with change in year and month
var year=dt.getFullYear(); // read the current year
var display_month=dt.getMonth();
var return_month=display_month +1;
var display_month_name=show_month(display_month);
var first_day=dt.getDay(); //, first day of present month
dt.setMonth(month+1,0); // Set to next month and one day backward.
var last_date=dt.getDate(); // Last date of present month
///// End of date variables ////////////
var dy=1; // day variable for adjustment of starting date.
// Top display Links with present Month & year //
var str1="<td><a href=# onclick=show_cal(0,-1," + display_month + "," + year +");><< </a> </td><td> <a href=# onclick=show_cal(-1,0," + display_month + "," + year + ");><</a> </td><td> "+ display_month_name +"</td><td> " + year + " </td><td align=right><a href=# onclick=show_cal(1,0," + display_month + "," + year + ");>></a> </td><td> <a href=# onclick=show_cal(0,1," + display_month + "," + year + ");>>></a></td>";
// End of top display links /////
// Display calendar body ////
var str = '';
str = "<table class='main'><tr> " + str1 + " <td align=right>";
str += " <a href=# onclick="close_cal();";>X</a></td></tr>"; // adding the close button.
str +="<tr><th>Su</th><th>Mon</th><th>Tue</th><th>Wed</th>";
str +="<th>Thu</th><th>Fri</th><th>Sat</th></tr>";
for(i=0;i<=41;i++){
if((i%7)==0){str = str + "</tr><tr>";} // if week is over then start a new line
if((i>= first_day) && (dy<= last_date)){
str = str + "<td><a href=# onclick=return_value(" + return_month + "," + dy + ","+ year + ");> "+ dy +"</a></td>";
dy=dy+1;
}else {str = str + "<td>*</td>";} // Blank dates.
} // end of for loop
str = str + "</tr></table>";
//alert(str);
return str;
}
function show_cal(chm, chy,month,year) {
document.getElementById('sub').innerHTML = cal(chm,chy,month,year);
document.getElementById('sub').style.display = 'inline';
}
//// Stop displaying calendar ///////////
function close_cal() {
document.getElementById('sub').style.display = 'none';
}
//
function start_cal() {
var dt_object=new Date();
var month=dt_object.getMonth();
var year=dt_object.getFullYear();
show_cal(0,0,month,year);
}
function return_value(month,dt,year){
document.getElementById('t1').value=month + '/' + dt + '/' + year ;
close_cal();
}
</script>
</head>
<body >
<form name=f1 action=date-project-calendar4ck.php method=post><input type=hidden id=h1><input type=text name=t1 id=t1>
<img src='../images/calendar.jpg' onClick="start_cal();";>
<div id="sub" style="position: absolute;top: 40px;left: 5px;z-index:1;"></div>
<input type=submit value=Submit>
</form>
</body>
</html>
Demo with two calendar layers & input boxes
Demo with two interlinked calendar to work on range of dates
<style type="text/css">
.....
.....
</style>
Then to integrate JavaScript code add this line within body tag or head tag. It should be above your HTML form.
<script language="javascript" src="date-picker4.js"></script>
<input type=text name=t1 id=t1>
<img src='calendar.jpg' onClick="start_cal();";>
<div id="sub" style="position: absolute;top: 40px;left: 5px;z-index:1;"></div>
If you are using your own textbox with different name then you have to change that name inside JavaScript function kept at date-picker4.js file. Towards the end you will find function return_value() used to return the date value to text box.
function return_value(month,dt,year){
document.getElementById('t1').value=month + '/' + dt + '/' + year ;
close_cal();
}
Change the text box name t1 to your name in above code.