DW日历js代码

合集下载

js编写当天简单日历效果(实现代码)

js编写当天简单日历效果(实现代码)

下面小编就为大家带来一篇js编写当天简单日历效果【实现代码】。

小编觉得挺不错的,现在分享给大家,也给大家做个参考之前一直很想用javascript写一个日历,但是因为完全没有好的思路,所以迟迟没有尝试。

最近在网上刚好看到用javascript编写的简单日历的例子,代码量虽然不大,但是我觉得很好地阐述了js日历的实现原理。

自己也尝试着做了一下,收获蛮大,掌握了基本的实现原理后,再想增加更多的功能,完全就可以自由发挥了,先在这里分享一下吧,有兴趣的可以试试!一.表格行数问题既然要显示日期表格的话,首先得知道这个表格有多少行多少列,列数是已经确定的,从星期天(日历上第1列是星期天)到星期六一共7列。

要解决行数问题之前,还得先知道这个月的第1天是星期几,因为每个月的1号并不都是从日历上的星期天开始排的,可能1号是星期五,星期六也说不定,所以1号的左边部分,就得用空表格代替了。

那么用多少个空表格代替呢,这里就得用到getDay()方法了,该方法返回数组[0-6]中的一个数字,0代表星期天,1代表星期一,2代表星期二,以此类推。

所以如果一个月的1号是星期五的话,那么刚好左边需要5个空表格代替。

然后,假如一个月有31天,最后求出的表格行数就是:var tr_nums = Math.ceil((5 + 31)/7); 当然,并不是每个月都是31天,所以我们得创建一个包含12个月份的数组,每个元素代表每个月份所包含的天数。

但是2月份比较特殊,闰年的2月份有29天,平年的2月份只有28天。

所以,在创建数组之前,得自己创建一个判断闰年的函数://如果当前年份能被4整除但是不能被100整除或者能被400整除,即可确定为闰年,返回1,否则返回0function isLeap(year) { return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;}然后我们创建一个月份数组:var days_per_month = new Array(31, 28 + isLeap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 这样就能保证无论是平年还是闰年都会取出正确的天数,下面的代码用于获取今天的相关信息:var today = new Date(),       //获取当前日期  y = today.getFullYear(),     //获取日期中的年份  m = today.getMonth(),      //获取日期中的月份(需要注意的是:月份是从0开始计算,获取的值比正常月份的值少1)  d = today.getDate(),       //获取日期中的日(方便在建立日期表格时高亮显示当天)  firstday = new Date(y, m, 1),  //获取当月的第一天  dayOfWeek = firstday.getDay(),  //判断第一天是星期几(返回[0-6]中的一个,0代表星期天,1代表星期一,以此类推)  days_per_month = new Array(31, 28 + isLeap(y), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), //创建月份数组所以最后就可以获取当月所需表格的行数:var str_nums = Math.ceil((dayOfWeek + days_per_month[m]) / 7); //确定日期表格所需的行数二.打印日历表格   表格本身是一个二维数组,所以让for大师出来跑两个循环就搞定啦,代码如下:for (i = 0; i < str_nums; i += 1) {   //第一层for循环创建tr标签  document.write('<tr>');  for (k = 0; k < 7; k++) {      //第二层for循环创建td标签   var idx = 7 * i + k;        //为每个表格创建索引,从0开始   var date = idx - dayOfWeek + 1;  //将当月的1号与星期进行匹配   //do something else  }  document.write('</tr>');  }三.附上完整的js日历代码<script>  //判断当前年份是否是闰年(闰年2月份有29天,平年2月份只有28天)  function isLeap(year) {  return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;  }  var i, k,  today = new Date(),                 //获取当前日期  y = today.getFullYear(),              //获取日期中的年份  m = today.getMonth(),                //获取日期中的月份(需要注意的是:月份是从0开始计算,获取的值比正常月份的值少1)  d = today.getDate(),                //获取日期中的日(方便在建立日期表格时高亮显示当天)  firstday = new Date(y, m, 1),            //获取当月的第一天  dayOfWeek = firstday.getDay(),           //判断第一天是星期几(返回[0-6]中的一个,0代表星期天,1代表星期一,以此类推)   days_per_month = new Array(31, 28 + isLeap(y), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),         //创建月份数组  str_nums = Math.ceil((dayOfWeek + days_per_month[m]) / 7);                        //确定日期表格所需的行数  document.write("<table cellspacing='0'><tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>"); //打印表格第一行(显示星期)  for (i = 0; i < str_nums; i += 1) {         //二维数组创建日期表格  document.write('<tr>');  for (k = 0; k < 7; k++) {   var idx = 7 * i + k;                //为每个表格创建索引,从0开始   var date = idx - dayOfWeek + 1;          //将当月的1号与星期进行匹配   (date <= 0 || date > days_per_month[m]) ? date = ' ': date = idx - dayOfWeek + 1;  //索引小于等于0或者大于月份最大值就用空表格代替   date == d ? document.write('<td class="today">' + date + '</td>') : document.write('<td>' + date + '</td>');  //高亮显示当天  }  document.write('</tr>');  }  document.write('</table>'); </script>css部分大家就自由发挥吧,当前时间是2016年5月2号,效果图如下:以上这篇js编写当天简单日历效果【实现代码】就是小编分享给大家的全部内容了,希望能给大家一个参考。

Javascript实现万年历(日历表)

Javascript实现万年历(日历表)

Javascript实现万年历(⽇历表)<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><style type="text/css">.main {width: 800px;margin: 0 auto;}.top {width: 800px;margin: 0 auto;}#nian {width: 100px;height: 30px;display: block;}#yue {width: 100px;height: 30px;display: block;}.float {display: block;width: 50px;}.center {margin: 0 auto;width: 800px;}#tab tr {height: 150px;}#tab tr td,th {width: 150px;}</style></head><body><div class="main"><div class="top"><select id="nian" class="float" onchange="huan()"></select><span class="float">年</span><select id="yue" class="float" onchange="huan()"></select><span class="float">⽉</span></div></div><div class="center"><table id="tab" border="" cellspacing="" cellpadding=""><tr><th>周⽇</th><th>周⼀</th><th>周⼆</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th></tr></table></div><script type="text/javascript">var tab = document.getElementById("tab");window.onload = function() {var nian = document.getElementById("nian");var yue = document.getElementById("yue");for (var i = 2019; i >= 1970; i--) {var sel = document.createElement("option");sel.value = i;sel.innerText = i;nian.appendChild(sel);}for (var i = 1; i <= 12; i++) {var sel = document.createElement("option");sel.value = i;sel.innerText = i;yue.appendChild(sel);}huan();}function huan() {var week = 0;//周⼏var day = 1;//从第⼀天开始var days = 30;//这个⽉⼀共有⼏天// try{// var ziji=document.getElementsByTagName("td")[0];// ziji.parentElement.parentElement.removeChild(ziji.parentElement); // }catch(e){// //TODO handle the exception// }try{//添加⼀个add1的类,⽅便删除var dataa=document.getElementsByClassName("add1"); for(var i=0;i<dataa.length;){dataa[0].remove();}}catch(e){//TODO handle the exception}var nian = document.getElementById("nian").value;var yue = document.getElementById("yue").value;var date = new Date(nian + "-" + yue + "-1");//计算这个⽉有多少天var data = new Date(nian,yue,0);days=data.getDate();// alert(days)var newtr = document.createElement("tr");newtr.classList.add("add1");for (var i = 0; i < date.getDay(); i++) {if (week == 7) {week = 0;}var newtd = document.createElement("td");newtr.appendChild(newtd);week++;}if (week <= 6) {for (; week <= 6; week++, day++) {var newtd = document.createElement("td");newtd.innerText = day;newtd.value = day;newtr.appendChild(newtd);}}tab.appendChild(newtr);week = 0;newtr = document.createElement("tr");newtr.classList.add("add1");for (; day <= days; day++, week++) {if (week == 7) {week = 0;tab.appendChild(newtr);newtr = document.createElement("tr");newtr.classList.add("add1");}var newtd = document.createElement("td");newtd.innerText = day;newtd.value = day;newtr.appendChild(newtd);}tab.appendChild(newtr);}</script></body></html>。

