① 求一款js做的时间日历控件
这是我找到的一个js做的日历,不知道是不是你要的,里面有包含时间,其实在日历里面加时间也是比较简单的,只要调用DATE的方法就可以了,希望对你有用。下面是代码段,效果图附上。
<html>
<head>
<SCRIPTLANGUAGE="javaScript"TYPE="text/javascript">
//定义月历函数
functioncalendar(){
vartoday=newDate();//创建日期对象
year=today.getYear();//读取年份
thisDay=today.getDate();//读取当前日
//创建每月天数数组
varmonthDays=newArray(31,28,31,30,31,30,31,31,30,31,30,31);
//如果是闰年,2月份的天数为29天
if(((year%4==0)&&(year%100!=0))||(year%400==0))monthDays[1]=29;
daysOfCurrentMonth=monthDays[today.getMonth()];//从每月天数数组中读取当月的天数
firstDay=today;//复制日期对象
firstDay.setDate(1);//设置日期对象firstDay的日为1号
startDay=firstDay.getDay();//确定当月第一天是星期几
//定义周日和月份中文名数组
vardayNames=newArray("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
varmonthNames=newArray("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
//创建日期对象
varnewDate=newDate();
//创建表格
document.write("<TABLEBORDER='0'CELLSPACING='0'CELLPADDING='2'ALIGN='CENTER'BGCOLOR='#0080FF'>")
document.write("<TR><TD><tableborder='0'cellspacing='1'cellpadding='2'bgcolor='#88FF99'>");
document.write("<TR><thcolspan='7'bgcolor='#C8E3FF'>");
//显示当前日期和周日
document.writeln("<FONTSTYLE='font-size:9pt;Color:#FF0000'>"+newDate.getYear()+"年"+monthNames[newDate.getMonth()]+""+newDate.getDate()+"日"+dayNames[newDate.getDay()]+"</FONT>");
//显示月历表头
document.writeln("</TH></TR><TR><THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>日</FONT></TH>");
document.writeln("<thbgcolor='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>一</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>二</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>三</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>四</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>五</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>六</FONT></TH>");
document.writeln("</TR><TR>");
//显示每月前面的"空日"
column=0;
for(i=0;i<startDay;i++){
document.writeln("<TD><FONTSTYLE='font-size:9pt'></FONT></TD>");
column++;
}
//如果是当前日就突出显示(红色),否则正常显示(黑色)
for(i=1;i<=daysOfCurrentMonth;i++){
if(i==thisDay){
document.writeln("</TD><TDALIGN='CENTER'><FONTSTYLE='font-size:9pt;Color:#ff0000'><B>")
}
else{
document.writeln("</TD><TDBGCOLOR='#88FF99'ALIGN='CENTER'><FONTSTYLE='font-size:9pt;font-family:Arial;font-weight:bold;Color:#000000'>");
}
document.writeln(i);
if(i==thisDay)document.writeln("</FONT></TD>")
column++;
if(column==7){
document.writeln("<TR>");
column=0;
}
}
document.writeln("<TR><TDCOLSPAN='7'ALIGN='CENTER'VALIGN='TOP'BGCOLOR='#0080FF'>")
document.writeln("<FORMNAME='time'onSubmit='0'><FONTSTYLE='font-size:9pt;Color:#ffffff'>")
//显示当前时间
document.writeln("当前时间:<INPUTTYPE='Text'NAME='textbox'ALIGN='TOP'></FONT></TD></TR></TABLE>")
document.writeln("</TD></TR></TABLE></FORM>");
}
</SCRIPT>
<SCRIPTLANGUAGE="JavaScript">
//初始化控制变量
vartimerID=null;
vartimerRunning=false;
//定义时间显示函数
functionstoptime(){
if(timerRunning)
clearTimeout(timerID);
timerRunning=false;}
//定义显示时间函数
functionshowtime(){
varnewDate=newDate();
varhours=newDate.getHours();
varminutes=newDate.getMinutes();
varseconds=newDate.getSeconds()
vartimeValue=""+((hours>12)?hours-12:hours)
timeValue+=((minutes<10)?":0":":")+minutes
timeValue+=((seconds<10)?":0":":")+seconds
timeValue+=(hours>=12)?"下午":"上午"
document.time.textbox.value=timeValue;
timerID=setTimeout("showtime()",1000);//设置超时,使时间动态显示
timerRunning=true;}
//显示当前时间
functionstarttime(){
stoptime();
showtime();}
</SCRIPT>
</head>
<BODYonLoad="starttime()"TEXT="#000000"TOPMARGIN="0">
<scriptlanguage="JavaScript"type="text/javascript">
calendar();//显示月历
</script>
</BODY>
</html>
② 用Javascript做一个日历控件!能写备忘
1document.write("<div id=meizzCalendarLayer style='position: absolute; z-index: 9999; width: 144; height: 193; display: none'>");
2document.write("<iframe name=meizzCalendarIframe scrolling=no frameborder=0 width=100% height=100%></iframe></div>");
3function writeIframe(){
4 var strIframe = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><style>"+
5 "*{font-size: 12px; font-family: 宋体}"+
6 ".bg{ color: "+ WebCalendar.lightColor +"; cursor: default; background-color: "+ WebCalendar.darkColor +";}"+
7 "table#tableMain{ width: 142; height: 180;}"+
8 "table#tableWeek td{ color: "+ WebCalendar.lightColor +";}"+
9 "table#tableDay td{ font-weight: bold;}"+
10 "td#meizzYearHead, td#meizzYearMonth{color: "+ WebCalendar.wordColor +"}"+
11 ".out { text-align: center; border-top: 1px solid "+ WebCalendar.DarkBorder +"; border-left: 1px solid "+ WebCalendar.DarkBorder +";"+
12 "border-right: 1px solid "+ WebCalendar.lightColor +"; border-bottom: 1px solid "+ WebCalendar.lightColor +";}"+
13 ".over{ text-align: center; border-top: 1px solid #FFFFFF; border-left: 1px solid #FFFFFF;"+
14 "border-bottom: 1px solid "+ WebCalendar.DarkBorder +"; border-right: 1px solid "+ WebCalendar.DarkBorder +"}"+
15 "input{ border: 1px solid "+ WebCalendar.darkColor +"; padding-top: 1px; height: 18; cursor: hand;"+
16 " color:"+ WebCalendar.wordColor +"; background-color: "+ WebCalendar.btnBgColor +"}"+
17 "</style></head><body onselectstart='return false' style='margin: 0px' oncontextmenu='return false'><form name=meizz>";
18 if (WebCalendar.drag){ strIframe += "<scr"+"ipt language=javascript>"+
19 "var drag=false, cx=0, cy=0, o = parent.WebCalendar.calendar; function document.onmousemove(){"+
20 "if(parent.WebCalendar.drag && drag){if(o.style.left=='')o.style.left=0; if(o.style.top=='')o.style.top=0;"+
21 "o.style.left = parseInt(o.style.left) + window.event.clientX-cx;"+
22 "o.style.top = parseInt(o.style.top) + window.event.clientY-cy;}}"+
23 "function document.onkeydown(){ switch(window.event.keyCode){ case 27 : parent.hiddenCalendar(); break;"+
24 "case 37 : parent.prevM(); break; case 38 : parent.prevY(); break; case 39 : parent.nextM(); break; case 40 : parent.nextY(); break;"+
25 "case 84 : document.forms[0].today.click(); break;} window.event.keyCode = 0; window.event.returnValue= false;}"+
26 "function dragStart(){cx=window.event.clientX; cy=window.event.clientY; drag=true;}</scr"+"ipt>"}
27 strIframe += "<select name=tmpYearSelect onblur='parent.hiddenSelect(this)' style='z-index:1;position:absolute;top:3;left:18;display:none'"+
28 " onchange='parent.WebCalendar.thisYear =this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+
29 "<select name=tmpMonthSelect onblur='parent.hiddenSelect(this)' style='z-index:1; position:absolute;top:3;left:74;display:none'"+
30 " onchange='parent.WebCalendar.thisMonth=this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+
31 "<table id=tableMain class=bg border=0 cellspacing=2 cellpadding=0>"+
32 "<tr><td width=140 height=19 bgcolor='"+ WebCalendar.lightColor +"'>"+
33 " <table width=140 id=tableHead border=0 cellspacing=1 cellpadding=0><tr align=center>"+
34 " <td width=15 height=19 class=bg title='向前翻 1 月
快捷键:←' style='cursor: hand' onclick='parent.prevM()'><b><</b></td>"+
35 " <td width=60 id=meizzYearHead title='点击此处选择年份' onclick='parent.funYearSelect(parseInt(this.innerText, 10))'"+
36 " onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+
37 " onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+
38 " <td width=50 id=meizzYearMonth title='点击此处选择月份' onclick='parent.funMonthSelect(parseInt(this.innerText, 10))'"+
39 " onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+
40 " onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+
41 " <td width=15 class=bg title='向后翻 1 月
快捷键:→' onclick='parent.nextM()' style='cursor: hand'><b>></b></td></tr></table>"+
42 "</td></tr><tr><td height=20><table id=tableWeek border=1 width=140 cellpadding=0 cellspacing=0 ";
43 if(WebCalendar.drag){strIframe += "onmousedown='dragStart()' onmouseup='drag=false' onmouseout='drag=false'";}
44 strIframe += " borderColorLight='"+ WebCalendar.darkColor +"' borderColorDark='"+ WebCalendar.lightColor +"'>"+
45 " <tr align=center><td height=20>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></table>"+
46 "</td></tr><tr><td valign=top width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+
47 " <table id=tableDay height=120 width=140 border=0 cellspacing=1 cellpadding=0>";
48 for(var x=0; x<5; x++){ strIframe += "<tr>";
49 for(var y=0; y<7; y++) strIframe += "<td class=out id='meizzDay"+ (x*7+y) +"'></td>"; strIframe += "</tr>";}
50 strIframe += "<tr>";
51 for(var x=35; x<39; x++) strIframe += "<td class=out id='meizzDay"+ x +"'></td>";
52 strIframe +="<td colspan=3 class=out title='"+ WebCalendar.regInfo +"'><input style=' background-color: "+
53 WebCalendar.btnBgColor +";cursor: hand; padding-top: 4px; width: 100%; height: 100%; border: 0' onfocus='this.blur()'"+
54 " type=button value=' 关闭' onclick='parent.hiddenCalendar()'></td></tr></table>"+
55 "</td></tr><tr><td height=20 width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+
56 " <table border=0 cellpadding=1 cellspacing=0 width=140>"+
57 " <tr><td><input name=prevYear title='向前翻 1 年
快捷键:↑' onclick='parent.prevY()' type=button value='<<'"+
58 " onfocus='this.blur()' style='meizz:expression(this.disabled=parent.WebCalendar.thisYear==1000)'><input"+
59 " onfocus='this.blur()' name=prevMonth title='向前翻 1 月
快捷键:←' onclick='parent.prevM()' type=button value='<'>"+
60 " </td><td align=center><input name=today type=button value='Today' onfocus='this.blur()' style='width: 50' title='当前日期
快捷键:T'"+
61 " onclick=\"parent.returnDate(new Date().getDate() +'/'+ (new Date().getMonth() +1) +'/'+ new Date().getFullYear())\">"+
62 " </td><td align=right><input title='向后翻 1 月
快捷键:→' name=nextMonth onclick='parent.nextM()' type=button value='>'"+
63 " onfocus='this.blur()'><input name=nextYear title='向后翻 1 年
快捷键:↓' onclick='parent.nextY()' type=button value='>>'"+
64 " onfocus='this.blur()' style='meizz:expression(this.disabled=parent.WebCalendar.thisYear==9999)'></td></tr></table>"+
65 "</td></tr><table></form></body></html>";
66 with(WebCalendar.iframe)
67 {
68 document.writeln(strIframe); document.close();
69 for(var i=0; i<39; i++)
70 {
71 WebCalendar.dayObj[i] = eval("meizzDay"+ i);
72 WebCalendar.dayObj[i].onmouseover = dayMouseOver;
73 WebCalendar.dayObj[i].onmouseout = dayMouseOut;
74 WebCalendar.dayObj[i].onclick = returnDate;
75 }
76 }
77}
78function WebCalendar() //初始化日历的设置
79{
80 this.regInfo = "WEB Calendar ver 3.0
关闭的快捷键:[Esc]";
81 this.daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
82 this.day = new Array(39); //定义日历展示用的数组
83 this.dayObj = new Array(39); //定义日期展示控件数组
84 this.dateStyle = null; //保存格式化后日期数组
85 this.objExport = null; //日历回传的显示控件
86 this.eventSrc = null; //日历显示的触发控件
87 this.inputDate = null; //转化外的输入的日期(d/m/yyyy)
88 this.thisYear = new Date().getFullYear(); //定义年的变量的初始值
89 this.thisMonth = new Date().getMonth()+ 1; //定义月的变量的初始值
90 this.thisDay = new Date().getDate(); //定义日的变量的初始值
91 this.today = this.thisDay +"/"+ this.thisMonth +"/"+ this.thisYear; //今天(d/m/yyyy)
92 this.iframe = window.frames("meizzCalendarIframe"); //日历的 iframe 载体
93 this.calendar = getObjectById("meizzCalendarLayer"); //日历的层
94 this.dateReg = ""; //日历格式验证的正则式
95 this.yearFall = 50; //定义年下拉框的年差值
96 this.format = "yyyy-mm-dd"; //回传日期的格式
97 this.timeShow = false; //是否返回时间
98 this.drag = true; //是否允许拖动
99 this.darkColor = "#408080"; //控件的暗色
100 this.lightColor = "#FFFFFF"; //控件的亮色
101 this.btnBgColor = "#ededed"; //控件的按钮背景色
102 this.wordColor = "#000000"; //控件的文字颜色
103 this.wordDark = "#DCDCDC"; //控件的暗文字颜色
104 this.dayBgColor = "#F5F5FA"; //日期数字背景色
105 this.todayColor = "#ff3300"; //今天在日历上的标示背景色
106 this.DarkBorder = "#D4D0C8"; //日期显示的立体表达色
107} var WebCalendar = new WebCalendar();
108function calendar() //主调函数
109{
110 var e = window.event.srcElement;
111 writeIframe();
112 var o = WebCalendar.calendar.style; WebCalendar.eventSrc = e;
113 if (arguments.length == 0) WebCalendar.objExport = e;
114 else WebCalendar.objExport = eval(arguments[0]);
115 WebCalendar.iframe.tableWeek.style.cursor = WebCalendar.drag ? "move" : "default";
116 var t = e.offsetTop, h = e.clientHeight, l = e.offsetLeft, p = e.type;
117 while (e = e.offsetParent){t += e.offsetTop; l += e.offsetLeft;}
118 o.display = ""; WebCalendar.iframe.document.body.focus();
119 var cw = WebCalendar.calendar.clientWidth, ch = WebCalendar.calendar.clientHeight;
120 var dw = document.body.clientWidth, dl = document.body.scrollLeft, dt = document.body.scrollTop;
121
122 if (document.body.clientHeight + dt - t - h >= ch) o.top = (p=="image")? t + h : t + h + 6;
123 else o.top = (t - dt < ch) ? ((p=="image")? t + h : t + h + 6) : t - ch;
124 if (dw + dl - l >= cw) o.left = l; else o.left = (dw >= cw) ? dw - cw + dl : dl;
125 if (!WebCalendar.timeShow) WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
126 else WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
127 try{
128 if (WebCalendar.objExport.value.trim() != ""){
129 WebCalendar.dateStyle = WebCalendar.objExport.value.trim().match(WebCalendar.dateReg);
130 if (WebCalendar.dateStyle == null)
131 {
132 WebCalendar.thisYear = new Date().getFullYear();
133 WebCalendar.thisMonth = new Date().getMonth()+ 1;
134 WebCalendar.thisDay = new Date().getDate();
135 alert("原文本框里的日期有错误!\n可能与你定义的显示时分秒有冲突!");
136 writeCalendar(); return false;
137 }
138 else
139 {
140 WebCalendar.thisYear = parseInt(WebCalendar.dateStyle[1], 10);
141 WebCalendar.thisMonth = parseInt(WebCalendar.dateStyle[3], 10);
142 WebCalendar.thisDay = parseInt(WebCalendar.dateStyle[4], 10);
143 WebCalendar.inputDate = parseInt(WebCalendar.thisDay, 10) +"/"+ parseInt(WebCalendar.thisMonth, 10) +"/"+
144 parseInt(WebCalendar.thisYear, 10); writeCalendar();
145 }
146 } else writeCalendar();
147 } catch(e){writeCalendar();}
148}
149function funMonthSelect() //月份的下拉框
150{
151 var m = isNaN(parseInt(WebCalendar.thisMonth, 10)) ? new Date().getMonth() + 1 : parseInt(WebCalendar.thisMonth);
152 var e = WebCalendar.iframe.document.forms[0].tmpMonthSelect;
153 for (var i=1; i<13; i++) e.options.add(new Option(i +"月", i));
154 e.style.display = ""; e.value = m; e.focus(); window.status = e.style.top;
155}
156function funYearSelect() //年份的下拉框
157{
158 var n = WebCalendar.yearFall;
159 var e = WebCalendar.iframe.document.forms[0].tmpYearSelect;
160 var y = isNaN(parseInt(WebCalendar.thisYear, 10)) ? new Date().getFullYear() : parseInt(WebCalendar.thisYear);
161 y = (y <= 1000)? 1000 : ((y >= 9999)? 9999 : y);
162 var min = (y - n >= 1000) ? y - n : 1000;
163 var max = (y + n <= 9999) ? y + n : 9999;
164 min = (max == 9999) ? max-n*2 : min;
165 max = (min == 1000) ? min+n*2 : max;
166 for (var i=min; i<=max; i++) e.options.add(new Option(i +"年", i));
167 e.style.display = ""; e.value = y; e.focus();
168}
169function prevM() //往前翻月份
170{
171 WebCalendar.thisDay = 1;
172 if (WebCalendar.thisMonth==1)
173 {
174 WebCalendar.thisYear--;
175 WebCalendar.thisMonth=13;
176 }
177 WebCalendar.thisMonth--; writeCalendar();
178}
179function nextM() //往后翻月份
180{
181 WebCalendar.thisDay = 1;
182 if (WebCalendar.thisMonth==12)
183 {
184 WebCalendar.thisYear++;
185 WebCalendar.thisMonth=0;
186 }
187 WebCalendar.thisMonth++; writeCalendar();
188}
189function prevY(){WebCalendar.thisDay = 1; WebCalendar.thisYear--; writeCalendar();}//往前翻 Year
190function nextY(){WebCalendar.thisDay = 1; WebCalendar.thisYear++; writeCalendar();}//往后翻 Year
191function hiddenSelect(e){for(var i=e.options.length; i>-1; i--)e.options.remove(i); e.style.display="none";}
192function getObjectById(id){ if(document.all) return(eval("document.all."+ id)); return(eval(id)); }
193function hiddenCalendar(){getObjectById("meizzCalendarLayer").style.display = "none";};
194function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}//日期自动补零程序
195function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}
196function dayMouseOver()
197{
198 this.className = "over";
199 this.style.backgroundColor = WebCalendar.darkColor;
200 if(WebCalendar.day[this.id.substr(8)].split("/")[1] == WebCalendar.thisMonth)
201 this.style.color = WebCalendar.lightColor;
202}
203function dayMouseOut()
204{
205 this.className = "out"; var d = WebCalendar.day[this.id.substr(8)], a = d.split("/");
206 this.style.removeAttribute('backgroundColor');
207 if(a[1] == WebCalendar.thisMonth && d != WebCalendar.today)
208 {
209 if(WebCalendar.dateStyle && a[0] == parseInt(WebCalendar.dateStyle[4], 10))
210 this.style.color = WebCalendar.lightColor;
211 else
212 this.style.color = WebCalendar.wordColor;
213 }
214}
215function writeCalendar() //对日历显示的数据的处理程序
216{
217 var y = WebCalendar.thisYear;
218 var m = WebCalendar.thisMonth;
219 var d = WebCalendar.thisDay;
220 WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28;
221 if (!(y<=9999 && y >= 1000 && parseInt(m, 10)>0 && parseInt(m, 10)<13 && parseInt(d, 10)>0)){
222 alert("对不起,你输入了错误的日期!");
223 WebCalendar.thisYear = new Date().getFullYear();
224 WebCalendar.thisMonth = new Date().getMonth()+ 1;
225 WebCalendar.thisDay = new Date().getDate(); }
226 y = WebCalendar.thisYear;
227 m = WebCalendar.thisMonth;
228 d = WebCalendar.thisDay;
229 WebCalendar.iframe.meizzYearHead.innerText = y +" 年";
230 WebCalendar.iframe.meizzYearMonth.innerText = parseInt(m, 10) +" 月";
231 WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28; //闰年二月为29天
232 var w = new Date(y, m-1, 1).getDay();
233 var prevDays = m==1 ? WebCalendar.daysMonth[11] : WebCalendar.daysMonth[m-2];
234 for(var i=(w-1); i>=0; i--) //这三个 for 循环为日历赋数据源(数组 WebCalendar.day)格式是 d/m/yyyy
235 {
236 WebCalendar.day[i] = prevDays +"/"+ (parseInt(m, 10)-1) +"/"+ y;
237 if(m==1) WebCalendar.day[i] = prevDays +"/"+ 12 +"/"+ (parseInt(y, 10)-1);
238 prevDays--;
239 }
240 for(var i=1; i<=WebCalendar.daysMonth[m-1]; i++) WebCalendar.day[i+w-1] = i +"/"+ m +"/"+ y;
241 for(var i=1; i<39-w-WebCalendar.daysMonth[m-1]+1; i++)
242 {
243 WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ (parseInt(m, 10)+1) +"/"+ y;
244 if(m==12) WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ 1 +"/"+ (parseInt(y, 10)+1);
245 }
246 for(var i=0; i<39; i++) //这个循环是根据源数组写到日历里显示
247 {
248 var a = WebCalendar.day[i].split("/");
249 WebCalendar.dayObj[i].innerText = a[0];
250 WebCalendar.dayObj[i].title = a[2] +"-"+ appendZero(a[1]) +"-"+ appendZero(a[0]);
251 WebCalendar.dayObj[i].bgColor = WebCalendar.dayBgColor;
252 WebCalendar.dayObj[i].style.color = WebCalendar.wordColor;
253 if ((i<10 && parseInt(WebCalendar.day[i], 10)>20) || (i>27 && parseInt(WebCalendar.day[i], 10)<12))
254 WebCalendar.dayObj[i].style.color = WebCalendar.wordDark;
255 if (WebCalendar.inputDate==WebCalendar.day[i]) //设置输入框里的日期在日历上的颜色
256 {WebCalendar.dayObj[i].bgColor = WebCalendar.darkColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
257 if (WebCalendar.day[i] == WebCalendar.today) //设置今天在日历上反应出来的颜色
258 {WebCalendar.dayObj[i].bgColor = WebCalendar.todayColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
259 }
260}
261function returnDate() //根据日期格式等返回用户选定的日期
262{
263 if(WebCalendar.objExport)
264 {
265 var returnValue;
266 var a = (arguments.length==0) ? WebCalendar.day[this.id.substr(8)].split("/") : arguments[0].split("/");
267 var d = WebCalendar.format.match(/^(\w{4})(-|\/)(\w{1,2})\2(\w{1,2})$/);
268 if(d==null){alert("你设定的日期输出格式不对!\r\n\r\n请重新定义 WebCalendar.format !"); return false;}
269 var flag = d[3].length==2 || d[4].length==2; //判断返回的日期格式是否要补零
270 returnValue = flag ? a[2] +d[2]+ appendZero(a[1]) +d[2]+ appendZero(a[0]) : a[2] +d[2]+ a[1] +d[2]+ a[0];
271 if(WebCalendar.timeShow)
272 {
273 var h = new Date().getHours(), m = new Date().getMinutes(), s = new Date().getSeconds();
274 returnValue += flag ? " "+ appendZero(h) +":"+ appendZero(m) +":"+ appendZero(s) : " "+ h +":"+ m +":"+ s;
275 }
276 WebCalendar.objExport.value = returnValue;
277 hiddenCalendar();
278 }
279}
280function document.onclick()
281{
282 if(WebCalendar.eventSrc != window.event.srcElement) hiddenCalendar();
283}
284
285
286/* jeff add
287 日期输入控件的控制函数
288*/
289function makeDate(Obj)
290{
291 var y=eval("document.all."+Obj+"YearInput").value;
292 var m=eval("document.all."+Obj+"MonthInput").value;
293 var d=eval("document.all."+Obj+"DayInput").value;
294 //使用 datetime 数据类型存储从 1753 年 1 月 1 日至 9999 年 12 月 31 日的日期
295 if(isDate(y,m,d) && y>=1753)
296 {
297 eval("document.all."+Obj).value=y+"-"+m+"-"+d;
298 }
299 else
300 {
这只是一部分 一次没法回答完
③ 日历js怎么写
加入到你的页面中js/Calendar.js这是控件的路径Calendar.js这个东西应该可以找的到吧,onclick="SelectDate(this)"这是调用的方法
④ 如何用JS写一个日历
首先必须解决的问题是表格的行与列问题。列是固定的,七列,因为一周有七天。行需要动态计算,因为,每一个月的第一天是星期几是一个变数,因而第一天在表格中的第几个单元也就跟着变化,同时,每个月的总天数不一致也影响着各个月对表格行数的需要量。
一. 表格的行数问题
1.首先取得处理月的总天数
JS不提供此参数,我们需要计算。考虑到闰年问题会影响二月份的天数,我们先编写一个判断闰年的自编函数:
function is_leap(year) {
return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
}
接着定义一个包含十二个月在内的月份总天数的数组:
m_days=new Array(31,28+is_leap(ynow),31,30,31,31,30,31,30,31,30,31);
m_days这个数组里,二月份的天数已经加入闰年的信息:28+is_leap(ynow)。数组元素从0开始,正好对应于JS提供的Date函数提供的getMonth返回值,即0表示一月,1表示二月,2表示三月,依此类推。
这样,各月总数可以这样取得:m_days[x]。其中,x为0至11的自然数。
2.计算处理月第一天是星期几
可以使用Date函数的getDay取得,返回的值从0到6,0表示星期一,1表示星期二,2表示星期三,其余依此类推。代码如下(假设要处理的时间为2008年3月):
n1str=new Date(2008,3,1);
firstday=n1str.getDay();
有了月总天数和该月第一天是星期几这两个已知条件,就可以解决表格所需行数问题:(当前月天数+第一天是星期几的数值)除以七。表格函数需要整数,因此,我们使用Math.ceil来处理:
tr_str=Math.ceil((m_days[mnow] + firstday)/7);
表格中的tr标签实际上代表表格的行,因此变量tr_str是我们往下写表格的重要依据。
二. 打印日历表格
可以使用两个for语句嵌套起来实现:外层for语句写行,内层for语句写单元格。
for(i=0;i<tr_str;i++) { //外层for语句 - tr标签
document.write("<tr>");
for(k=0;k<7;k++) { //内层for语句 - td标签
idx=i*7+k; //表格单元的自然序号
date_str=idx-firstday+1; //计算日期
//这里是处理有效日期代码
} //内层for语句结束
document.write("</tr>");
} //外层for语句结束
单元格的自然序号是否代表有效日期非常关键,为此必须加入一个过滤机制:仅打印有效的日期。有效的日期大于0小于小于等于处理月的总天数。
⑤ 关于用JAVASCRIPT做日历的问题!
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>日期选择 - BY ziyue</title>
<script language="JavaScript" type="text/JavaScript">
/*=======Calendar.js=======By Jiang Hongbin=======*/
var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("日","一", "二", "三", "四", "五", "六");
var today;
document.writeln("<div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'></div>");
function getDays(month, year)
{
//下面的这段代码是判断当前是否是闰年的
if (1 == month)
return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
else
return daysInMonth[month];
}
function getToday()
{
//得到今天的年,月,日
this.now = new Date();
this.year = this.now.getFullYear();
this.month = this.now.getMonth();
this.day = this.now.getDate();
}
function getStringDay(str)
{
//得到输入框的年,月,日
var str=str.split("-")
this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
this.year = this.now.getFullYear();
this.month = this.now.getMonth();
this.day = this.now.getDate();
}
function newCalendar() {
var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
var day = -1;
var startDay = newCal.getDay();
var daily = 0;
if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
day = today.day;
var tableCal = document.all.calendar;
var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
{
var cell = tableCal.rows[intWeek].cells[intDay];
if ((intDay == startDay) && (0 == daily))
daily = 1;
if(day==daily) //今天,调用今天的Class
{
cell.style.background='#6699CC';
cell.style.color='#FFFFFF';
//cell.style.fontWeight='bold';
}
else if(intDay==6) //周六
cell.style.color='green';
else if (intDay==0) //周日
cell.style.color='red';
if ((daily > 0) && (daily <= intDaysInMonth))
{
cell.innerText = daily;
daily++;
}
else
cell.innerText = "";
}
}
function GetDate(InputBox)
{
var sDate;
//这段代码处理鼠标点击的情况
if (event.srcElement.tagName == "TD")
if (event.srcElement.innerText != "")
{
sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
eval("document.all."+InputBox).value=sDate;
HiddenCalendar();
}
}
function HiddenCalendar()
{
//关闭选择窗口
document.all.Calendar.style.visibility='hidden';
}
function ShowCalendar(InputBox)
{
var x,y,intLoop,intWeeks,intDays;
var DivContent;
var year,month,day;
//var o=document.getElementById(InputBox);
var o=InputBox;
var oid=o.id;
var thisyear; //真正的今年年份
if(!oid)oid=o.name;
thisyear=new getToday();
thisyear=thisyear.year;
today = o.value;
if(isDate(today))
today = new getStringDay(today);
else
today = new getToday();
//显示的位置
x=o.offsetLeft;
y=o.offsetTop;
while(o=o.offsetParent)
{
x+=o.offsetLeft;
y+=o.offsetTop;
}
document.all.Calendar.style.left=x+2;
document.all.Calendar.style.top=y+20;
document.all.Calendar.style.visibility="visible";
//下面开始输出日历表格(border-color:#9DBAF7)
DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
DivContent+="<tr>";
DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
//年
DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
for (intLoop = thisyear - 100; intLoop < (thisyear + 2); intLoop++)
DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
DivContent+="</select>";
//月
DivContent+="<select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
for (intLoop = 0; intLoop < months.length; intLoop++)
DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
DivContent+="</select>";
DivContent+="</td>";
DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA; font-weight:bold; font-family:Wingdings 2,Wingdings,Webdings; font-size:16px; padding-top:2px; color:#4477FF; cursor:hand' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>S</td>";
DivContent+="</tr>";
DivContent+="<tr><td align='center' colspan='2'>";
DivContent+="<table id='calendar' border='0' width='100%'>";
//星期
DivContent+="<tr>";
for (intLoop = 0; intLoop < days.length; intLoop++)
DivContent+="<td align='center' style='font-size:12px'>" + days[intLoop] + "</td>";
DivContent+="</tr>";
//天
for (intWeeks = 0; intWeeks < 6; intWeeks++)
{
DivContent+="<tr>";
for (intDays = 0; intDays < days.length; intDays++)
DivContent+="<td onClick='GetDate(\"" + oid + "\")' style='cursor:hand; border-right:1px solid #BBBBBB; border-bottom:1px solid #BBBBBB; color:#215DC6; font-family:Verdana; font-size:12px' align='center'></td>";
DivContent+="</tr>";
}
DivContent+="</table></td></tr></table>";
document.all.Calendar.innerHTML=DivContent;
newCalendar();
}
function isDate(dateStr)
{
var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) return false;
var month = matchArray[3];
var day = matchArray[5];
var year = matchArray[1];
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
if (month == 2)
{
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) return false;
}
return true;
}
</script>
<style type="text/css">
<!--
td,input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</head>
<body>
<form name="form1" method="post" action="">
<table width="500" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="#FFFFFF">
<td align="right">您的生日:</td>
<td><input name="birthday" type="text" id="birthday" title="点击选择" onClick="javascript:ShowCalendar(this)" size="20">
<input type="button" name="Submit" value="选 择" onClick="javascript:ShowCalendar(form1.birthday)"></td>
<td>BLOG:<a href="http://web-v.com/">http://web-v.com/</a></td>
</tr>
</table>
</form>
</body>
</html>
⑥ JS,Javascript,编程 日历制作思路
去年末我复自己写了个jquery的日历插件制,我当时的思路是这样的:
1,获取每月第一天是星期几 eg:3
2,获取每月多少天 eg:30
然后循环,30天,循环30次,循环每次加1就不用说了 然后把每个值放进表格的td里面
然后获取上个月和下个月有多少天,按照上月自减和下月自加的方式把其他表格内容补齐,这样一个完整的以月为单元的日历就行了
⑦ 有谁给提供一个带周的js日历啊,能够选择一年中的第几周
http://time.rootinfo.com.tw/main.html
这个可以选择第几周的~!
⑧ 想用js做日历怎么弄
1.写一个获取当前时间的脚本
2.写一个获取事件源绝对坐标的脚本
3.写一个日历生成脚本,根据年(闰年)月(大小月)周(周一到周日)生成日历
4.当事件源(一般是文本框)获得焦点的时候,触发获取绝对坐标的事件,然后生成当前月的日历,将此日历的坐标用脚本移动到文本框下方
5.生成日历的时候,给所有日添加返回事件,把当前选中的年月日反馈
6.用脚本把反馈回来的日期信息,写入事件源控件
7.网上日历控件一大堆,最好别自己写,很容易蛋疼的
⑨ js设置日历选择范围,并且开始时间不能大于截止时间
// 初始化时间设置
var DateRange = {
startTime:“2016-02”,
endTime:“2017-07”
};
$(".workload [name = 'startMonth']").datepicker({
"startView":1,
minViewMode:1,
format:"yyyy-mm",
'endDate':DateRange.endTime,
'startDate':DateRange.startTime
}).on('changeDate',function(e){
var startTime = e.date;
$(".workload [name = 'endMonth']").datepicker('setStartDate',startTime);
});
$(".workload [name = 'endMonth']").datepicker({
"startView":1,
minViewMode:1,
format:"yyyy-mm",
'maxDate':DateRange.endTime,
'minDate':DateRange.startTime
}).on('changeDate',function(e){
var endTime = e.date;
$(".workload [name = 'startMonth']").datepicker('setEndDate',endTime);
});
⑩ js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
应为要有交互,选择了Js来实现,也算是
结对编程
的初试吧。
我将显示部分用html
写好,点击的按钮触发事件函数是check();
复制代码
代码如下:
function
onCheck(){
var
Year
=
document.getElementById("year").value;
//获取文本框的“年”
var
theYear
=Year
*
1;
//转换为number类型
//alert(theYear);
//
获取月值
var
month
=
document.getElementById("month");
var
index1=month.selectedIndex;
var
theMonth
=
month.options[index1].value;
//获取月值
var
day
=
document.getElementById("day");
var
index2=day.selectedIndex;
var
theDay
=
day.options[index2].value;
//
输入值判断部分
...
//调用核心函数
days(theYear,theMonth,theDay);
}