① 求一款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);
}