日历JS脚本
轻量级的原生js日历插件calendar.js使用指南
轻量级的原⽣js⽇历插件calendar.js使⽤指南使⽤说明:需要引⼊插件calendar.js/calendar.min.js须要引⼊calendar.css 样式表,可以⾃定义⾃⼰想要的⽪肤本⽇历插件⽀持cmd模块化如下调⽤:复制代码代码如下:xvDate({'targetId':'date1',//时间写⼊对象的id'triggerId':['date1','dateBtn1'],//触发事件的对象id'alignId':'datesWrap1',//⽇历对齐对象'format':'-',//时间格式默认'YYYY-MM-DD HH:MM:SS''min':'2014-09-20 10:00:00',//最⼤时间'max':'2014-10-30 10:00:00'//最⼩时间});参数说明:targetId :⽇期写⼊对象的id,不能为空triggerId :触发事件的对象id,如果不设置则默认为targetIdalignId :⽇历盒⼦的对齐基准,如果不设置则默认为targetIdhms :时分秒是否开启,默认值为'on'则表⽰开启时分秒(2014-09-20 10:00:00),'off'则表⽰关闭时分秒模式(2014-09-20) format :为⽇期格式默认值为'-' (2014-09-20),'/'则表⽰(2014/09/20)min :最⼩时间限制,min 的时间格式和前⾯的时间格式保持⼀直max :最⼤时间限制,max 的时间格式和前⾯的时间格式保持⼀直zIndex :最⼤时间限制,⽇历盒⼦的层级,默认9999如果需要使⽤模块化在需要依赖的模块⽤引⼊即可如:复制代码代码如下:define('mod1',[],function(require, exports, module){var xvDate = require("xvDate");})以上所述就是本⽂的全部内容了,希望⼤家能够喜欢。
JS实现日期加减的方法
JS实现日期加减的方法在JavaScript中,可以使用Date对象和一些内置方法来实现日期的加减。
下面是一种方法,使用1200字以上对其进行详细说明。
在JavaScript中,Date对象表示了一个特定的日期和时间。
我们可以使用Date对象进行日期的加减操作。
以下是一种常用的日期加减的方法:```javascript//加法function addDays(date, days)var result = new Date(date);result.setDate(result.getDate( + days);return result;//减法function subtractDays(date, days)var result = new Date(date);result.setDate(result.getDate( - days);return result;```这两个函数都接受两个参数:`date`表示要进行加减操作的日期,`days`表示要加减的天数。
对于加法函数,我们使用`Date.setDate(`方法将`date`的日期设置为`date`的日期加上`days`;对于减法函数,我们使用`Date.setDate(`方法将`date`的日期设置为`date`的日期减去`days`。
这两个函数返回的是一个新的Date对象,代表进行加减操作后的日期。
原始的`date`对象不会受到影响。
接下来,我们可以使用这些函数来进行日期的加减操作。
以下是一个示例:```javascriptvar date = new Date(; // 获取当前日期var nextWeek = addDays(date, 7); // 将日期增加7天console.log(nextWeek);var lastMonth = subtractDays(date, 30); // 将日期减少30天console.log(lastMonth);```在我们的示例中,我们首先创建一个新的Date对象,然后使用`addDays(`函数将日期增加7天,并将结果保存在`nextWeek`变量中。
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获取⽇期:昨天今天和明天、后天复制代码代码如下:<html><head><meta http-equiv="Content-Type" content="textml; charset=utf-8"><title>js获取⽇期:前天、昨天、今天、明天、后天 - </title></head><body><script language="JavaScript" type="text/javascript">function GetDateStr(AddDayCount) {var dd = new Date();dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的⽇期var y = dd.getFullYear();var m = dd.getMonth()+1;//获取当前⽉份的⽇期var d = dd.getDate();return y+"-"+m+"-"+d;}document.write("前天:"+GetDateStr(-2));document.write("<br />昨天:"+GetDateStr(-1));document.write("<br />今天:"+GetDateStr(0));document.write("<br />明天:"+GetDateStr(1));document.write("<br />后天:"+GetDateStr(2));document.write("<br />⼤后天:"+GetDateStr(3));</script></body><ml>其中有⼀种⽅法是:Date.parse(dateVal),此函数功能强⼤,但是有个致命的缺点,那就是不⽀持我们常⽤的“年-⽉-⽇”格式,短⽇期可以使⽤“/”或“-”作为⽇期分隔符,但是必须⽤⽉/⽇/年的格式来表⽰,例如"7/20/96"。
js+php 实现一款超酷的日历控件
return;
}
var cls = el.className.split(" ");
var ar = new Array();
for (var i = cls.length; i > 0;) {
if (cls[--i] != className) {
var tmp = this.getAbsolutePos(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
};
Calendar.isRelated = function (el, evt) {
var related = evt.relatedTarget;
Calendar.is_ie5 = ( Calendar.is_ie && /msie 5\.0/i.test(erAgent) );
/// detect Opera browser
Calendar.is_opera = /opera/i.test(erAgent);
* Read the entire license text here: /licenses/lgpl.html
*/
// $Id: calendar.js,v 1.51 2005/03/07 16:44:31 mishoo Exp $
/** The Calendar object constructor. */
this.multiple = null;
// HTML elements
Calendar日历控件使用
Calendar⽇历控件使⽤<link rel="stylesheet" href="__STATIC__/js/calendar/calendar-blue.css"/><script src="__STATIC__/js/calendar/calendar.js"></script>⾸先引⼊ calendar.js calendar-blue.css ⽂件下载链接在HTML 的body 标签下1<input type="text" name="time_start" id="J_time_start" class="date" size="12">2 -3<input type="text" name="time_end" id="J_time_end" class="date" size="12">45<script>6 Calendar.setup({7 inputField : "J_time_start",8 ifFormat : "%Y-%m-%d",9 showsTime : false,10 timeFormat : "24"11 });12 Calendar.setup({13 inputField : "J_time_end",14 ifFormat : "%Y-%m-%d",15 showsTime : false,16 timeFormat : "24"17 });18 $('.J_preview').preview(); //查看⼤图19 $('.J_cate_select').cate_select({top_option:lang.all}); //分类联动20 $('.J_tooltip[title]').tooltip({offset:[10, 2], effect:'slide'}).dynamic({bottom:{direction:'down', bounce:true}});21</script>运⾏⼀下吧<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><link rel="stylesheet" href="__STATIC__/js/calendar/calendar-blue.css"/></head><body><input type="text" name="time_start" id="J_time_start" class="date" size="12">-<input type="text" name="time_end" id="J_time_end" class="date" size="12"><script src="__STATIC__/js/calendar/calendar.js"></script><script>Calendar.setup({inputField : "J_time_start",ifFormat : "%Y-%m-%d",showsTime : false,timeFormat : "24"});Calendar.setup({inputField : "J_time_end",ifFormat : "%Y-%m-%d",showsTime : false,timeFormat : "24"});$('.J_preview').preview(); //查看⼤图$('.J_cate_select').cate_select({top_option:lang.all}); //分类联动$('.J_tooltip[title]').tooltip({offset:[10, 2], effect:'slide'}).dynamic({bottom:{direction:'down', bounce:true}});</script></body></html>效果图如下:如果想要在input 标签右侧出现⼩图标:看下⾯<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><link rel="stylesheet" href="__STATIC__/js/calendar/calendar-blue.css"/><style type="text/css">input.date,input.endDate{background: #fff url(__STATIC__/css/admin/bgimg/input_date.png) no-repeat right 3px; padding-right:18px;font-size:12px;} </style></head><body><input type="text" name="time_start" id="J_time_start" class="date" size="12" >-<input type="text" name="time_end" id="J_time_end" class="date" size="12" ><script src="__STATIC__/js/calendar/calendar.js"></script><script>Calendar.setup({inputField : "J_time_start",ifFormat : "%Y-%m-%d",showsTime : false,timeFormat : "24"});Calendar.setup({inputField : "J_time_end",ifFormat : "%Y-%m-%d",showsTime : false,timeFormat : "24"});$('.J_preview').preview(); //查看⼤图$('.J_cate_select').cate_select({top_option:lang.all}); //分类联动$('.J_tooltip[title]').tooltip({offset:[10, 2], effect:'slide'}).dynamic({bottom:{direction:'down', bounce:true}});</script></body></html>。
js显示时间+农历+节日
195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
}
if(offset==0 && leap>0 && i==leap+1)
if(this.isLeap)
{ this.isLeap = false; }
else
{ this.isLeap = true; --i; --this.monCyl;}
return(cl+SY+'年'+(SM+1)+'月'+SD+'日</font>');
}
function weekday(){
var day = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var cl = '<font color="#000000" STYLE="font-size:9pt;">';
if(leap>0 && i==(leap+1) && this.isLeap==false)
js获取当前周、上一周、下一周日期
js获取当前周、上⼀周、下⼀周⽇期效果图:代码如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html ><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>⽇历操作</title><script>window.onload = function(){var cells = document.getElementById('monitor').getElementsByTagName('td');var clen = cells.length;var currentFirstDate;var formatDate = function(date){var year = date.getFullYear()+'年';var month = (date.getMonth()+1)+'⽉';var day = date.getDate()+'⽇';var week = '('+['星期天','星期⼀','星期⼆','星期三','星期四','星期五','星期六'][date.getDay()]+')';return year+month+day+' '+week;};var addDate= function(date,n){date.setDate(date.getDate()+n);return date;};var setDate = function(date){var week = date.getDay()-1;date = addDate(date,week*-1);currentFirstDate = new Date(date);for(var i = 0;i<clen;i++){cells[i].innerHTML = formatDate(i==0 ? date : addDate(date,1));}};document.getElementById('last-week').onclick = function(){setDate(addDate(currentFirstDate,-7));};document.getElementById('next-week').onclick = function(){setDate(addDate(currentFirstDate,7));};setDate(new Date());}</script></head><body><button id="last-week">上⼀周</button><button id="next-week">下⼀周</button><table id="monitor"><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table></body></html>以上就是本⽂的全部内容,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,同时也希望多多⽀持!。
JS日期操作代码,获取当前日期,加一天,减一天
JS⽇期操作代码,获取当前⽇期,加⼀天,减⼀天全选注:引⼊外部Js需再刷新⼀下页⾯才能执⾏]"/TR/xhtml1/DTD/xhtml1-transitional.dtd"><metacontent="text/html;charset=utf-8" http-equiv="Content-type"/><title>title</title> <inputid="date" type="text"/><input id="Add"type="button"value="加"/> <inputid="Minus" type="button"value="减"/> <scripttype="text/javascript"> $= function(o) { returntypeof o === 'string' ?document.getElementById(o) : o }; //设置当前⽇期var ma =[['1','3','5','7','8','10'],['4','6','9','11']]; var dd =new Date(); var x =dd.toLocaleString(); var o= pasDate(x);$('date').value = o.join('-');//转化⽇期函数 functionpasDate(da) { var yp =da.indexOf('年'), mp =da.indexOf('⽉'), dp =da.indexOf('⽇'); var y =da.substr(0,yp), m =da.substr(yp + 1,mp - yp -1), d = da.substr(mp +1,dp - mp - 1); return[y,m,d]; } //判断数组a是否存在在元素n functioncheck(n,a) { for(var i =0,len = a.length;i <len;i++) { if(a[i] == n) {return true; } } returnfalse; } //闰?年? functionisLeap(y) { return ((y % 4== 0 && y % 100 != 0) || y% 400 == 0) ? true : false;} //实现加减f:'1'加,'0'减function trans(o,f) { var d= o.split('-'); var l =isLeap(d[0]); if(f == '1') {if((check(d[1],ma[0]) &&(d[2] == '31')) ||(check(d[1],ma[1]) &&(d[2] == '30')) || (d[1] == '2'&& d[2] == '28' && !l) ||(d[1] == '2' && d[2] == '29'&& l)) { return d[0] + '-' +。
JSP日历控件
cal.sel.value = date; // just update the date in the input field.
if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
cal.showsTime = true;
cal.time24 = (showsTime == "24");
}
if (showsOtherMonths) {
cal.showsOtherMonths = true;
}
calendar = cal; // remember it in the global var
cal.callCloseHandler();
}
function closeHandler(cal) {
cal.hide(); // hide the calendar
// cal.destroy();
calendar = null;
return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}
</script>
<input type=text readonly size=12 name="f_date" id=start value="<%=fbrq%>"><input type="reset" value="选择" onclick="return showCalendar('start', '%Y-%m-%d %H:%M', '24', true);">
JS制作简单的日历控件【JS Date对象练习实例】
JS制作简单的日历控件【JS Date对象练习实例】一直对JS 中的Date 对象不是很熟练,缺乏操作实践,端午节抽空复习了一下,做了一个简单的日期选择控件日历外观参考了淘宝旅行中的日期控件,控件只有基本功能,木有做节日显示,木有做IE6的SELECT遮挡控制,仅供练习:使用方法:只需传入日期INPUT元素的ID即可,isSelect选项为是否为SELECT下拉选择年月设置var myDate1 = new Calender({id:'j_Date1'});var myDate2 = new Calender({id:'j_Date2',isSelect:!0});演示如下:支持选择年月支持SELECT选择年月没空做教程啦,有兴趣的看源代码吧,详细源代码+注释如下:<!DOCTYPE HTML><html><head><meta charset="utf-8"><title>日历组件示例</title><style>.calendar{font-family:Tahoma; background:#fff; float:left; border-style:solid; border-width:1px; border-color:#85BEE5 #3485C0 #3485C0 #85BEE5; position:relative; padding:10px; overflow:hidden;}.calendar dl,.calendar dd{margin:0; padding:0; width:183px; font-size:12px; line-height:22px;}.calendar dt.title-date{ display:block; border-bottom:1px solid #E4E4E4; font-weight:700; position:relative; margin-bottom:5px;}.calendar dt{ float:left; width:25px; margin-left:1px; text-align:center;}.calendar dt.title-date{ width:100%;}.calendar dd{clear: both;width: 183px;height: 139px;font-weight: 700;background:url(/cnblogs_com/NNUF/379856/o_bg.png) no-repeat; margin:0;}.prevyear,.nextyear, .prevmonth,.nextmonth{cursor:pointer;height:9px; background:url(/cnblogs_com/NNUF/379856/o_nextprv.png)no-repeat; overflow:hidden;position:absolute; top:8px; text-indent:-999px;}.prevyear{ left:4px; width:9px;}.prevmonth{ width:5px; background-position:-9px 0; left:20px;}.nextyear{ width:9px; background-position:-19px 0; right:5px;}.nextmonth{ width:5px; background-position:-14px 0; right:20px;}.calendar dd a{float: left;width: 25px;height: 22px; color:blue; overflow: hidden; text-decoration: none;margin: 1px 0 0 1px; text-align:center;}.calendar dd a.disabled{color:#999;}.calendar dd a.tody{ color:red; }.calendar dd a.on{background:blue; color:#fff;}.calendar dd a.live{cursor:pointer}.input{ border:1px solid #ccc; padding:4px; background:url(/cnblogs_com/NNUF/379856/o_nextprv.png)no-repeat right -18px;}</style></head><body><br/><br/><h3>支持选择年月 支持SELECT选择年月</h3><div><input type="text" id="j_Date1" class="input"> <input type="text" id="j_Date2" class="input"></div><br/><div></div><!--日历控件JS源码--><script>/*** @namespace _CalF* 日历控件所用便捷函数* */_CalF = {// 选择元素$:function(arg,context){var tagAll,n,eles=[],i,sub = arg.substring(1);context = context||document;if(typeof arg =='string'){switch(arg.charAt(0)){case '#':return document.getElementById(sub);break;case '.':if(context.getElementsByClassName) return context.getElementsByClassName(sub);tagAll = _CalF.$('*',context);n = tagAll.length;for(i = 0;i<n;i++){if(tagAll[i].className.indexOf(sub) > -1) eles.push(tagAll[i]);}return eles;break;default:return context.getElementsByTagName(arg);break;}}},// 绑定事件bind:function(node,type,handler){node.addEventListener?node.addEventListener(type, handler, false):node.attachEvent('on'+ type, handler);},// 获取元素位置getPos:function (node) {var scrollx = document.documentElement.scrollLeft || document.body.scrollLeft,scrollt = document.documentElement.scrollTop || document.body.scrollTop;pos = node.getBoundingClientRect();return {top:pos.top + scrollt, right:pos.right + scrollx, bottom:pos.bottom + scrollt, left:pos.left + scrollx }},// 添加样式名addClass:function(c,node){node.className = node.className + ' ' + c;},// 移除样式名removeClass:function(c,node){var reg = new RegExp("(^|\\s+)" + c + "(\\s+|$)","g");node.className = node.className.replace(reg, '');},// 阻止冒泡stopPropagation:function(event){event = event || window.event;event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;}};/*** @name Calender* @constructor* @created by VVG* @/NNUF/* @mysheller@* */function Calender() {this.initialize.apply(this, arguments);}Calender.prototype = {constructor:Calender,// 模板数组_template :['<dl>','<dt class="title-date">','<span class="prevyear">prevyear</span><span class="prevmonth">prevmonth</span>','<span class="nextyear">nextyear</span><span class="nextmonth">nextmonth</span>','</dt>','<dt><strong>日</strong></dt>','<dt>一</dt>','<dt>二</dt>','<dt>三</dt>','<dt>四</dt>','<dt>五</dt>','<dt><strong>六</strong></dt>','<dd></dd>','</dl>'],// 初始化对象initialize :function (options) {this.id = options.id; // input的IDthis.input = _CalF.$('#'+ this.id); // 获取INPUT元素this.isSelect = options.isSelect; // 是否支持下拉SELECT选择年月,默认不显示this.inputEvent(); // input的事件绑定,获取焦点事件},// 创建日期最外层盒子,并设置盒子的绝对定位createContainer:function(){// 如果存在,则移除整个日期层Containervar odiv = _CalF.$('#'+ this.id + '-date');if(!!odiv) odiv.parentNode.removeChild(odiv);var container = this.container = document.createElement('div');container.id = this.id + '-date';container.style.position = "absolute";container.zIndex = 999;// 获取input表单位置inputPosvar input = _CalF.$('#' + this.id),inputPos = _CalF.getPos(input);//console.log(inputPos.top + ':' + inputPos.right + ' ' + inputPos.bottom + ' ' + inputPos.left);// 根据input的位置设置container高度container.style.left = inputPos.left + 'px';container.style.top = inputPos.bottom - 1 + 'px';// 设置日期层上的单击事件,仅供阻止冒泡,用途在日期层外单击关闭日期层_CalF.bind(container, 'click', _CalF.stopPropagation);document.body.appendChild(container);},// 渲染日期drawDate:function (odate) { // 参数odate 为日期对象格式var dateWarp, titleDate, dd, year, month, date, days, weekStart,i,l,ddHtml=[],textNode;var nowDate = new Date(),nowyear = nowDate.getFullYear(),nowmonth = nowDate.getMonth(),nowdate = nowDate.getDate();this.dateWarp = dateWarp = document.createElement('div');dateWarp.className = 'calendar';dateWarp.innerHTML = this._template.join('');this.year = year = odate.getFullYear();this.month = month = odate.getMonth()+1;this.date = date = odate.getDate();this.titleDate = titleDate = _CalF.$('.title-date', dateWarp)[0];// 是否显示SELECTif(this.isSelect){var selectHtmls =[];selectHtmls.push('<select>');for(i = 2020;i>1970;i--){if(i != this.year){selectHtmls.push('<option value ="'+ i +'">'+ i +'</option>');}else{selectHtmls.push('<option value ="'+ i +'" selected>'+ i +'</option>');}}selectHtmls.push('</select>');selectHtmls.push('年');selectHtmls.push('<select>');for(i = 1;i<13;i++){if(i != this.month){selectHtmls.push('<option value ="'+ i +'">'+ i +'</option>');}else{selectHtmls.push('<option value ="'+ i +'" selected>'+ i +'</option>');}}selectHtmls.push('</select>');selectHtmls.push('月');titleDate.innerHTML = selectHtmls.join('');// 绑定change事件this.selectChange();}else{textNode = document.createTextNode(year + '年' + month + '月');titleDate.appendChild(textNode);this.btnEvent();}// 获取模板中唯一的DD元素this.dd = dd = _CalF.$('dd',dateWarp)[0];// 获取本月天数days = new Date(year, month, 0).getDate();// 获取本月第一天是星期几weekStart = new Date(year, month-1,1).getDay();// 开头显示空白段for (i = 0; i < weekStart; i++) {ddHtml.push('<a> </a>');}// 循环显示日期for (i = 1; i <= days; i++) {if (year < nowyear) {ddHtml.push('<a class="live disabled">' + i + '</a>');} else if (year == nowyear) {if (month < nowmonth + 1) {ddHtml.push('<a class="live disabled">' + i + '</a>');} else if (month == nowmonth + 1) {if (i < nowdate) ddHtml.push('<a class="live disabled">' + i + '</a>');if (i == nowdate) ddHtml.push('<a class="live tody">' + i + '</a>');if (i > nowdate) ddHtml.push('<a class="live">' + i + '</a>');} else if (month > nowmonth + 1) {ddHtml.push('<a class="live">' + i + '</a>');}} else if (year > nowyear) {ddHtml.push('<a class="live">' + i + '</a>');}}dd.innerHTML = ddHtml.join('');// 如果存在,则先移除this.removeDate();// 添加this.container.appendChild(dateWarp);// A link事件绑定this.linkOn();// 区域外事件绑定this.outClick();},// SELECT CHANGE 事件selectChange:function(){var selects,yearSelect,monthSelect,that = this;selects = _CalF.$('select',this.titleDate);yearSelect = selects[0];monthSelect = selects[1];_CalF.bind(yearSelect, 'change',function(){var year = yearSelect.value;var month = monthSelect.value;that.drawDate(new Date(year, month-1, that.date));});_CalF.bind(monthSelect, 'change',function(){var year = yearSelect.value;var month = monthSelect.value;that.drawDate(new Date(year, month-1, that.date));})},// 移除日期DIV.calendarremoveDate:function(){var odiv = _CalF.$('.calendar',this.container)[0];if(!!odiv) this.container.removeChild(odiv);},// 上一月,下一月按钮事件btnEvent:function(){var prevyear = _CalF.$('.prevyear',this.dateWarp)[0],prevmonth = _CalF.$('.prevmonth',this.dateWarp)[0],nextyear = _CalF.$('.nextyear',this.dateWarp)[0],nextmonth = _CalF.$('.nextmonth',this.dateWarp)[0],that = this;prevyear.onclick = function(){var idate = new Date(that.year-1, that.month-1, that.date);that.drawDate(idate);};prevmonth.onclick = function(){var idate = new Date(that.year, that.month-2,that.date);that.drawDate(idate);};nextyear.onclick = function(){var idate = new Date(that.year + 1,that.month - 1, that.date);that.drawDate(idate);};nextmonth.onclick = function(){var idate = new Date(that.year , that.month, that.date);that.drawDate(idate);}},// A 的事件linkOn:function(){var links = _CalF.$('.live',this.dd),i,l=links.length,that=this;for(i = 0;i<l;i++){links[i].index = i;links[i].onmouseover = function(){_CalF.addClass("on",links[this.index]);};links[i].onmouseout = function(){_CalF.removeClass("on",links[this.index]);};links[i].onclick = function(){that.date = this.innerHTML;that.input.value = that.year + '-' + that.month + '-' + that.date;that.removeDate();}}},// 表单的事件inputEvent:function(){var that = this;_CalF.bind(this.input, 'focus',function(){that.createContainer();that.drawDate(new Date());});_CalF.bind(this.input, 'click',_CalF.stopPropagation);},// 鼠标在对象区域外点击,移除日期层outClick:function(){var that = this;_CalF.bind(document, 'click',function(){that.removeDate();})}};var myDate1 = new Calender({id:'j_Date1'});var myDate2 = new Calender({id:'j_Date2',isSelect:!0});</script></body></html>。
一个表单日期输入的JS脚本
document.write("<style type=text/css>");
document.write(" .ds_font td,span { font: normal 12px 宋体; color: #000000; }");
document.write(" .ds_border { border: 1px solid #000000; cursor: hand; background-color: #DDDDDD }");
document.write("<div style='position:absolute;display:none;text-align:center;width:0px;height:0px;overflow:visible' onselectstart='return false;'>");
{
month=1;
addYear(obj);
}
myObj[2].innerHTML=month;
document.write(" <table cellpadding=0 cellspacing=1 width=140 height=20 bgcolor=#CEDAE7 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>");
JS日期控件My97DatePicker基本用法
JS⽇期控件My97DatePicker基本⽤法My97DatePicker是⼀款⾮常灵活好⽤的⽇期控件。
使⽤⾮常简单。
1、下载My97DatePicker组件包2、在页⾯中引⼊该组件js⽂件:<script type="text/JavaScript" src="My97DatePicker/WdatePicker.js"></script>3、例⼦<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My97DatePicker⽇期控件使⽤</title></head><body><center><h2>My97DatePicker⽇期控件的使⽤</h2></center>基本⽤法:<input id="" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true})" /><br><br>只能选择今天以前的⽇期:<input id="" class="Wdate" onfocus="WdatePicker({readOnly:true,maxDate:'%y-%M-%d'})" /><br><br>使⽤运算表达式只能选择 20⼩时前⾄ 30⼩时后的⽇期<input id="" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',minDate:'%y-%M-%d {%H-20}:%m:%s',maxDate:'%y-%M-%d {%H+30}:%m:%s'})" /><br><br>开始,结束⽇期:<!-- $dp.$ 相当于 document.getElementById 函数. --><input id="sdate" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true,maxDate:'#F{$dp.$D(\'edate\')}'})" />-<input id="edate" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true,minDate:'#F{$dp.$D(\'sdate\')}',startDate:'#F{$dp.$D(\'sdate\',{d:+1})}'})" /><br><br> </body><script type="text/javascript" src="/js/My97DatePicker/WdatePicker.js"></script></html>如需修改配置信息,只需WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true,maxDate:'#F{$dp.$D(\'edate\')}'})" 加上相关配置信息即可4、常⽤的配置信息。
js日历控件
function TranYearMonthTitle(year,month){
with(new Date(year,month-1,1))
return "<span style='text-decoration:underline;cursor:hand;font-weight:bold;padding:1 2 0 1;width:40px;' onclick=showMore(1940,2050,this.innerHTML) onmouseover=\"this.runtimeStyle.cssText='color:#fff;'\" onmouseout=\"this.runtimeStyle.cssText=''\" onpropertychange=showC()>" + getYear() + "</span>" + "年" + "<span style='text-decoration:underline;cursor:hand;font-weight:bold;padding:1 2 0 1;width:20px;' onclick=showMore(1,12,this.innerHTML) onmouseover=\"this.runtimeStyle.cssText='color:#fff;'\" onmouseout=\"this.runtimeStyle.cssText=''\" onpropertychange=showC()>" + (getMonth()+1) + "</span>" + "月" ;
js获取日期(昨天、上一周、上一月、上一年或去年)
js获取⽇期(昨天、上⼀周、上⼀⽉、上⼀年或去年)下⾯代码是亲测且可⽤的,计算上⼀周时间确实费了不少劲,因为要考虑跨⽉和跨年的情况:$("#report").change(function () {var report = $(this).val();var beginTime = '';var endTime = '';var myDate = new Date();if (report == 'daily') { // 获取昨天myDate.setTime(myDate.getTime()-24*3600*1000);beginTime = myDate.getFullYear()+"-" + (myDate.getMonth()+1) + "-" + myDate.getDate();endTime = beginTime;} else if (report == 'weekly') {// 获取上⼀周的开始和结束时间var weekDate = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);// 计算开始时间⽤var weekDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);// 计算结束时间⽤var day = weekDate.getDay();var time = weekDate.getDate()-day+(day===0?-6:1);var startDate = new Date(weekDate.setDate(time));beginTime = startDate.getFullYear()+'-'+(startDate.getMonth()+1)+'-'+startDate.getDate();var endDate = new Date(weekDate2.setDate(time+6));endTime = endDate.getFullYear()+'-'+(endDate.getMonth()+1)+'-'+endDate.getDate();} else if (report == 'monthly') {// 获取上⼀⽉的开始和结束时间var year = myDate.getFullYear();var month = myDate.getMonth();if (month === 0) {month = 12;year = year-1;} else if (month < 10) {month = '0'+month;}var monthDate = new Date(year, month, 0);beginTime = year+'-'+month+'-01';endTime = year+'-'+month+'-'+monthDate.getDate();} else if (report == 'annual') {// 获取去年的开始和结束时间var year = myDate.getFullYear()-1;beginTime = year+'-01-01';endTime = year+'-12-31';}$("#begintime").val(beginTime);$("#endtime").val(endTime);});。
JS获取日期的方法实例【昨天,今天,明天,前n天,后n天的日期】
JS获取⽇期的⽅法实例【昨天,今天,明天,前n天,后n天的⽇期】本⽂实例讲述了JS获取⽇期的⽅法。
分享给⼤家供⼤家参考,具体如下:原理很简单,⼀天的时间的毫秒数是1000*60*60*24,前n天的⽇期就是现在⽇期换成毫秒-n*1000*60*60*24。
再把这个值换成⽇期即可(通过setTime⽅法)<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><title> ⽇期计算</title><meta name="Generator" content="EditPlus"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""></head><body><select id="num" name="num" style="width:150px" onchange="ff()"><option value="-1">昨天</option><option value="0">今天</option><option value="1">明天</option></select><br><textarea id="ar" rows="3" cols="20">haha</textarea><br/><input type="text" value="" name="bday" id="bday"/>天之前<input type="button" onclick="bday()" value="计算"><br/><input type="text" value="" name="aday" id="aday"/>天之后<input type="button" onclick="aday()" value="计算"><br/></body><script>ff();function ff(){var obj=document.getElementById("num");var v1=obj.options[obj.selectedIndex].text;var v2=obj.options[obj.selectedIndex].value;var tvv=giveDateStr(getTargetday(v2));document.getElementById('ar').innerText=tvv;}function isInteger(obj) {var r = /^\+?[1-9][0-9]*$/; //正整数return r.test(obj);}function bday(){var bef=document.getElementById("bday").value;if(isInteger(bef)){var bvv=giveDateStr(getTargetday(-bef));document.getElementById('ar').innerText=bvv;}else{alert("请输⼊正整数!");document.getElementById("bday").value='';document.getElementById("bday").focus();}}function aday(){var atf=document.getElementById("aday").value;if(isInteger(atf)){var avv=giveDateStr(getTargetday(atf));document.getElementById('ar').innerText=avv;}else{alert("请输⼊正整数!");document.getElementById("aday").value='';document.getElementById("aday").focus();}}function getTargetday(num){var number=null;if(num==null||num==0){number=0;}else{number=num;}var today=new Date();var targetday_milliseconds=today.getTime()+1000*60*60*24*number;var targetday=new Date();targetday.setTime(targetday_milliseconds); //注意,这⾏是关键代码return targetday;}function giveDateStr(dd){var riqiyue=dd.getMonth()+1;var riqihao=dd.getDate();var riqixingqi=null;if(dd.getDay()=='0'){xingqi="星期⽇";}else if(dd.getDay()=='1'){xingqi='星期⼀';}else if(dd.getDay()=='2'){xingqi='星期⼆';}else if(dd.getDay()=='3'){xingqi='星期三';}else if(dd.getDay()=='4'){xingqi='星期四';}else if(dd.getDay()=='5'){xingqi='星期五';}else if(dd.getDay()=='6'){xingqi='星期六';}var riqi=riqiyue+"⽉"+riqihao+"⽇"+"("+xingqi+")";return riqi;}</script></html>运⾏效果如下:PS:这⾥再为⼤家推荐⼏款⽐较实⽤的天数计算在线⼯具供⼤家使⽤:更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》希望本⽂所述对⼤家JavaScript程序设计有所帮助。
js日历控件
js⽇历控件document.write("<div id='meizzCalendarLayer' style='POSITION: absolute; z-index: 9999; width: 144px; height: 210px; display: none'>"); document.write("<iframe name=meizzCalendarIframe scrolling=no frameborder=0 width=100% height=100%></iframe></div>"); function document.onclick(){if(WebCalendar.eventSrc != window.event.srcElement) hiddenCalendar();}var WebCalendar = new WebCalendar();function WebCalendar() //初始化⽇历的设置{this.regInfo = "梅花⾬⽇历控件3.1(修正:⽔边) 关闭的快捷键:[Esc]";this.dayShow = 38; //定义页⾯上要显⽰的天数,不能⼩于35,或⼤于39this.daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//定义1~12⽉的天数this.day = new Array(this.dayShow); //定义⽇历展⽰⽤的数组this.dayObj = new Array(this.dayShow); //定义⽇期展⽰控件数组this.dateStyle = null; //保存格式化后⽇期数组this.objExport = null; //⽇历回传的显⽰控件this.eventSrc = null; //⽇历显⽰的触发控件this.inputDate = null; //转化外的输⼊的⽇期(d/m/yyyy)this.thisYear = new Date().getFullYear(); //定义年的变量的初始值this.thisMonth = new Date().getMonth()+ 1; //定义⽉的变量的初始值this.thisDay = new Date().getDate(); //定义⽇的变量的初始值this.today = this.thisDay +"/"+ this.thisMonth +"/"+ this.thisYear; //今天(d/m/yyyy)this.iframe = window.frames("meizzCalendarIframe"); //⽇历的 iframe 载体this.calendar = getObjectById("meizzCalendarLayer"); //⽇历的层this.dateReg = ""; //⽇历格式验证的正则式this.yearFall = 50; //定义显⽰的年份下拉框的年差值,如果今年是2000年,这⾥设置为50,就显⽰1950-2050this.format = "yyyy-mm-dd"; //回传⽇期的格式this.timeShow = false; //是否返回时间this.drag = true; //是否允许拖动this.darkColor = "#95B7F3"; //控件的暗⾊this.lightColor = "#FFFFFF"; //控件的亮⾊this.btnBgColor = "#E6E6FA"; //控件的按钮背景⾊this.wordColor = "#000080"; //控件的⽂字颜⾊this.wordDark = "#DCDCDC"; //控件的暗⽂字颜⾊this.dayBgColor = "#F5F5FA"; //⽇期数字背景⾊this.todayColor = "#FF0000"; //今天在⽇历上的标⽰背景⾊this.DarkBorder = "#D4D0C8"; //⽇期显⽰的⽴体表达⾊}function writeIframe(){var strIframe = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><style>"+"*{font-size: 12px; font-family: 宋体}"+".bg{ color: "+ WebCalendar.lightColor +"; cursor: default; background-color: "+ WebCalendar.darkColor +";}"+"table#tableMain{ width: 142; height: 180;}"+"table#tableWeek td{ width:14%;color: "+ WebCalendar.lightColor +";}"+"table#tableDay td{ width:14%;font-weight: bold;}"+"td#meizzYearHead, td#meizzYearMonth{color: "+ WebCalendar.wordColor +"}"+".out { text-align: center; border-top: 1px solid "+ WebCalendar.DarkBorder +"; border-left: 1px solid "+ WebCalendar.DarkBorder +";"+ "border-right: 1px solid "+ WebCalendar.lightColor +"; border-bottom: 1px solid "+ WebCalendar.lightColor +";}"+".over{ text-align: center; border-top: 1px solid #FFFFFF; border-left: 1px solid #FFFFFF;"+"border-bottom: 1px solid "+ WebCalendar.DarkBorder +"; border-right: 1px solid "+ WebCalendar.DarkBorder +"}"+"input{ border: 1px solid "+ WebCalendar.darkColor +"; padding-top: 1px; height: 18; cursor: hand;"+" color:"+ WebCalendar.wordColor +"; background-color: "+ WebCalendar.btnBgColor +"}"+"</style></head><body onselectstart='return false' style='margin: 0px' oncontextmenu='return false'><form name=meizz>";if (WebCalendar.drag){ strIframe += "<scr"+"ipt language=javascript>"+"var drag=false, cx=0, cy=0, o = parent.WebCalendar.calendar; function document.onmousemove(){"+"if(parent.WebCalendar.drag && drag){if(o.style.left=='')o.style.left=0; if(o.style.top=='')o.style.top=0;"+"o.style.left = parseInt(o.style.left) + window.event.clientX-cx;"+"o.style.top = parseInt(o.style.top) + window.event.clientY-cy;}}"+"function document.onkeydown(){ switch(window.event.keyCode){ case 27 : parent.hiddenCalendar(); break;"+"case 37 : parent.prevM(); break; case 38 : parent.prevY(); break; case 39 : parent.nextM(); break; case 40 : parent.nextY(); break;"+"case 84 : document.forms[0].today.click(); break;} window.event.keyCode = 0; window.event.returnValue= false;}"+"function dragStart(){cx=window.event.clientX; cy=window.event.clientY; drag=true;}</scr"+"ipt>"}strIframe += "<select name=tmpYearSelect onblur='parent.hiddenSelect(this)' style='z-index:1;position:absolute;top:3;left:18;display:none'"+" onchange='parent.WebCalendar.thisYear =this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+"<select name=tmpMonthSelect onblur='parent.hiddenSelect(this)' style='z-index:1; position:absolute;top:3;left:74;display:none'"+" onchange='parent.WebCalendar.thisMonth=this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+"<table id=tableMain class=bg border=0 cellspacing=2 cellpadding=0>"+"<tr><td width=140 height=19 bgcolor='"+ WebCalendar.lightColor +"'>"+" <table width=140 id=tableHead border=0 cellspacing=1 cellpadding=0><tr align=center>"+" <td width=15 height=19 class=bg title='向前翻 1 ⽉快捷键:←' style='cursor: hand' onclick='parent.prevM()'><b><</b></td>"+" <td width=60 id=meizzYearHead title='点击此处选择年份' onclick='parent.funYearSelect(parseInt(this.innerText, 10))'"+" onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+" onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+" <td width=50 id=meizzYearMonth title='点击此处选择⽉份' onclick='parent.funMonthSelect(parseInt(this.innerText, 10))'"+" onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+" onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+" <td width=15 class=bg title='向后翻 1 ⽉快捷键:→' onclick='parent.nextM()' style='cursor: hand'><b>></b></td></tr></table>"+ "</td></tr><tr><td height=20><table id=tableWeek border=1 width=140 cellpadding=0 cellspacing=0 ";if(WebCalendar.drag){strIframe += "onmousedown='dragStart()' onmouseup='drag=false' onmouseout='drag=false'";}strIframe += " borderColorLight='"+ WebCalendar.darkColor +"' borderColorDark='"+ WebCalendar.lightColor +"'>"+" <tr align=center><td height=20>⽇</td><td>⼀</td><td>⼆</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></table>"+"</td></tr><tr><td valign=top width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+" <table id=tableDay height=120 width=140 border=0 cellspacing=1 cellpadding=0>";for(var x=0; x<5; x++){strIframe += "<tr>";for(var y=0; y<7; y++)strIframe += "<td class=out id='meizzDay"+ (x*7+y) +"'></td>";strIframe += "</tr>";}strIframe += "<tr>";for(var x=35; x<WebCalendar.dayShow; x++)strIframe += "<td class=out id='meizzDay"+ x +"'></td>";strIframe +="<td colspan="+(42-WebCalendar.dayShow).toString()+" class=out style='text-align:center;' title='"+ WebCalendar.regInfo +"'>" +"<input style=' background-color: " + WebCalendar.btnBgColor +";cursor: hand; padding-top: 2px; width: 44%; height: 100%;' onfocus='this.blur()'"+" type=button value='清空' onclick='parent.WebCalendar.objExport.value=\"\";parent.hiddenCalendar()'>" +" " +"<input style=' background-color: " + WebCalendar.btnBgColor +";cursor: hand; padding-top: 2px; width: 44%; height: 100%;' onfocus='this.blur()'"+" type=button value='关闭' onclick='parent.hiddenCalendar()'>" +"</td></tr></table>"+"</td></tr><tr><td height=20 width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+" <table border=0 cellpadding=1 cellspacing=0 width=140>"+" <tr><td><input name=prevYear title='向前翻 1 年快捷键:↑' onclick='parent.prevY()' type=button value='<<'"+" onfocus='this.blur()' style='meizz:expression(this.disabled=parent.WebCalendar.thisYear==1000)'><input"+" onfocus='this.blur()' name=prevMonth title='向前翻 1 ⽉快捷键:←' onclick='parent.prevM()' type=button value='< '>"+" </td><td align=center><input name=today type=button value='Today' onfocus='this.blur()' style='width: 50' title='当前⽇期快捷键:T'"+" onclick=\"parent.returnDate(new Date().getDate() +'/'+ (new Date().getMonth() +1) +'/'+ new Date().getFullYear())\">"+" </td><td align=right><input title='向后翻 1 ⽉快捷键:→' name=nextMonth onclick='parent.nextM()' type=buttonvalue=' >'"+" onfocus='this.blur()'><input name=nextYear title='向后翻 1 年快捷键:↓' onclick='parent.nextY()' type=button value='>>'"+" onfocus='this.blur()' style='meizz:expression(this.disabled=parent.WebCalendar.thisYear==9999)'></td></tr></table>"+"</td></tr><table></form></body></html>";with(WebCalendar.iframe){document.writeln(strIframe); document.close();for(var i=0; i<WebCalendar.dayShow; i++){WebCalendar.dayObj[i] = eval("meizzDay"+ i);WebCalendar.dayObj[i].onmouseover = dayMouseOver;WebCalendar.dayObj[i].onmouseout = dayMouseOut;WebCalendar.dayObj[i].onclick = returnDate;}}}function calendar() //主调函数{var e = window.event.srcElement; writeIframe();var o = WebCalendar.calendar.style; WebCalendar.eventSrc = e;if (arguments.length == 0) WebCalendar.objExport = e;else WebCalendar.objExport = eval(arguments[0]);WebCalendar.iframe.tableWeek.style.cursor = WebCalendar.drag ? "move" : "default";var t = e.offsetTop, h = e.clientHeight, l = e.offsetLeft, p = e.type;while (e = e.offsetParent){t += e.offsetTop; l += e.offsetLeft;}o.display = ""; WebCalendar.iframe.document.body.focus();var cw = WebCalendar.calendar.clientWidth, ch = WebCalendar.calendar.clientHeight;var dw = document.body.clientWidth, dl = document.body.scrollLeft, dt = document.body.scrollTop;if (document.body.clientHeight + dt - t - h >= ch) o.top = (p=="image")? t + h : t + h + 6;else o.top = (t - dt < ch) ? ((p=="image")? t + h : t + h + 6) : t - ch;if (dw + dl - l >= cw) o.left = l; else o.left = (dw >= cw) ? dw - cw + dl : dl;if (!WebCalendar.timeShow) WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;else WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;try{if (WebCalendar.objExport.value.trim() != ""){WebCalendar.dateStyle = WebCalendar.objExport.value.trim().match(WebCalendar.dateReg);if (WebCalendar.dateStyle == null){WebCalendar.thisYear = new Date().getFullYear();WebCalendar.thisMonth = new Date().getMonth()+ 1;WebCalendar.thisDay = new Date().getDate();alert("原⽂本框⾥的⽇期有错误!\n可能与你定义的显⽰时分秒有冲突!");writeCalendar(); return false;}else{WebCalendar.thisYear = parseInt(WebCalendar.dateStyle[1], 10);WebCalendar.thisMonth = parseInt(WebCalendar.dateStyle[3], 10);WebCalendar.thisDay = parseInt(WebCalendar.dateStyle[4], 10);WebCalendar.inputDate = parseInt(WebCalendar.thisDay, 10) +"/"+ parseInt(WebCalendar.thisMonth, 10) +"/"+ parseInt(WebCalendar.thisYear, 10); writeCalendar();}} else writeCalendar();} catch(e){writeCalendar();}}function funMonthSelect() //⽉份的下拉框{var m = isNaN(parseInt(WebCalendar.thisMonth, 10)) ? new Date().getMonth() + 1 : parseInt(WebCalendar.thisMonth); var e = WebCalendar.iframe.document.forms[0].tmpMonthSelect;for (var i=1; i<13; i++) e.options.add(new Option(i +"⽉", i));e.style.display = ""; e.value = m; e.focus(); window.status = e.style.top;}function funYearSelect() //年份的下拉框{var n = WebCalendar.yearFall;var e = WebCalendar.iframe.document.forms[0].tmpYearSelect;var y = isNaN(parseInt(WebCalendar.thisYear, 10)) ? new Date().getFullYear() : parseInt(WebCalendar.thisYear);y = (y <= 1000)? 1000 : ((y >= 9999)? 9999 : y);var min = (y - n >= 1000) ? y - n : 1000;var max = (y + n <= 9999) ? y + n : 9999;min = (max == 9999) ? max-n*2 : min;max = (min == 1000) ? min+n*2 : max;for (var i=min; i<=max; i++) e.options.add(new Option(i +"年", i));e.style.display = ""; e.value = y; e.focus();}function prevM() //往前翻⽉份{WebCalendar.thisDay = 1;if (WebCalendar.thisMonth==1){WebCalendar.thisYear--;WebCalendar.thisMonth=13;}WebCalendar.thisMonth--; writeCalendar();}function nextM() //往后翻⽉份{WebCalendar.thisDay = 1;if (WebCalendar.thisMonth==12){WebCalendar.thisYear++;WebCalendar.thisMonth=0;}WebCalendar.thisMonth++; writeCalendar();}function prevY(){WebCalendar.thisDay = 1; WebCalendar.thisYear--; writeCalendar();}//往前翻 Year function nextY(){WebCalendar.thisDay = 1; WebCalendar.thisYear++; writeCalendar();}//往后翻 Year function hiddenSelect(e){for(var i=e.options.length; i>-1; i--)e.options.remove(i); e.style.display="none";} function getObjectById(id){ if(document.all) return(eval("document.all."+ id)); return(eval(id)); } function hiddenCalendar(){getObjectById("meizzCalendarLayer").style.display = "none";};function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}//⽇期⾃动补零程序function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}function dayMouseOver(){this.className = "over";this.style.backgroundColor = WebCalendar.darkColor;if(WebCalendar.day[this.id.substr(8)].split("/")[1] == WebCalendar.thisMonth)this.style.color = WebCalendar.lightColor;}function dayMouseOut(){this.className = "out"; var d = WebCalendar.day[this.id.substr(8)], a = d.split("/");this.style.removeAttribute('backgroundColor');if(a[1] == WebCalendar.thisMonth && d != WebCalendar.today){if(WebCalendar.dateStyle && a[0] == parseInt(WebCalendar.dateStyle[4], 10))this.style.color = WebCalendar.lightColor;this.style.color = WebCalendar.wordColor;}}function writeCalendar() //对⽇历显⽰的数据的处理程序{var y = WebCalendar.thisYear;var m = WebCalendar.thisMonth;var d = WebCalendar.thisDay;WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28;if (!(y<=9999 && y >= 1000 && parseInt(m, 10)>0 && parseInt(m, 10)<13 && parseInt(d, 10)>0)){alert("对不起,你输⼊了错误的⽇期!");WebCalendar.thisYear = new Date().getFullYear();WebCalendar.thisMonth = new Date().getMonth()+ 1;WebCalendar.thisDay = new Date().getDate(); }y = WebCalendar.thisYear;m = WebCalendar.thisMonth;d = WebCalendar.thisDay;WebCalendar.iframe.meizzYearHead.innerText = y +" 年";WebCalendar.iframe.meizzYearMonth.innerText = parseInt(m, 10) +" ⽉";WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28; //闰年⼆⽉为29天var w = new Date(y, m-1, 1).getDay();var prevDays = m==1 ? WebCalendar.daysMonth[11] : WebCalendar.daysMonth[m-2];for(var i=(w-1); i>=0; i--) //这三个 for 循环为⽇历赋数据源(数组 WebCalendar.day)格式是 d/m/yyyy {WebCalendar.day[i] = prevDays +"/"+ (parseInt(m, 10)-1) +"/"+ y;if(m==1) WebCalendar.day[i] = prevDays +"/"+ 12 +"/"+ (parseInt(y, 10)-1);prevDays--;}for(var i=1; i<=WebCalendar.daysMonth[m-1]; i++) WebCalendar.day[i+w-1] = i +"/"+ m +"/"+ y;for(var i=1; i<WebCalendar.dayShow-w-WebCalendar.daysMonth[m-1]+1; i++){WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ (parseInt(m, 10)+1) +"/"+ y;if(m==12) WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ 1 +"/"+ (parseInt(y, 10)+1);}for(var i=0; i<WebCalendar.dayShow; i++) //这个循环是根据源数组写到⽇历⾥显⽰{var a = WebCalendar.day[i].split("/");WebCalendar.dayObj[i].innerText = a[0];WebCalendar.dayObj[i].title = a[2] +"-"+ appendZero(a[1]) +"-"+ appendZero(a[0]);WebCalendar.dayObj[i].bgColor = WebCalendar.dayBgColor;WebCalendar.dayObj[i].style.color = WebCalendar.wordColor;if ((i<10 && parseInt(WebCalendar.day[i], 10)>20) || (i>27 && parseInt(WebCalendar.day[i], 10)<12))WebCalendar.dayObj[i].style.color = WebCalendar.wordDark;if (WebCalendar.inputDate==WebCalendar.day[i]) //设置输⼊框⾥的⽇期在⽇历上的颜⾊{WebCalendar.dayObj[i].bgColor = WebCalendar.darkColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;} if (WebCalendar.day[i] == WebCalendar.today) //设置今天在⽇历上反应出来的颜⾊{WebCalendar.dayObj[i].bgColor = WebCalendar.todayColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;} }}function returnDate() //根据⽇期格式等返回⽤户选定的⽇期{if(WebCalendar.objExport){var returnValue;var a = (arguments.length==0) ? WebCalendar.day[this.id.substr(8)].split("/") : arguments[0].split("/");var d = WebCalendar.format.match(/^(\w{4})(-|\/)(\w{1,2})\2(\w{1,2})$/);if(d==null){alert("你设定的⽇期输出格式不对!\r\n\r\n请重新定义 WebCalendar.format !"); return false;}var flag = d[3].length==2 || d[4].length==2; //判断返回的⽇期格式是否要补零returnValue = flag ? a[2] +d[2]+ appendZero(a[1]) +d[2]+ appendZero(a[0]) : a[2] +d[2]+ a[1] +d[2]+ a[0];if(WebCalendar.timeShow){var h = new Date().getHours(), m = new Date().getMinutes(), s = new Date().getSeconds();returnValue += flag ? " "+ appendZero(h) +":"+ appendZero(m) +":"+ appendZero(s) : " "+ h +":"+ m +":"+ s;}WebCalendar.objExport.value = returnValue;hiddenCalendar();}}-------将上⾯的保存到⼀个JS⽂件中,采⽤UTF-8编码---------------------------调⽤------------------<script language=javascript src="/JS/calendar.js"></script><input name="s1" type="text" onfocus="calendar()" value='<%=date %>' />。
JS日期和时间选择控件
tiannetPopCalendar();
}
//用户主调函数-选择日期和小时及分钟
function setDayHM(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
m_bolShowMinute = true;
//如果标签中有值,则将日期和小时及分钟初始化为当前值
// ---------------------- 用户可调用的函数 -----------------------------//
//用户主调函数-只选择日期
function setDay(obj){
tiannetOutObject = obj;
//如果标签中有值,则将日期初始化为当前值
tiannetYearSt = intDateBeg;
tiannetYearEnd = intDateEnd;
}
//设置日期分隔符。默认为"-"
function setDateSplit(strDateSplit){
tiannetDateSplit = strDateSplit;
}
* (4)设置(1),(2),(3)中的分隔符 setSplit(strDateSplit,strDateTimeSplit,strTimeSplit);
* (5)设置开始和结束年份 setYearPeriod(intDateBeg,intDateEnd)
*说明:
* 默认返回的日期时间格式如同:2005-02-02 08:08
var tiannetDateSplit = "-"; //日期的分隔符号
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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>';
strFrame+='<td style="font-size:12px;color:#FFFFFF">五</td><td style="font-size:12px;color:#FFFFFF">六</td></tr>';
strFrame+=' DateLayer.posTop += window.event.clientY-datelayery;}}';
strFrame+='function DragStart() /*開始日曆拖動*/';
strFrame+='{var DateLayer=parent.document.all.meizzDateLayer.style;';
strFrame+='<td width=48 align=center style="font-size:12px;cursor:default" onmouseover="style.backgroundColor=\'#FFD700\'" ';
strFrame+=' onmouseout="style.backgroundColor=\'white\'" onclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align=center>';
for (i=0;i<7;i++)
{
strFrame+='<td width=20 height=20 id=meizzDay'+n+' style="font-size:12px" onclick=parent.meizzDayClick(this.innerText,0)></td>';n++;}
strFrame+='onmouseover="style.backgroundColor=\'#FFD700\'" onmouseout="style.backgroundColor=\'white\'" ';
strFrame+='onclick="parent.tmpSelectYearInnerHTML(this.innerText.substring(0,4))" title="點擊這裏選擇年份"><span id=meizzYearHead></span></td>';
strFrame+=' onclick="parent.meizzPrevM()" title="向前翻 1 月"><b><</b>';
strFrame+=' </td><td width=60 align=center style="font-size:12px;cursor:default" ';
// 一、<input name=txt><input type=button value=setday onclick="setday(this,document.all.txt)">
// 二、<input onfocus="setday(this)">
// 本日曆的年份限制是(1000 - 9999)
strFrame+='</tr>';
}
strFrame+=' <tr align=center>';
strFrame+=' title="點擊這裏選擇月份"><span id=meizzMonthHead></span></td>';
strFrame+=' <td width=16 bgcolor=#C6D7F1 align=center style="font-size:12px;cursor: hand;color: #ffffff" ';
var bMoveable=true; //設置日曆是否可以拖動
var _VersionInfo="" //版本資訊
//==================================================== WEB 頁面顯示部分 =====================================================
strFrame+='<tr align=center valign=bottom><td style="font-size:12px;color:#FFFFFF">日</td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF">一</td><td style="font-size:12px;color:#FFFFFF">二</td>';
strFrame+='<span id=tmpSelectMonthLayer style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>';
strFrame+='<table border=1 cellspacing=0 cellpadding=0 width=142 height=160 bordercolor=#C6D7F1 bgcolor=#C6D7F1>';
strFrame+='<table border=1 cellspacing=0 cellpadding=0 bgcolor=#C6D7F1 ' + (bMoveable? 'onmousedown="DragStart()" onmouseup="DragEnd()"':'');
strFrame+=' BORDERCOLORLIGHT=#C6D7F1 BORDERCOLORDARK=#FFFFFF width=140 height=20 style="cursor:' + (bMoveable ? 'move':'default') + '">';
strFrame='<style>';
strFrame+='INPUT.button{BORDER-RIGHT: #C6D7F1 1px solid;BORDER-TOP: #C6D7F1 1px solid;BORDER-LEFT: #C6D7F1 1px solid;';