我写的一个日期输入JS程序
<SCRIPT LANGUAGE="JavaScript">
/*
此程序由紫之龙[qq10977837]在2003年9月28日改进编写
此功能用法:
将此SCRIPT嵌套加在WEB页开头,然后在需要输入时间的部分,用最下边的范例引用替换掉<input>
函数参数fDrawCal(生成表单名称,4位年数,月份数,显示单元宽度(建议为15), 显示单元高度(建议为15), 内部字符大小(CSS格式),字符样式(CSS格式),显示模式(建议为1));
其中time.dUpdate()表示把选择的年月设置成当天
还有一些具体功能请看公共变量声明
注意事项:
建立对象的名称和其表单名称参数要一致,否则出错
如建立表单名为A,会建立A_temp和A表单。
如选择1980年1月1日
A.value='1980-1-1'
A_temp.value='1980年1月1日'
小生写程序中变量名称和一些地方可能不是很标准,请高手多多见谅
*/
<!-- Begin
var dDate = new Date();
var dCurMonth = dDate.getMonth();
var dCurDayOfMonth = dDate.getDate();
var dCurYear = dDate.getFullYear();
var objPrevElement = new Object();
var dTopBgColor = "#99CCFF" //顶部内容背景
var dBgColor ="#eeeeee" //时间内容背景
var dBgOverColor ="#aaaaaa" //时间内容鼠标OVER状态背景
var dYearBgOverColor ="#88bbee" //年份背景颜色
var dMonthBgOverColor ="#88bbee" //月份份背景颜色
var dSmallYear =1980 //显示年份最小值
var dBigYear =2010 //显示年份最大值
function getIE(e,m)
{
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
if(m=="top"){return t}
if(m=="left"){return l}
}
function fSetSelectedDay(myElement){
if (myElement.id == "calCell") {
if (!isNaN(parseInt(myElement.children["calDateText_"+this.name].innerText))) {
myElement.bgColor = "#c0c0c0";
objPrevElement.bgColor = "";
document.all[this.name].value = document.all["tbSelYear_"+this.name].value+"-"+document.all["tbSelMonth_"+this.name].value+"-"+myElement.children["calDateText_"+this.name].innerText;
document.all[this.name+"_temp"].value = document.all["tbSelYear_"+this.name].value+"年"+document.all["tbSelMonth_"+this.name].value+"月"+myElement.children["calDateText_"+this.name].innerText+"日";
objPrevElement = myElement;
document.all["div_"+this.name].style.visibility='hidden'
}
}
}
function fGetDaysInMonth(iMonth, iYear) {
var dPrevDate = new Date(iYear, iMonth, 0);
return dPrevDate.getDate();
}
function fBuildCal(iYear, iMonth, iDayStyle) {
var aMonth = new Array();
aMonth[0] = new Array(7);
aMonth[1] = new Array(7);
aMonth[2] = new Array(7);
aMonth[3] = new Array(7);
aMonth[4] = new Array(7);
aMonth[5] = new Array(7);
aMonth[6] = new Array(7);
var dCalDate = new Date(iYear, iMonth-1, 1);
var iDayOfFirst = dCalDate.getDay();
var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
var iVarDate = 1;
var i, d, w;
if (iDayStyle == 2) {
aMonth[0][0] = "Sunday";
aMonth[0][1] = "Monday";
aMonth[0][2] = "Tuesday";
aMonth[0][3] = "Wednesday";
aMonth[0][4] = "Thursday";
aMonth[0][5] = "Friday";
aMonth[0][6] = "Saturday";
} else if (iDayStyle == 1) {
aMonth[0][0] = "日";
aMonth[0][1] = "一";
aMonth[0][2] = "二";
aMonth[0][3] = "三";
aMonth[0][4] = "四";
aMonth[0][5] = "五";
aMonth[0][6] = "六";
} else {
aMonth[0][0] = "Su";
aMonth[0][1] = "Mo";
aMonth[0][2] = "Tu";
aMonth[0][3] = "We";
aMonth[0][4] = "Th";
aMonth[0][5] = "Fr";
aMonth[0][6] = "Sa";
}
for (d = iDayOfFirst; d < 7; d++) {
aMonth[1][d] = iVarDate;
iVarDate++;
}
for (w = 2; w < 7; w++) {
for (d = 0; d < 7; d++) {
if (iVarDate <= iDaysInMonth) {
aMonth[w][d] = iVarDate;
iVarDate++;
}
}
}
return aMonth;
}
function fUpdate()
{
var dCurDate = new Date();
document.all["tbSelMonth_"+this.name].options[dCurDate.getMonth()].selected = true;
for (i = 0; i < document.all["tbSelYear_"+this.name].length; i++)
if (document.all["tbSelYear_"+this.name].options[i].value == dCurDate.getFullYear())
document.all["tbSelYear_"+this.name].options[i].selected = true;
}
function fUpdateCal(iYear, iMonth) {
myMonth = fBuildCal(iYear, iMonth);
objPrevElement.bgColor = "";
//document.all.calSelectedDate.value = "";
for (w = 1; w < 7; w++) {
for (d = 0; d < 7; d++) {
if (!isNaN(myMonth[w][d])) {
document.all["calDateText_"+this.name][((7*w)+d)-7].innerText = myMonth[w][d];
} else {
document.all["calDateText_"+this.name][((7*w)+d)-7].innerText = " ";
}
}
}
}
function fDrawCal(name,iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight, iDayStyle) {
this.name=name
this.year=iYear
this.Month=iMonth
this.SelectDay=fSetSelectedDay
this.Update=fUpdateCal
this.dUpdate=fUpdate
var myMonth;
myMonth = fBuildCal(iYear, iMonth, iDayStyle);
document.write("<input type=\"hidden\" name=\""+this.name+"\" value=\"\">")
document.write("<input type=\"text\" readonly name=\""+this.name+"_temp\" value=\"点此选择时间\" onclick=\"div_"+this.name+".style.pixelTop=getIE(this,'top')+30;div_"+this.name+".style.pixelLeft=getIE(this,'left')+40;div_"+this.name+".style.visibility='visible'\">")
//隐藏层部分
document.write("<div id='div_"+this.name+"' style='Z-INDEX: 3; VISIBILITY: hidden; POSITION: absolute;' >")
document.write("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">")
document.write("<tr>")
document.write("<td bordercolor=\"#000000\">")
document.write("<table border=\"0\">")
document.write("<tr>")
document.write("<td>")
document.write("<select name=\"tbSelYear_"+this.name+"\" onchange='"+this.name+".Update(document.all.tbSelYear_"+this.name+".value, document.all.tbSelMonth_"+this.name+".value)' style=\"text-align:center;border: 1px solid; font-size: 9pt; font-family: \"宋体\"; border-color:#000000 solid\">")
for(var year2=dSmallYear;year2<=dBigYear;year2++)
document.write("<option style='background-color:"+dYearBgOverColor+"' value=\""+year2+"\">"+year2+"</option>")
document.write("</select>")
document.write("<select name=\"tbSelMonth_"+this.name+"\" onchange='"+this.name+".Update(document.all.tbSelYear_"+this.name+".value, document.all.tbSelMonth_"+this.name+".value)' style=\"text-align:center;border: 1px solid; font-size: 9pt; font-family: \"宋体\"; border-color:#000000 solid\">")