网页日历制作源代码

网页日历制作源代码

网页日历制作源代码(上课练习)<html><head><script language="JavaScript" >function changColor(index) {document.getElementById(index).bgColor="gray";}function recoverColor(index) {document.getElementById(index).bgColor="";}monthnames = new Array("1月","2月","3月","4月","5月","6月","7月","8月","10月","11月","12月");var linkcount=0;function addlink(month, day, href) {var entry = new Array(3);entry[0] = month;entry[1] = day;entry[2] = href;this[linkcount++] = entry;}Array.prototype.addlink = addlink;linkdays = new Array();monthdays = new Array(12);monthdays[0]=31;monthdays[1]=28;monthdays[2]=31;monthdays[3]=30;monthdays[4]=31;monthdays[5]=30;monthdays[6]=31;monthdays[7]=31;monthdays[8]=30;monthdays[9]=31;monthdays[10]=30;monthdays[11]=31;todayDate=new Date();thisday=todayDate.getDay();thismonth=todayDate.getMonth();thisdate=todayDate.getDate();thisyear=todayDate.getY ear();if (((thisyear % 4 == 0) && !(thisyear % 100 == 0))||(thisyear % 400 == 0)) monthdays[1]++;startspaces = thisdate;if(startspaces > 7) startspaces %= 7;startspaces = thisday - startspaces + 1;if(startspaces < 0) startspaces += 7;document.write("<table border=2 bgcolor=white width=300px");document.write("bordercolor=black><font color=black>");//style.attributedocument.write("<tr><td colspan=7><center>"+ thisyear+"年"+monthnames[thismonth-1]+"</center></font></td></tr>");document.write("<tr>");document.write("<td align=center>日</td>");document.write("<td align=center>一</td>");document.write("<td align=center>二</td>");document.write("<td align=center>三</td>");document.write("<td align=center>四</td>");document.write("<td align=center>五</td>");document.write("<td align=center>六</td>");document.write("</tr>");document.write("<tr>");var i=0;for (s=0;s<startspaces;s++) {document.write("<td>&nbsp</td>");}count=1;while (count <= monthdays[thismonth]) {for (b = startspaces;b<7;b++) {linktrue=false;document.write("<td id='tt" + ++i + "' onclick=\"changColor('tt" + i + "')\" onblur=\"recoverColor('tt" + i + "')\">");/*for (c=0;c<linkdays.length;c++) {if (linkdays[c] != null) {if ((linkdays[c][0]==thismonth + 1) && (linkdays[c][1]==count)) {document.write("<a href=\"" + linkdays[c][2] + "\">");linktrue=true;}}}*/if (count==thisdate) {document.write("<font color='FF0000'><strong>");}if (count <= monthdays[thismonth]) {document.write(count);}else {document.write("&nbsp");}if (count==thisdate) {document.write("</strong></font>");}if (linktrue)document.write("</a>");document.write("</td>");count++;}document.write("</tr>");document.write("<tr>");startspaces=0;}document.write("</table></p>");</script></head><body></body></html>。

Javawebjsp页面实现日历的显示(WdatePicker控件)

Javawebjsp页面实现日历的显示(WdatePicker控件)

Javawebjsp页⾯实现⽇历的显⽰(WdatePicker控件)<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><script language="javascript" type="text/javascript"src="js/My97DatePicker/WdatePicker.js"></script><script type="text/javascript"><% java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");java.util.Date currentTime = new java.util.Date();//得到当前系统时间String str_date = formatter.format(currentTime); //将⽇期时间格式化%></script><div id="div1"></div><script>WdatePicker({eCont:'div1',onpicked:function(dp){alert()}})</script></head><body></body></html>。

js实现的带有农历的日历

js实现的带有农历的日历

