JS脚本的日历控件

<HEAD>
<title>联动案设置</title>

<script src="Calendar.js" type="text/javascript"></script>


< script  language ="javascript"  src ="Calendar.js" ></ script >


</HEAD>
调用方法



    
< TD  style ="HEIGHT: 25px" >< FONT  face ="宋体" > 开业时间:
                                                    
< asp:textbox  id ="dtOpenTime"  runat ="server"  Width ="133px"  visible ="True" ></ asp:textbox >< INPUT  class ="but"  id ="btndtOpenTimeDate"  style ="WIDTH: 25px; CURSOR: hand; HEIGHT: 24px"
                                                        onclick
="javascript:Cal_dropdown(dtOpenTime)"  type ="button"  value ="..."  name ="btndtOpenTimeDate" ></ FONT ></ TD >



    

脚本文件 Calendar.js

/* ------------------------------------------------------------------------
  日期控件
  function Cal_dropdown(edit,min,max)
    弹出日历,可不给参数min和max,参数edit必须有   

  function Cal_datevalid(edit,min,max)
    检查edit中值是否为大于等于min,小于等于max的有效日期格式字符串。
    是则返回 true,否则返回false
    可不给参数min和max(字符串格式)
    参数edit必须有,如果edit无,则必须是:edit为edit和img的父亲(如span、div)的第一个元素
    
*/


var  Cal_popup  =  window.createPopup();
var  Cal_edit;
var  Cal_editdate  =   new  Date();
var  Cal_maxdate;
var  Cal_mindate;

function  Cal_clearTime(thedate)
{
  thedate.setHours(
0 );
  thedate.setMinutes(
0 );
  thedate.setSeconds(
0 );
  thedate.setMilliseconds(
0 );
}


var  Cal_today  =   new  Date();
Cal_clearTime(Cal_today);

// 检查日期//
//
editctrl:日期控件输入框
//
 false: 日期错误

function  ChkCZDate(editctrl)
{
if (editctrl.value == '' )
{ alert( ' 请输入日期! ' )
  editctrl.select();
  editctrl.focus();
  
return   false ;}

if ( ! Cal_datevalid(editctrl, ' 1910-1-1 ' , ' 3000-1-1 ' ))
{
alert(
' 日期格式不正确,日期有效范围为1910年到3000年 ' );
editctrl.focus();
return   false ;
}

else
 
return   true ;
}

 
 

 

function  Cal_decDay(thedate,days)
{
  
if  ( ! days) days  =   1 ;
  thedate.setTime(thedate 
-  days * 24 * 60 * 60 * 1000 );
}