js实现的带有农历的日历<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns=""><head></head><body><script>var CalendarData=new Array(20);var madd=new Array(12);var TheDate=new Date();var tgString="甲乙丙丁戊己庚辛壬癸";var dzString="子丑寅卯辰巳午未申酉戌亥";var numString="一二三四五六七八九十";var monString="正二三四五六七八九十冬腊";var weekString="日一二三四五六";var sx="鼠牛虎兔龙蛇马羊猴鸡狗猪";var cYear;var cMonth;var cDay;var cHour;var cDateString;var DateString;var Browser=navigator.appName;var bsDate;var bsDate2;var bsWeek;var bsYear;var bsYear2;function init(){CalendarData[0]=0x41A95; CalendarData[1]=0xD4A; CalendarData[2]=0xDA5; CalendarData[3]=0x20B55; CalendarData[4]=0x56A; CalendarData[5]=0x7155B; CalendarData[6]=0x25D; CalendarData[7]=0x92D; CalendarData[8]=0x5192B; CalendarData[9]=0xA95; CalendarData[10]=0xB4A; CalendarData[11]=0x416AA; CalendarData[12]=0xAD5; CalendarData[13]=0x90AB5; CalendarData[14]=0x4BA; CalendarData[15]=0xA5B; CalendarData[16]=0x60A57; CalendarData[17]=0x52B; CalendarData[18]=0xA93; CalendarData[19]=0x40E95; madd[0]=0;madd[1]=31;madd[2]=59;madd[3]=90;madd[4]=120;madd[5]=151;madd[6]=181;madd[7]=212;madd[8]=243;madd[9]=273;madd[10]=304;madd[11]=334;}function GetBit(m,n){return (m>>n)&1;}function e2c(){var total,m,n,k;var isEnd=false;var tmp=TheDate.getYear();if (tmp<1900) tmp+=1900;total=(tmp-2001)*365+Math.floor((tmp-2001)/4)+madd[TheDate.getMonth()]+TheDate.getDate()-23;if (TheDate.getYear()%4==0&&TheDate.getMonth()>1) total++;for(m=0;;m++){k=(CalendarData[m]<0xfff)?11:12;for(n=k;n>=0;n--){if(total<=29+GetBit(CalendarData[m],n)){isEnd=true;break;}total=total-29-GetBit(CalendarData[m],n);}if(isEnd)break;}cYear=2001 + m;cMonth=k-n+1;cDay=total;if(k==12){if(cMonth==Math.floor(CalendarData[m]/0x10000)+1) cMonth=1-cMonth;if(cMonth>Math.floor(CalendarData[m]/0x10000)+1) cMonth--;}cHour=Math.floor((TheDate.getHours()+3)/2);}function GetcDateString(){ var tmp="";var tmp2="";tmp+=tgString.charAt((cYear-4)%10); //年干tmp+=dzString.charAt((cYear-4)%12); //年支tmp+="年(";tmp+=sx.charAt((cYear-4)%12);tmp+=") ";bsYear=tmp;if(cMonth<1){tmp2+="闰";tmp2+=monString.charAt(-cMonth-1);}elsetmp2+=monString.charAt(cMonth-1);tmp2+="月";tmp2+=(cDay<11)?"初":((cDay<20)?"十":((cDay<30)?"廿":"卅"));if(cDay%10!=0||cDay==10)tmp2+=numString.charAt((cDay-1)%10);bsYear2=tmp2;cDateString=tmp;return tmp;}function GetDateString(){var tmp="";var t1=TheDate.getYear();if (t1<1900)t1+=1900;tmp+=t1+"年"+(TheDate.getMonth()+1)+"月" ;bsDate=tmp;bsDate2=TheDate.getDate();bsWeek= "星期"+weekString.charAt(TheDate.getDay());DateString=tmp;alert(tmp)return tmp;}init();e2c();GetDateString();GetcDateString();function CAL(){document.write("<table border='1'align='center' cellspacing='3' width='90' bordercolor='#9FC6F1' bgcolor='#FFFFFF' height='110' cellpadding='2'");document.write("<tr><td align='center' style='font-size: 9pt; font-family: 宋体'><b><font color=#5478B3>"+bsDate+"</font><br><font face='Arial' size='6' color=#FF8040>"+bsDate2+"</font><br><font color=#5478B3><span style='FONT-SIZE: 10.5pt'>");document.write(bsWeek+"</span><br>"+"<br></b><fon t color=#9B4E00>");document.write(bsYear+"<br>"+bsYear2+"</td></tr></ta ble>");}CAL();</script></body></html>。

日历JS脚本

日历JS脚本
strFrame+='var bDrag; /*標記是否開始拖動*/';
strFrame+='function document.onmousemove() /*在滑鼠移動事件中,如果開始拖動日曆,則移動日曆*/';
strFrame+='{if(bDrag && window.event.button==1)';
strFrame+=' datelayerx=window.event.clientX;';
strFrame+=' datelayery=window.event.clientY;';
strFrame+=' bDrag=true;}';
strFrame+='function DragEnd(){ /*結束日曆拖動*/';
var strFrame; //存放日曆層的HTML代碼
document.writeln('<iframe id=meizzDateLayer frameborder=0 style="position: absolute; width: 144; height: 211; z-index: 9998; display: none"></iframe>');
strFrame+='<td style="font-size:12px;color:#FFFFFF">三</td><td style="font-size:12px;color:#FFFFFF">四</td>';

原生JS实现日历组件的示例代码

原生JS实现日历组件的示例代码

原⽣JS实现⽇历组件的⽰例代码想要实现的效果点击⽇期选择框出现⽇历有个⽇期控制栏帮助选择⽇期,包括年、⽉、⽇的选择和今天⽇历格⼦,初次点击⽇期选择框时显⽰此刻的⽇期,⽇历格⼦的⽇期应该包括这个⽉的所有天数,同时如果当⽉的1号不是周⽇,还应补全从周⽇到1号的天数。

还要在这个⽉最后1号的后⾯补全到周六。

⽇期控制栏和⽇历格⼦的⽇期还有选择框⾥的⽇期的变化要是同步的。

实现思路为了组件的可复⽤性,需要⽤⾯向对象的思想。

每个⽇历组件都是⼀个⽇历对象,主要包括⽇期选择框,⽇期控制显⽰栏,还有⽇历格⼦,为了保持⽇期控制显⽰栏和⽇历格⼦⽇期同步变化,⽇期控制栏和⽇历⾥⾯的每个格⼦都应该包含⼀个Date属性,点击⽇历⾥的格⼦,将格⼦存的Date属性作为函数参数,调⽤函数改变⽇期控制栏显⽰的时间。

同理,⽇期控制栏时间变化时,也将Date属性作为参数调⽤函数,函数重新绘制⽇历格⼦。

上码:function Calendar(parentId) {this.parentElement = document.getElementById(parentId);this.init();}Calendar.prototype = {init: function() {this.contains = document.createElement("div");this.contains.onselectstart = function(){return false}; //让按钮点击时不会出现⽂字被选中的蓝⾊块this.dateInput = document.createElement("input");this.datePicker = document.createElement("div");this.showDateBar = document.createElement("div");this.dateBox = document.createElement("div");this.icon = document.createElement("i");this.contains.className = 'datepicker-container';this.dateInput.className = 'date-input';this.dateInput.readOnly = true;var parent = this;this.dateInput.onclick = function(event){parent.onDateInputClick(event); //点击⽇期选择框时显⽰⽇历格⼦};this.contains.onblur = function(){parent.datePicker.style.display = 'none';}this.datePicker.className = 'date-picker';this.datePicker.style.display = 'none';this.showDateBar.className = 'show-date';this.dateBox.className = 'date-box';this.icon.className = 'date-icon';this.icon.innerHTML = ' '; //iconfont这⾥⽤的阿⾥图标,可以⾃⾏替换this.datePicker.appendChild(this.showDateBar);this.datePicker.appendChild(this.dateBox);this.contains.appendChild(this.dateInput);this.contains.appendChild(this.icon);this.contains.appendChild(this.datePicker);this.parentElement.appendChild(this.contains);},}初始化⽇期控制栏:drawShowDateBar: function(parentElement){var parent = this;var nowDate = new Date();parentElement.date = nowDate;var nowYear = nowDate.getFullYear();var nowMonth = nowDate.getMonth();var nowDay = nowDate.getDate();//showDateBar内容拼接var contentStr ='<div class="year-input"><span>'+nowYear+'年</span><i class="select-year-btn"> </i><ul class="year-select-box" style="display : none">'; for(var i=0;i<150;i++){contentStr+='<li>'+(i+1900)+'年</li>';}contentStr+='</ul></div>'+'<div class="month-input"><i class="prev-month"> </i><select class="months-options">'for(var i=0;i<12;i++){contentStr+='<option>'+(i+1)+'⽉</option>';}contentStr+='</select><i class="next-month"> </i></div>'+'<div class="day-input"><i class="prev-day"> </i><select class="days-options"></select>'+'<i class="next-day"> </i></div>'+'<button class="today-btn">今天</button>'+'<div class="days-title">';var weekday = ['⽇', '⼀', '⼆', '三', '四', '五', '六'];for (var i = 0; i < 7; i++) {contentStr+='<span class="day-title">'+weekday[i]+'</span>';}contentStr+='</div>';parentElement.innerHTML = contentStr;this.changeShowDateBar(nowDate); //插⼊到showTimeBar之后,初始化,传⼊的参数是现在的时间var yearInput = parentElement.firstChild;//年选择框点击显⽰和隐藏选择列表yearInput.onclick = function(){ //target和this的区别 target是触发事件的元素,this是处理事件的元素var ul = stChild;ul.style.display==='none'||ul.style.display==='none'? ul.style.display='inline-block':ul.style.display='none';};//为年选择下拉框绑定点击事件var yearSelectBox = stChild;var yearLi = yearSelectBox.children;for(var i=0;i<yearLi.length;i++){yearLi[i].onclick = function(){parent.showDateBar.date.setFullYear(this.innerText.slice(0,-1));parent.changeShowDateBar(parent.showDateBar.date); //时间改变之后都要重新调⽤,因为不同年,不同⽉,某个⽉的天数不全⼀样 };}//为month的前后按钮添加点击事件var monthInput = yearInput.nextSibling;monthInput.firstChild.onclick = function(){var monthOptions = this.nextSibling;if(monthOptions.selectedIndex>0){parent.showDateBar.date.setMonth(--monthOptions.selectedIndex);}else{monthOptions.selectedIndex = 11;parent.showDateBar.date.setFullYear(parent.showDateBar.date.getFullYear()-1);parent.showDateBar.date.setMonth(11);}parent.changeShowDateBar(parent.showDateBar.date);};stChild.onclick = function(){var monthOptions = this.previousSibling;if(monthOptions.selectedIndex<11){parent.showDateBar.date.setMonth(++monthOptions.selectedIndex);}else{monthOptions.selectedIndex = 0;parent.showDateBar.date.setFullYear(parent.showDateBar.date.getFullYear()+1);parent.showDateBar.date.setMonth(0);}parent.changeShowDateBar(parent.showDateBar.date);}monthInput.children[1].onchange = function(){parent.showDateBar.date.setMonth(this.selectedIndex);parent.changeShowDateBar(parent.showDateBar.date)};//为day的前后按钮添加点击事件var dayInput = monthInput.nextSibling;dayInput.firstChild.onclick = function(){var dayOptions = this.nextSibling;if(dayOptions.selectedIndex>0){parent.showDateBar.date.setDate(dayOptions.selectedIndex--);}else{parent.showDateBar.date.setMonth(parent.showDateBar.date.getMonth()-1);parent.showDateBar.date.setDate(parent.getDaysOfMonth(parent.showDateBar.date));}parent.changeShowDateBar(parent.showDateBar.date);};stChild.onclick = function(){var dayOptions = this.previousSibling;if(dayOptions.selectedIndex < dayOptions.length-1){dayOptions.selectedIndex++;parent.showDateBar.date.setDate(dayOptions.selectedIndex+1);}else{parent.showDateBar.date.setDate(1);parent.showDateBar.date.setMonth(parent.showDateBar.date.getMonth()+1);}parent.changeShowDateBar(parent.showDateBar.date);};dayInput.children[1].onchange = function(){parent.showDateBar.date.setDate(this.selectedIndex+1);parent.changeShowDateBar(parent.showDateBar.date)};//为今天按钮绑定点击事件var todayBtn = dayInput.nextSibling;todayBtn.onclick = function(){parent.drawPicker(new Date());parent.changeShowDateBar(new Date());}},drawShowDateBar函数为⽇期控制栏的年份、⽉份、和天的点击按钮设置了点击事件处理函数。

纯js简单日历实现代码

纯js简单日历实现代码

纯js简单⽇历实现代码复制代码代码如下:<!doctype html><html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">body,ul,li,h2,p{margin:0px;padding:0xp;}#div1{width:200px;height:200px;background:#ccc;}.calendar{width:240px;height:345px;background:#ccc;margin:auto;}.calendar ul{list-style:none;width:232px;height:260px;margin:0px;padding:0px;}.calendar ul li{width:45px;height:65px;background:#333;float:left;display:inline;margin-left:11px;margin-top:10px;border:1px solid #ccc;text-align:center;color:#FFF;}.calendar ul li:hover{border:1px solid #000;}.text{background:#F96;margin:10px;padding-bottom:10px;height:45px;}.text h2{display:inline;}.active{background:#FFF !important;color:#C03 !important;}</style><script type="text/javascript">/* window.onload=function(){var oBtn=document.getElementById('btn1');var oTxt=document.getElementById('txt1');var oDiv=document.getElementById('div1');oBtn.onclick=function(){oDiv.innerHTML=oTxt.value;}}*/window.onload=function(){var aLi= document.getElementsByTagName('li');var Otxt =document.getElementById('tab').getElementsByTagName('div')[0];var aDats =['元旦1⽉1⽇','2⽉14⽇情⼈节','3⽉3⽇全国爱⽿⽇','4⽉1⽇愚⼈节','5⽉1⽇国际劳动节','6⽉1⽇国际⼉童节','7⽉1⽇中国共产党诞⽣⽇','8⽉1⽇中国⼈民解放军建军节','9⽉10⽇中国教师节', '10⽉1⽇中华⼈民共和国国庆节','11⽉9⽇消防宣传⽇','12⽉3⽇世界残疾⼈⽇' ];var i=0;for(i=0;i<aLi.length;i++) //for循环历遍li元素{aLi[i].index=i;aLi[i].onmouseover =function() //添加⿏标指向事件{for(i=0;i<aLi.length;i++) //for循环历遍li元素去掉li样式{aLi[i].className ='';}this.className ='active'; //给当前标签添加active 样式Otxt.innerHTML ='<h2>'+(this.index+1)+'</h2>'+'⽉'+'<p>'+aDats[this.index]+'</p>'; }}};</script></head><body><div id="tab" class="calendar"><ul><li class="active"><h2>1</h2><p>JAN</p></li> <li><h2>2</h2><p>FER</p></li><li><h2>3</h2><p>MAR</p></li><li><h2>4</h2><p>APR</p></li><li><h2>5</h2><p>MAY</p></li><li><h2>6</h2><p>JUM</p></li><li><h2>7</h2><p>JUL</p></li><li><h2>8</h2><p>AUG</p></li><li><h2>9</h2><p>SEP</p></li><li><h2>10</h2><p>OCT</p></li><li><h2>11</h2><p>NOV</p></li><li><h2>12</h2><p>DEC</p></li></ul><div class="text"></div></div></body></html>效果预览:。

dreamweaver显示日期和跳动的时间

dreamweaver显示日期和跳动的时间

显示日期和跳动的时间作者:未知出处:Internet 加入日期:2004.11.02 点击数:1512[大字体中字体小字体]现在很多网页上都有一个区域显示日期、时间和星期,许多网友问这是怎么实现的呢?下面给出一个实现的方法,最终的效果请看下面演示:今天是:2005年9月30现在时间:日星期五源码粘贴框:1.在要显示日期和星期的地方插入如下一段JavaScript:<script language=JavaScript> var version = 1.0;</script><script language=JavaScript1.1> var version = 1.1;</script><script language=JavaScript1.2> var version = 1.2;</script><script language=JavaScript1.3> var version = 1.3;</script><script language="JavaScript"><!--var y=new Date();var gy=y.getYear();var dName=new Array("星期天","星期一","星期二","星期三","星期四","星期五var mName=new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月if (version < 1.3){下面是详细的步骤,我们来具体做一下吧。

原生JavaScript实现日历功能代码实例(无引用Jq)

原生JavaScript实现日历功能代码实例(无引用Jq)

原⽣JavaScript实现⽇历功能代码实例(⽆引⽤Jq)这篇⽂章主要介绍了原⽣JavaScript实现⽇历功能代码实例(⽆引⽤Jq),⽂中通过⽰例代码介绍的⾮常详细,对⼤家的学习或者⼯作具有⼀定的参考学习价值,需要的朋友可以参考下成品显⽰,可左右切换⽉份html 代码<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>移动端⽇历</title><link rel="stylesheet" href="./css/calendar.css" rel="external nofollow" ></head><body><div id="calendarElement"><div class="header"><div class="prev"><i class="previ"></i></div><div class="date"></div><div class="next"><i class="nexti"></i></div></div><div class="content"><div class="week"><div>⽇</div><div>⼀</div><div>⼆</div><div>三</div><div>四</div><div>五</div><div>六</div></div><div class="weekMany clearfloat"></div></div></div><script src="./js/calendar.js"></script></body></html>css代码*{margin: 0;padding: 0;}/*清除浮动代码*/.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}.clearfloat{zoom:1}#calendarElement{margin: 100px auto;width: 80%;box-shadow: 0 0 10px #999999;}#calendarElement>.header{height: 80px;background-color: coral;display: flex;border-bottom: 1px solid #fff;}#calendarElement>.header .prev{width: 20%;position: relative;}height: 20px;display: block;position: absolute;left: 50%;top: 50%;margin-top: -10px;margin-left: -10px;transform: rotate(45deg);border: 2px solid #fff;border-right: none;border-top: none;}#calendarElement>.header .next{position: relative;}#calendarElement>.header .next i{width: 20px;height: 20px;display: block;position: absolute;left: 50%;top: 50%;margin-top: -10px;margin-left: -10px;transform: rotate(45deg);border: 2px solid #fff;border-left: none;border-bottom: none;}#calendarElement>.header .date{width: 60%;font-size: 22px;line-height: 80px;color: #fff;text-align: center;}#calendarElement>.header .next{width: 20%;}#calendarElement>.content >.week{box-sizing: border-box;width: 100%;height: 40px;color: #fff;display: flex;padding: 0 1%;}#calendarElement>.content >.week >div{width: 14%;text-align: center;line-height: 40px;}#calendarElement>.content >.weekMany{padding-top: 5px;padding-bottom: 15px;}#calendarElement>.content >.weekMany>div{float: left;width: 14.28%;height: 40px;text-align: center;line-height: 40px;font-size: 14px;}#calendarElement>.content >.weekMany>.otherMonth{color: #999999}JS代码var currentTime=""; //当前时间年⽉⽇var dom=document.querySelector("#calendarElement"); //承载元素var color="";getCurrentTime();randomColor();function getCurrentTime(){ //获取当前时间var time=new Date();var year=time.getFullYear();var month=time.getMonth()+1;var day=time.getDate();if(month<10){month="0"+month}var data=year+ "-" +month;currentTime=year+ "-" +month+"-"+day;document.querySelector(".date").innerHTML=data;};dom.addEventListener("click",function(e){if(e.target.className=="previ" || e.target.className=="prev"){getMonths("prev")}else if(e.target.className=="nexti" || e.target.className=="next"){getMonths("next")}})function showDay(){var html="";var MonthOne=currentTime;var yearMonth=currentTime.split('-').slice(0,2);yearMonth=yearMonth.join('-');document.querySelector(".date").innerHTML=yearMonth;MonthOne=MonthOne.split('');MonthOne.splice(8,2,"01")MonthOne=MonthOne.join('');var monthLen=getMonthLength(MonthOne); //每⽉有多少天var weekMany=new Date(MonthOne).getDay(); //每⽉⼀号是星期⼏html+=getPrevMonthHtml(weekMany);html+=getNowMonthHtml(monthLen);html+=getNextMonthHtml(weekMany,monthLen);document.querySelector(".weekMany").innerHTML=html;}function getPrevMonthHtml(weekMany){var html="";var lastMonth=currentTime.substring(0, 7); //得出年⽉lastMonth=lastMonth.split('-')if(lastMonth[1]-1==0){lastMonth[1]=12;lastMonth[0]=lastMonth[0]-1;}else if(lastMonth[1]-1<10){lastMonth[1]="0"+(lastMonth[1]-1);}lastMonth=lastMonth.join('-');var monthLen=getMonthLength(lastMonth);var start=monthLen-weekMany;for(var i=start+1;i<=monthLen;i++){html+='<div class="otherMonth day" data-date="'+lastMonth+'-'+i+'">'+i+'</div>';}return html;}function getNowMonthHtml(monthLen){var html="";var MonthOne=currentTime.substring(0, 7); //得出年⽉var today=currentTime.split('-')[2];for(var i=1;i<=monthLen;i++){if(i<10){var q="0"+i;}else{var q=i;}if(i==today){html+='<div class="thisMonth day" style="background-color:'+color+';color:#fff" data-date="'+MonthOne+'-'+q+'">'+i+'</div>'; }else{html+='<div class="thisMonth day" data-date="'+MonthOne+'-'+q+'">'+i+'</div>';}}return html;}function getNextMonthHtml(weekMany,monthLen){var html="";var daynum=weekMany+monthLen;if(daynum%7==0){}else{var num=daynum%7;var lessNum=7-num; //差⼏天var lowerMonth=currentTime.substring(0, 7); //得出年⽉lowerMonth=lowerMonth.split('-')if(lowerMonth[1]+1==13){lowerMonth[1]="0"+1;lowerMonth[0]=+lowerMonth[0]+1;}else{lowerMonth[1]=+lowerMonth[1]+1;if(lowerMonth[1]<10){lowerMonth[1]="0"+lowerMonth[1];}}lowerMonth=lowerMonth.join('-');for(var i=1;i<=lessNum;i++){if(i<10){var q="0"+i}html+='<div class="otherMonth day" data-date="'+lowerMonth+'-'+q+'">'+i+'</div>';}}return html;}function getMonths(around){if(around=="prev"){currentTime=currentTime.split('-');currentTime[1]=currentTime[1]-1;if(currentTime[1]==0){currentTime[1]="12"currentTime[0]=+currentTime[0]-1}if(currentTime[1]<10){currentTime[1]="0"+currentTime[1]}currentTime=currentTime.join('-');showDay();}else if(around=="next"){currentTime=currentTime.split('-');currentTime[1]=+currentTime[1]+1;if(currentTime[1]==13){currentTime[1]="1"currentTime[0]=+currentTime[0]+1}if(currentTime[1]<10){currentTime[1]="0"+currentTime[1]}currentTime=currentTime.join('-');showDay();}}function getMonthLength(date) { // 获取每⽉有多少天let d = new Date(date)// 将⽇期设置为下⽉⼀号d.setMonth(d.getMonth()+1)d.setDate('1')// 获取本⽉最后⼀天d.setDate(d.getDate()-1)return d.getDate()}function randomColor(){ //随机颜⾊color = '#'+Math.floor(Math.random()*16777215).toString(16);if(color.length==6){color+="0"}document.querySelector(".header").style.backgroundColor=color;document.querySelector(".week").style.backgroundColor=color;};以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

js中日历的代码

js中日历的代码

js中⽇历的代码Html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>⽆标题⽂档</title><link rel="stylesheet" href="⽇历样式.css" type="text/css" /><script src="⽇历Js.js" type="text/javascript"></script></head><body><div id="table"><ul><li class="active"><h2>1</h2><P>JAN</P></li><li><h2>2</h2><P>FER</P></li><li><h2>3</h2><P>MAR</P></li><li><h2>4</h2><P>APR</P></li><li><h2>5</h2><P>MAY</P></li><li><h2>6</h2><P>JUN</P></li><li><h2>7</h2><P>JUL</P></li><li><h2>8</h2><P>AUG</P></li><li><h2>9</h2><P>SEP</P></li><li><h2>10</h2><P>OCT</P></li><li><h2>11</h2><P>NOV</P></li><li><h2>12</h2><P>DEC</P></li></ul><div class="div1"><h2>⼀⽉活动</h2><p>快过年了,⼤家商量着去哪玩啊</p></div></div></body></html>css@charset "utf-8";/* CSS Document */#table{width:300px;height:700px;background-color:#CCC;text-align:center;position:absolute;margin:0 600px 0 500px;}#table ul{line-height:10px;}#table ul li{float:left;padding:20px;list-style-type:none;}#table .div1{width:300px;height:200px;background:red;position:absolute;bottom:0px;}#table ul h2 {color:#FFF;}#table ul p {color:#FFF;}.active{background-color:black;}Js// JavaScript Documentwindow.onload=function(){var arr=[' ⼀⽉','⼆⽉','三⽉','四⽉','五⽉','六⽉','七⽉','⼋⽉','九⽉','⼗⽉','⼗⼀⽉','⼗⼆⽉',]var aTal=document.getElementById('table');var aLi=aTal.getElementsByTagName('li');var aDiv1=aTal.getElementsByTagName('div')[0];for (var i=0;i<aLi.length;i++){aLi[i].index=i;aLi[i].onmouseover=function(){for (var i=0;i<aLi.length;i++){aLi[i].className="";}this.className="active";aDiv1.innerHTML= '<h2>'+(this.index+1)+'⽉活动</h2><p>'+arr[this.index]+'</p>'; }}。

日历javascript代码

日历javascript代码
日历js代码
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('k($4j.3X){$f={};19(o p 4i $2l)k(5Z $2l[p]=="6d"){$f[p]={};19(o 4B 4i $2l[p])$f[p][4B]=$2l[p][4B]}q $f[p]=$2l[p]}q $f=$2l;19(p 4i $4j)$f[p]=$4j[p];o $c;k($62){6o.2N.7c("6W",l($){k(!$)h.2h();t $});6o.2N.7b("6N",l(){o $=h.6K;3l($.5f!=1)$=$.7e;t $});7a.2N.2T=l($,b){o A=$.1l(/6C/,"");b.6B=l($){6M.1Q=$;t b()};h.7g(A,b.6B,1n)}}l 5n(){$c=h;h.2F=[];$d=1P.75("x"

javascript实现万年历代码

javascript实现万年历代码

!DOCTYPE HTML PUBLIC -W3CDTD HTML 4.0 TransitionalENHTMLHEADTITLE在线万年历TITLEMETA http-equiv=content-type content=texthtml; charset=gb2312METAcontent=时间; JavaScript 日历;世界时间; 农历; 阳历; 阴历; 节日; 时区; 节气; 干支; 生肖; world time clock; gregorian solar; chinese lunar; calendar;name=keywordsMETA content=General name=RA TINGMETA content=ALL name=ROBOTSMETA content=1 name=REVISITMETA content=0 name=expiresMETA content=no-cache name=PragmaMETA content=no-cache name=Cache-ControlSCRIPT language=JScript!--个人偏好设定var conWeekend = 3; 周末颜色显示1=黑色, 2=绿色, 3=红色, 4=隔周休日期资料var lunarInfo=new Array(0x4bd8,0x4ae0,0xa570,0x54d5,0xd260,0xd950,0x5554,0x56af,0x9ad0,0x55d2,0x4ae0,0xa5b6,0xa4d0,0xd250,0xd255,0xb54f,0xd6a0,0xada2,0x95b0,0x4977,0x497f,0xa4b0,0xb4b5,0x6a50,0x6d40,0xab54,0x2b6f,0x9570,0x52f2,0x4970,0x6566,0xd4a0,0xea50,0x6a95,0x5adf,0x2b60,0x86e3,0x92ef,0xc8d7,0xc95f,0xd4a0,0xd8a6,0xb55f,0x56a0,0xa5b4,0x25df,0x92d0,0xd2b2,0xa950,0xb557,0x6ca0,0xb550,0x5355,0x4daf,0xa5b0,0x4573,0x52bf,0xa9a8,0xe950,0x6aa0,0xaea6,0xab50,0x4b60,0xaae4,0xa570,0x5260,0xf263,0xd950,0x5b57,0x56a0,0x96d0,0x4dd5,0x4ad0,0xa4d0,0xd4d4,0xd250,0xd558,0xb540,0xb6a0,0x95a6,0x95bf,0x49b0,0xa974,0xa4b0,0xb27a,0x6a50,0x6d40,0xaf46,0xab60,0x9570,0x4af5,0x4970,0x64b0,0x74a3,0xea50,0x6b58,0x5ac0,0xab60,0x96d5,0x92e0,0xc960,0xd954,0xd4a0,0xda50,0x7552,0x56a0,0xabb7,0x25d0,0x92d0,0xcab5,0xa950,0xb4a0,0xbaa4,0xad50,0x55d9,0x4ba0,0xa5b0,0x5176,0x52bf,0xa930,0x7954,0x6aa0,0xad50,0x5b52,0x4b60,0xa6e6,0xa4e0,0xd260,0xea65,0xd530,0x5aa0,0x76a3,0x96d0,0x4afb,0x4ad0,0xa4d0,0xd0b6,0xd25f,0xd520,0xdd45,0xb5a0,0x56d0,0x55b2,0x49b0,0xa577,0xa4b0,0xaa50,0xb255,0x6d2f,0xada0,0x4b63,0x937f,0x49f8,0x4970,0x64b0,0x68a6,0xea5f,0x6b20,0xa6c4,0xaaef,0x92e0,0xd2e3,0xc960,0xd557,0xd4a0,0xda50,0x5d55,0x56a0,0xa6d0,0x55d4,0x52d0,0xa9b8,0xa950,0xb4a0,0xb6a6,0xad50,0x55a0,0xaba4,0xa5b0,0x52b0,0xb273,0x6930,0x7337,0x6aa0,0xad50,0x4b55,0x4b6f,0xa570,0x54e4,0xd260,0xe968,0xd520,0xdaa0,0x6aa6,0x56df,0x4ae0,0xa9d4,0xa4d0,0xd150,0xf252,0xd520);var solarMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);var Gan=new Array(甲,乙,丙,丁,戊,己,庚,辛,壬,癸);var Zhi=new Array(子,丑,寅,卯,辰,巳,午,未,申,酉,戌,亥);var Animals=new Array(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪);var solarTerm = new Array(小寒,大寒,立春,雨水,惊蛰,春分,清明,谷雨,立夏,小满,芒种,夏至,小暑,大暑,立秋,处暑,白露,秋分,寒露,霜降,立冬,小雪,大雪,冬至);var sTermInfo = new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,2633 43,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758);var nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十');var nStr2 = new Array('初','十','廿','卅','□');var monthName = new Array(JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC);公历节日表示放假日var sFtv = new Array(0101新年元旦,0202 世界湿地日,0207 国际声援南非日,0210 国际气象节,0214 情人节,0301 国际海豹日,0303 全国爱耳日,0308 国际妇女节,0312 植树节孙中山逝世纪念日,0314 国际警察日,0315 国际消费者权益日,0317 中国国医节国际航海日,0321 世界森林日消除种族歧视国际日,0321 世界儿歌日,0322 世界水日,0323 世界气象日,0324 世界防治结核病日,0325 全国中小学生安全教育日,0330 巴勒斯坦国土日,0401 愚人节全国爱国卫生运动月(四月) 税收宣传月(四月),0407 世界卫生日,0422 世界地球日,0423 世界图书和版权日,0424 亚非新闻工作者日,0501 国际劳动节,0504 中国五四青年节,0505 碘缺乏病防治日,0508 世界红十字日,0512 国际护士节,0515 国际家庭日,0517 世界电信日,0518 国际博物馆日,0520 全国学生营养日,0523 国际牛奶日,0531 世界无烟日,0601 国际儿童节,0605 世界环境日,0606 全国爱眼日,0617 防治荒漠化和干旱日,0623 国际奥林匹克日,0625 全国土地日,0626 国际反毒品日,0701 中国共产党建党日世界建筑日,0702 国际体育记者日精品推介站()正式对外开放纪念日, 0707 中国人民抗日战争纪念日,0711 世界人口日,0730 非洲妇女日,0801 中国建军节,0808 中国男子节(爸爸节),0815 日本正式宣布无条件投降日,0908 国际扫盲日国际新闻工作者日,0910 教师节,0914 世界清洁地球日梅竹松生日^o^,0916 国际臭氧层保护日,0918 九·一八事变纪念日,0920 国际爱牙日,0927 世界旅游日,1001国庆节世界音乐日国际老人节,1001 国际音乐日,1002 国际和平与民主自由斗争日,1004 世界动物日,1008 全国高血压日,1008 世界视觉日,1009 世界邮政日万国邮联日,1010 辛亥革命纪念日世界精神卫生日,1013 世界保健日国际教师节,1014 世界标准日,1015 国际盲人节(白手杖节),1016 世界粮食日,1017 世界消除贫困日,1022 世界传统医药日,1024 联合国日世界发展信息日,1031 世界勤俭日,1107 十月社会主义革命纪念日,1108 中国记者日,1109 全国消防安全宣传教育日,1110 世界青年节,1111 国际科学与和平周(本日所属的一周),1112 孙中山诞辰纪念日,1114 世界糖尿病日,1117 国际大学生节世界学生节,1121 世界问候日世界电视日,1129 国际声援巴勒斯坦人民国际日,1201 世界艾滋病日,1203 世界残疾人日,1205 国际经济和社会发展志愿人员日,1208 国际儿童电视日,1209 世界足球日,1210 世界人权日,1212 西安事变纪念日,1213 南京大屠杀(1937年)纪念日!紧记血泪史!,1221 国际篮球日,1224 平安夜,1225 圣诞节,1229 国际生物多样性日);某月的第几个星期几。

日历的制作代码

日历的制作代码

日历的制作代码<?php$year=@$_GET['year']; //获得地址栏的年份$month=@$_GET['month']; //获得地址栏的月份if(empty($year))$year=date("Y"); //初始化为本年度的年份if(empty($month))$month=date("n"); //初始化为本年度的月份$day=date("j"); //获取当天的天数$wd_ar=array("日","一","二","三","四","五","六"); //星期数组$wd=date("w",mktime(0,0,0,$month,1,$year)); //计算当月第一天是星期几//年链接$y_lnk1=$year<=1970?$year=1970:$year-1;$y_lnk2=$year>=2037?$year=2037:$year+1;//月链接$m_lnk1=$month<=1?$month=1:$month-1;$m_lnk2=$month>=12?$momth=12:$month+1;echo"<table cellpadding=6 cellspacing=0 width=200 bgcolor=#eeeeee><tr align=center bgcolor=#cccccc>";//输出年份。

单击“<”链接跳到上一年,单击“>”跳到下一年echo"<td colspan=4><a href='key.php?year=$y_lnk1&month=$month'><</a>".$year."年<a href='key.php?year=$y_lnk2&month=$month'>></a><td>";//输出月份。

dw简单实用的日历框效果,带有当前时间显示

dw简单实用的日历框效果,带有当前时间显示
document.writeln('<TH BGCOLOR="#ff6699"><font style="font-size:9pt;Color:White">二</FONT></TH>');
document.writeln('<TH BGCOLOR="#ff6699"><font style="font-size:9pt;Color:White">三</FONT></TH>');
var timevalue = " " + ((hours >12) ? hours -12 :hours)
timevalue += ((minutes < 10) ? ":0" : ":") + minutes
timevalue += ((seconds < 10) ? ":0" : ":") + seconds
document.writeln('<TH BGCOLOR="#ff6699"><font style="font-size:9pt;Color:White">六</FONT></TH>');
document.writeln("</TR><TR>");
column = 0;
for (i=0; i<startDay; i++) {

dreamweaver 经典网页特效及网页代码设计技巧系列之---农历日期

dreamweaver 经典网页特效及网页代码设计技巧系列之---农历日期

dreamweaver 经典网页特效及网页代码设计技巧系列之---农历日期我爱浪漫樱花资料网<scriptlanguage="JavaScript"><!-- Beginvar bsYear;var bsDate;var bsWeek;var arrLen=8; var sValue=0; var dayiy=0;我爱浪漫樱花资料网var miy=0;var iyear=0;var dayim=0;var spd=86400; varyear1999="30;29;29;30;29;29;30;29;30;30;30;29";varyear2000="30;30;29;我爱浪漫樱花资料网29;30;29;29;30;29;30;30;29";varyear2001="30;30;29;30;29;30;29;29;30;29;30;29;30";varyear2002="30;30;29;30;29;30;29;29;30;29;30;29";我爱浪漫樱花资料网varyear2003="30;30;29;30;30;29;30;29;29;30;29;30";varyear2004="29;30;29;30;30;29;30;29;30;29;30;29;30";varyear2005="29;30;29;我爱浪漫樱花资料网30;29;30;30;29;30;29;30;29";varyear2006="30;29;30;29;30;30;29;29;30;30;29;29;30";var month1999="正月;二月;三月;四月;五月;六月;七月;八月;九月;十月;十一月;十二月"我爱浪漫樱花资料网var month2001="正月;二月;三月;四月;闰四月;五月;六月;七月;八月;九月;十月;十一月;十二月"var month2004="正月;二月;闰二月;三月;四月;五月;六月;七月;八月;九月;十月;十一月;十二月"我爱浪漫樱花资料网var month2006="正月;二月;三月;四月;五月;六月;七月;闰七月;八月;九月;十月;十一月;十二月"var Dn="初一;初二;初三;初四;初五;初六;初七;初八;初九;初十;十一;十二;十三;十四;十五;十六;十七;十八;我爱浪漫樱花资料网十九;二十;廿一;廿二;廿三;廿四;廿五;廿六;廿七;廿八;廿九;三十";var Ys=newArray(arrLen); Ys[0]=919094400;Ys[1]=949680000;Ys[2]=980265600;Ys[3]=1013443200;Ys我爱浪漫樱花资料网[4]=1044028800;Ys[5]=1074700800; Ys[6]=1107878400;Ys[7]=1138464000; var Yn=newArray(arrLen); Yn[0]="己卯年";Yn[1]="庚辰年";Yn[2]="辛巳年";Yn[3]="壬午年我爱浪漫樱花资料网";Yn[4]="癸未年";Yn[5]="甲申年";Yn[6]="乙酉年";Yn[7]="丙戌年";var D=new Date(); var yy=D.getYear(); varmm=D.getMonth()+1; var dd=D.getDate(); var ww=D.getDay();我爱浪漫樱花资料网if (ww==0) ww="<fontcolor=RED>星期日"; if (ww==1) ww="星期一";if (ww==2) ww="星期二";if (ww==3) ww="星期三";if (ww==4) ww="星期四";我爱浪漫樱花资料网if (ww==5) ww="星期五";if (ww==6) ww="<font color=RED>星期六";ww=ww;varss=parseInt(D.getTime() / 1000); if (yy<100) yy="19"+yy;我爱浪漫樱花资料网for(i=0;i<arrLen;i++) if (ss>=Ys[i]){ iyear=i; sValue=ss-Ys[i]; }dayiy=parseInt(sValue/spd)+1;var dpm=year1999; if (iyear==1)我爱浪漫樱花资料网dpm=year2000; if (iyear==2) dpm=year2001; if (iyear==3) dpm=year2002; if (iyear==4) dpm=year2003; if (iyear==5) dpm=year2004; if (iyear==6) 我爱浪漫樱花资料网dpm=year2005; if (iyear==7)dpm=year2006; dpm=dpm.split(";"); var Mn=month1999; if (iyear==2) Mn=month2001; if (iyear==5)Mn=month2004; if (iyear==7)我爱浪漫樱花资料网Mn=month2006; Mn=Mn.split(";"); var Dn="初一;初二;初三;初四;初五;初六;初七;初八;初九;初十;十一;十二;十三;十四;十五;十六;十七;十八;十九;二十;廿一;廿二;廿三;廿四;廿五;廿六;廿七;廿八;廿九;三十我爱浪漫樱花资料网";Dn=Dn.split(";"); dayim=dayiy;var total=newArray(13);total[0]=parseInt(dpm[0]);for(i=1;i<dpm.length-1;i++)我爱浪漫樱花资料网total[i]=parseInt(dpm[i])+total[i-1]; for(i=dpm.length-1;i>0;i--)if(dayim>total[i-1]){ dayim=dayim-total[i-1];miy=i;我爱浪漫樱花资料网}bsWeek=ww;bsDate=yy+"年"+mm+"月 ";bsDate2=dd; bsYear="农历"+Yn[iyear]; bsYear2=Mn[miy]+Dn[dayim-1];if我爱浪漫樱花资料网(ss>=Ys[7]||ss<Ys[0]) bsYear=Yn[7]; window.status="今天是:"+bsDate+bsDate2+"日 "+bsWeek+","+bsYear+" "+bsYear2; // End --></script>。

js实现日历

js实现日历

js实现⽇历这周写⾃⼰的项⽬发现⼜⽤到⽇历了,加之⾃⼰毕业之后的第⼀个⼯作中遇到的任务也是需要写个⽇历(组员写了,我就不⽤写了)今天就来好好折腾⼀下⽇历是怎么写的。

⾸先,我们看看 windows 的⽇历。

发现总共有这么⼏个元素。

先实现试试。

1.年份的选择、⽉份的选择2.周⼀ ~ 周⽇(周⽇ ~ 周六)3.⽇历格⼦ 6*7 = 42从数据的⾓度来分析⽇历的实现是⽐较简单的1.我们需要显⽰⼀个当前时间的结构 - new Date()2.我们需要显⽰当⽉的信息 - [星期(周⼀~周⽇),⽇期(1-[28,29,30,31])]其中我们只要知道了每个⽉的 1⽇是星期⼏,就能很容易地摆放后⾯的⽇⼦(万事开头难)。

我们最多需要 6 ⾏来显⽰我们的⽇期,因为要第⼀排如果只包含本⽉的⼀天 6(上个⽉的) + (1 + 4*7),这样就五⾏了,当⽉天数若⼤于 29,就显⽰不下了确定了 6 ⾏之后,我们发现我们可能需要获取上个⽉,和下个⽉多出来的⼏天的摆放位置。

不同年份的不同⽉的 2⽉份,我们知道它的⽇期是不同的,所以我们还需要判断平年还是闰年。

3.显⽰上个⽉,下个⽉的切换。

我们发现需要有个函数来帮我们更新⽇历。

分析完之后,让我们跟着新增/修改⼀些代码。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>.week-item {display: inline-block;width: 80px;height: 40px;line-height: 40px;border: 1px solid sandybrown;text-align: center;}.date-item {display: inline-block;width: 80px;height: 40px;line-height: 40px;border: 1px solid beige;text-align: center;}</style></head><body><div class="wrapper"><div class="year-line"><button id="preMonth" class="year-prev">上⼀⽉</button><button id="nowYear" class="year-now"></button><button id="nowMonth"></button><button id="nowDate"></button><button id="nextMonth" class="year-next">下⼀⽉</button></div><div id="weekLine" class="week-line"></div><div id="dateWrap" class="date-wrap"></div></div></body><script>// ⼯具⽅法 - start// 1.为了获得每个⽉的⽇期有多少,我们需要判断平年闰年[四年⼀闰,百年不闰,四百年再闰]const isLeapYear = (year) => {return (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0);};// 2.获得每个⽉的⽇期有多少,注意 month - [0-11]const getMonthCount = (year, month) => {let arr = [31, null, 31, 30,31, 30, 31, 31,30, 31, 30, 31];let count = arr[month] || (isLeapYear(year) ? 29 : 28);return Array.from(new Array(count), (item, value) => value + 1);};// 3.获得某年某⽉的 1号是星期⼏,这⾥要注意的是 JS 的 API-getDay() 是从 [⽇-六](0-6),返回 numberconst getWeekday = (year, month) => {let date = new Date(year, month, 1);return date.getDay();};// 4.获得上个⽉的天数const getPreMonthCount = (year, month) => {if (month === 0) {return getMonthCount(year - 1, 11);} else {return getMonthCount(year, month - 1);}};// 5.获得下个⽉的天数const getNextMonthCount = (year, month) => {if (month === 11) {return getMonthCount(year + 1, 0);} else {return getMonthCount(year, month + 1);}};// ⼯具⽅法 - endlet weekStr = '⽇⼀⼆三四五六';weekArr = weekStr.split('').map(item => '星期' + item);// 插⼊星期 domlet weekDomStr = '';let oFragWeek = document.createDocumentFragment();weekArr.forEach(item => {let oSpan = document.createElement('span');let oText = document.createTextNode(item);oSpan.appendChild(oText);oSpan.classList.add('week-item');oFragWeek.appendChild(oSpan);});let weekWrap = document.getElementById('weekLine');weekWrap.appendChild(oFragWeek);// 这⾥获得我们第⼀次的数据数组const updateCalendar = (year, month, day) => {if (typeof year === 'undefined' && typeof month === 'undefined' && typeof day === 'undefined') {let nowDate = new Date();year = nowDate.getFullYear();month = nowDate.getMonth();day = nowDate.getDate();}// 更新⼀下顶部的年⽉显⽰document.getElementById('nowYear').innerHTML = year;document.getElementById('nowMonth').innerHTML = month + 1;document.getElementById('nowDate').innerHTML = day;// ⽣成⽇历数据,上个⽉剩下的的 x 天 + 当⽉的 28(平年的2⽉)或者29(闰年的2⽉)或者30或者31天 + 下个⽉的 y 天 = 42 let res = [];let currentMonth = getMonthCount(year, month);let preMonth = getPreMonthCount(year, month);let nextMonth = getNextMonthCount(year, month);let whereMonday = getWeekday(year, month);if (whereMonday === 0) {whereMonday = 7}// 感谢⽹友 luoyiming 的测试(哈哈!谢谢!):这⾥当 whereMonday 为 0 的时候会截取上⽉的所有数据let preArr = preMonth.slice(-1 * whereMonday)let nextArr = nextMonth.slice(0, 42 - currentMonth.length - whereMonday);res = [].concat(preArr, currentMonth, nextArr);// 上⾯经过我本⼈的测试是没有什么问题,接下来就是更新 dom 的信息的问题let hadDom = document.getElementsByClassName('date-item');if (hadDom && hadDom.length) {let domArr = document.getElementsByClassName('date-item');for (let i = 0; i < domArr.length; i++) {domArr[i].innerHTML = res.shift();}} else {// 如果之前没有结构的话let str = '';for (let i = 0; i < 6; i++) {str += '<div class="date-line">';for (let j = 0; j < 7; j++) {str += `<span class='date-item'>${res.shift()}</span>`;if (j === 6) {str += '</div>';}}}document.getElementById('dateWrap').innerHTML = str;}};updateCalendar();// 添加上⼀⽉,下⼀⽉事件let oPreButton = document.getElementById('preMonth');let oNextButton = document.getElementById('nextMonth');oPreButton.addEventListener('click', function () {let currentYear = +document.getElementById('nowYear').textContent;let currentMonth = +document.getElementById('nowMonth').textContent - 1;let currentDate = +document.getElementById('nowDate').textContent;if (currentMonth === 0) {updateCalendar(currentYear - 1, 11, currentDate);} else {updateCalendar(currentYear, currentMonth - 1, currentDate);}});oNextButton.addEventListener('click', function () {let currentYear = +document.getElementById('nowYear').textContent;let currentMonth = +document.getElementById('nowMonth').textContent - 1;let currentDate = +document.getElementById('nowDate').textContent;if (currentMonth === 11) {updateCalendar(currentYear + 1, 0, currentDate);} else {updateCalendar(currentYear, currentMonth + 1, currentDate);}});</script></html>发现⽤ dom 直接操作⽽不是通过 mvvm 框架实现确实还是⽐较蛋疼的,以下是这次实现的效果。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
var days = new Array("日","一", "二", "三","四", "五", "六");
varclassTemp;
var today=new getToday();
var year=today.year;
var month=today.month;
var newCal;
function getDays(month, year) {
<thead>
<tr align="center" valign="middle">
<td colspan="7" class="Title">
<a href="javaScript:subMonth();" title="上一月" Class="DayButton">3</a> <input name="year" type="text" size="4" maxlength="4" onkeydown="if (event.keyCode==13){setDate()}" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onpaste="this.value=this.value.replace(/[^0-9]/g,'')">年<input name="month" type="text" size="1" maxlength="2" onkeydown="if (event.keyCode==13){setDate()}" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onpaste="this.value=this.value.replace(/[^0-9]/g,'')">月<a href="JavaScript:addMonth();" title="下一月" Class="DayButton">4</a>
{
month=0;
year=year+1;
} else
{
month=month+1;
}
Calendar();
}
function setDate()
{
if (document.all.month.value<1||document.all.month.value>12)
{
alert("月的有效范围在1-12之间!");
{
day = today.day;
}
var caltable = document.all.caltable.tBodies.calendar;
var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
for (var intWeek = 0; intWeek < caltable.rows.length;intWeek++)
.DayButton {font-family: Webdings;font-size: 9pt;font-weight: bold;color: #243F65;cursor:hand;text-decoration: none;}
</Style>
<table border="0" cellpadding="0" cellspacing="1" class="Calendar" id="caltable">
.DaySun {font-family: verdana;font-size: 7pt;color: #FF0000;text-decoration: none;background-color:#E5E9F2;text-align: center;height: 18px;width: 12%;}
if(day==daily) cell.className="DayNow";
else if(intDay==6) cell.className = "DaySat";
else if (intDay==0) cell.className ="DaySun";
else cell.className="Day";
obj.runtimeStyle.cssText = "background-color:#FFFFFF";
// obj.className="Hover";
}
function buttonOut()
{
var obj = window.event.srcElement;
window.setTimeout(function(){obj.runtimeStyle.cssText = "";},300);
if ((intDay == startDay) && (0 == daily)){ daily = 1;}
var daytemp=daily<10?("0"+daily):(daily);
var d="<"+newCal.getFullYear()+"-"+montemp+"-"+daytemp+">";
if ((daily > 0) && (daily <= intDaysInMonth))
{
cell.innerText = daily;
daily++;
} else
{
cell.className="CalendarTD";
cell.innerText = "";
}
}
document.all.year.value=year;
.Calendar {font-family: verdana;text-decoration: none;width: 170;background-color: #C0D0E8;font-size: 9pt;border:0px dotted #1C6FA5;}
.CalendarTD {font-family: verdana;font-size: 7pt;color: #000000;background-color:#f6f6f6;height: 20px;width:11%;text-align: center;}
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();
document.all.monthຫໍສະໝຸດ value=month+1;
}
function subMonth()
{
if ((month-1)<0)
{
month=11;
year=year-1;
} else
{
month=month-1;
}
Calendar();
}
function addMonth()
{
if((month+1)>11)
for (var intDay = 0;intDay < caltable.rows[intWeek].cells.length;intDay++)
{
var cell = caltable.rows[intWeek].cells[intDay];
var montemp=(newCal.getMonth()+1)<10?("0"+(newCal.getMonth()+1)):(newCal.getMonth()+1);
.DaySatTitle {font-family: verdana;font-size: 9pt;color:#FF0000;text-decoration: none;background-color:#C0D0E8;text-align: center;height: 20px;width: 12%;}
return;
}
year=Math.ceil(document.all.year.value);
month=Math.ceil(document.all.month.value-1);
Calendar();
}
</Script>
<Script>
function buttonOver()
{
相关文档
最新文档