function  Cal_incMonth(year,month)
{
  
if  (month  ==   11 {
    month 
=   0 ;
    year
++ ;
  }
  else  month ++ ;
  Cal_writeHTML(year,month);
}


function  Cal_decMonth(year,month)
{
  
if  (month  ==   0 {
    month 
=   11 ;
    year
-- ;
  }
  else  month -- ;
  Cal_writeHTML(year,month);
}


function  Cal_decYear(year,month)
{
  Cal_writeHTML(year
- 1 ,month);
}


function  Cal_incYear(year,month)
{
  Cal_writeHTML(year
+ 1 ,month);
}


function  Cal_writeHTML(theyear,themonth)
{
  
var  html =
    
' <div id="Cal_div1" style="width:231px;FONT-SIZE:9pt;background-color:#fffef5;border:black 1px solid"> ' +
    
' <TABLE style="border-bottom:black 1px solid;FONT-SIZE: 9pt;background-color:#0fa8c7;color:white; ' +
    
' padding-top:2px;font-weight:bold;text-align:center"  ' +
    
' cellSpacing="0" cellPadding="0" width="100%" border="0"> ' +
    
' <TR><TD style="cursor:hand" align="left" width=24 οnmοuseοver="this.style.background= '   +
    
" '#ffb468'; "   +   ' " '   +   '  οnmοuseοut="this.style.background= '   +   " '#0fa8c7'; "   +   ' " '   +
    
'  οnclick="parent.Cal_decYear( '   +  theyear  +   ' , '   +  themonth  +   ' );"  ' +
    
' ><年</TD> ' +
    
' <TD style="cursor:hand" align="left" width=24 οnmοuseοver="this.style.background= '   +
    
" '#ffb468'; "   +   ' " '   +   '  οnmοuseοut="this.style.background= '   +   " '#0fa8c7'; "   +   ' " '   +
    
'  οnclick="parent.Cal_decMonth( '   +  theyear  +   ' , '   +  themonth  +   ' );"  ' +
    
' ><月</TD> ' +
    
' <TD align="center"> ' ;
    
  html 
+=  theyear  +   ' '   +  (themonth  +   1 +   ' 月</TD> ' +
    
' <TD style="cursor:hand" align="right" width=24 οnmοuseοver="this.style.background= '   +
    
" '#ffb468'; "   +   ' " '   +   '  οnmοuseοut="this.style.background= '   +   " '#0fa8c7'; "   +   ' " '   +
    
'  οnclick="parent.Cal_incMonth( '   +  theyear  +   ' , '   +  themonth  +   ' );"  ' +
    
' >月></TD> ' +
    
' <TD style="cursor:hand" align="right" width=24 οnmοuseοver="this.style.background= '   +
    
" '#ffb468'; "   +   ' " '   +   '  οnmοuseοut="this.style.background= '   +   " '#0fa8c7'; "   +   ' " '   +
    
'  οnclick="parent.Cal_incYear( '   +  theyear  +   ' , '   +  themonth  +   ' );"  ' +
    
' >年></TD> ' ;
    
  html 
+=
    
' </TR></TABLE> ' +
    
' <TABLE style="FONT-SIZE: 9pt;font-weight:bold;text-align:center;border-bottom:black 1px solid"  ' +
    
' cellSpacing="2" cellPadding="0" width="100%" border="0"> ' +
    
' <TR><TD>日</TD><TD>一</TD><TD>二< /TD><TD>三</TD><TD>四</TD><TD>五< /TD><TD>六</TD> ' +
    
' </TR></table> ' +
    
' <TABLE style="FONT-SIZE: 9pt;text-align:center;cursor:hand" cellSpacing="2"  ' +
    
' cellPadding="0" width="100%" border="0"> ' ;

  
var  day1  =   new  Date(theyear,themonth, 1 );
  
if  (day1.getDay() != 0 )
  Cal_decDay(day1,day1.getDay()); 
//  日历开始日
   for  ( var  i = 1 ;i <= 6 ;i ++ {
    html 
+=   ' <TR> ' ;
    
for  ( var  j = 1 ;j <= 7 ;j ++ {
      html 
+=   ' <TD ' ;
      
if  (day1.getTime() == Cal_today.getTime())
        html 
+=   '  style="color:blue" ' ;
      
else
      
if  (day1.getTime() == Cal_editdate.getTime())
        html 
+=   '  style="color:red" ' ;
      
else
      
if  (day1.getMonth()  !=  themonth)
        html 
+=   '  style="color:#aaaaaa" ' ;
      html 
+=   '  οnmοuseοver="this.style.background= '   +
              
" '#ffb468'; "   +   ' " ' +
              
'  οnmοuseοut="this.style.background= '   +
              
" '#fffef5'; "   +   ' " ' ;
      html 
+=   '  οnclick="parent.Cal_clickday( ' + day1.getTime()  +   ' );" ' ;
      html 
+= ' > '   +  day1.getDate()  +   ' </TD> ' ;
      Cal_decDay(day1,
- 1 );
    }

    html 
+=   ' </TR> ' ;
    
if  (day1.getMonth()  !=  themonth)  break ;
  }

    
  html 
+=
    
' </TABLE> ' +
    
' <div style="border-top:black 1px solid;text-align:center;padding:2px">今天是  ' +
    
' <span style="color:blue;cursor:hand;text-decoration:underline" οnclick="javascript:parent.Cal_clickday( ' +
    Cal_today.getTime() 
+   ' );"> ' +
    Cal_today.getFullYear() 
+   ' - '   +  (Cal_today.getMonth() + 1 +   ' - '   +  Cal_today.getDate()  +
    
' </span></div> ' +
    
' </div> ' ;
  Cal_popup.document.body.innerHTML 
=  html;
  
if  (Cal_popup.isOpen)  //  重新调整显示高度
    Cal_popup.show( 0 , Cal_edit.offsetHeight,  231 , Cal_popup.document.all( " Cal_div1 " ).offsetHeight,Cal_edit);
}


//  字符串转换为日期
function  Cal_strtodate(str)
{
  
var  date  =  Date.parse(str);
  
if  (isNaN(date))  {
    date 
=  Date.parse(str.replace( /-/ g, " / " ));  //  识别日期格式:YYYY-MM-DD
     if  (isNaN(date)) date  =   0 ;
  }

  
return (date);
}


// 返回日期间相差的天数
function  Cal_DateDiff(Date1, Date2)
{
    
return  (Date2 - Date1) / ( 24 * 60 * 60 * 1000 );
}


// 返回日期间相差的月数(最大误差小于一个月)
function  Cal_MonthDiff(DateA, DateB)
{
    Date1
= new  Date();
    Date2
= new  Date();
    Date1.setTime(DateA);
    Date2.setTime(DateB);
    months 
=  (Date2.getFullYear()  -  Date1.getFullYear())  *   12 ;
    addmonths 
=  Date2.getMonth()  -  Date1.getMonth();
    months 
=  months  +  addmonths;
    
if (Date2.getDate()  <  Date1.getDate())
        months
-- ;
    
return  months;
}


//  弹出日历,可不给参数min和max,参数edit必须有
function  Cal_dropdown(edit,min,max)  {
  
if  ( ! edit)  {
    edit 
=  window.event.srcElement.parentElement.children( 0 );
    
if  (( ! edit.type)  ||  (edit.type.toLowerCase()  !=   " text " ))  return ;
  }

  Cal_edit 
=  edit;
  
var  date  =  Cal_strtodate(edit.value);
  
if  (date  ==   0 ) date  =  Cal_today.getTime();
  
if  (max) Cal_maxdate  =  Cal_strtodate(max);
  
else  Cal_maxdate = 0 ;
  
if  (min) Cal_mindate  =  Cal_strtodate(min);
  
else  Cal_mindate  =   0 ;
  Cal_editdate.setTime(date);
  Cal_writeHTML(Cal_editdate.getFullYear(),Cal_editdate.getMonth());
  Cal_popup.show(
0 , edit.offsetHeight,  231 149 ,edit);
  Cal_popup.show(
0 , edit.offsetHeight,  231 , Cal_popup.document.all( " Cal_div1 " ).offsetHeight,edit);
}


//  点击日期
function  Cal_clickday(day,edit)
{
  
if  (Cal_maxdate  !=   0 ) day  =  Math.min(day,Cal_maxdate);
  day 
=  Math.max(day,Cal_mindate);
  Cal_editdate.setTime(day);
  
var  im;
  
var  id;
  
if  ((Cal_editdate.getMonth() + 1 ) < 10 )
  
{
   im 
=  Cal_editdate.getMonth() + 1 ;
   im 
=   ' 0 '   +  im;
  }

  
else
   im 
=  Cal_editdate.getMonth() + 1 ;
  
if  ((Cal_editdate.getDate()) < 10 )
   id 
=   " 0 " + Cal_editdate.getDate();
  
else
   id 
=  Cal_editdate.getDate();
  Cal_edit.value 
=  Cal_editdate.getFullYear()  +   " - "   +  im  +   " - "
                   
+  id;
  Cal_popup.hide();
  Cal_edit.fireEvent(
" onkeydown " );
  Cal_edit.focus();
}


function  Cal_datevalid(edit,min,max)
{
  
//  检查edit中值是否为大于等于min,小于等于max的有效日期格式字符串。
 
  
var  date  =  Cal_strtodate(edit.value);
  
if  (date  ==   0 return   false ;
  
if  (max)  {
    
var  max  =  Cal_strtodate(max);
    
if  ((max != 0 ) && (date > max))  return   false ;
  }

  
if  (min)  {
    
var  min  =  Cal_strtodate(min);
    
if  ((min != 0 ) && (date < min))  return   false ;
  }

  date 
=   new  Date(date);
  edit.value 
=  date.getFullYear()  +   " - "   +  (date.getMonth() + 1 +
               
" - "   +  date.getDate();
  
return   true ;
}

 

                           
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值