---
title: "有关js的日历控件大全"
author: "abeetle"
date: "2007-08-07T15:25:00Z"
tags: []
slug: "js"
draft: false
lastmod: "2007-08-07T15:25:00Z"
id: 233851
url: "https://abeetle.iteye.com/blog/233851"
---

经常会用到日历控件，现在就整理一下，好看即好用的日历控件

**第一种方案**

1.建立一个JS文件：Calendar.js

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!--  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)var cal;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)var isFocus=false; //是否为焦点  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//function SelectDate(obj,strFormat) //两个参数改为只传一个  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)function SelectDate(obj)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var date = new Date();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var by = date.getFullYear()-10; //最小值 → 10 年前  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var ey = date.getFullYear()+10; //最大值 → 10 年后  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    cal = (cal==null) ? new Calendar(by, ey, 0) : cal;    //初始化为中文版，1为英文版  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //cal.dateFormatStyle = strFormat; // 默认显示格式为:yyyy-MM-dd ,还可显示 yyyy/MM/dd  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    cal.show(obj);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//* 返回日期 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)String.prototype.toDate = function(style)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)var y = this.substring(style.indexOf('y'),style.lastIndexOf('y')+1);//年  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)var m = this.substring(style.indexOf('M'),style.lastIndexOf('M')+1);//月  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)var d = this.substring(style.indexOf('d'),style.lastIndexOf('d')+1);//日  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(isNaN(y)) y = new Date().getFullYear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(isNaN(m)) m = new Date().getMonth();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(isNaN(d)) d = new Date().getDate();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)var dt ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)eval ("dt = new Date('"+ y+"', '"+(m-1)+"','"+ d +"')");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return dt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//* 格式化日期 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)Date.prototype.format = function(style)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)var o = ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "M+" : this.getMonth() + 1, //month  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "d+" : this.getDate(),      //day  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "h+" : this.getHours(),     //hour  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "m+" : this.getMinutes(),   //minute  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "s+" : this.getSeconds(),   //second  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "w+" : "天一二三四五六".charAt(this.getDay()),   //week  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "q+" : Math.floor((this.getMonth() + 3) / 3), //quarter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    "S" : this.getMilliseconds() //millisecond  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(/(y+)/.test(style))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    style = style.replace(RegExp.$1,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(var k in o)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if(new RegExp("("+ k +")").test(style))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      style = style.replace(RegExp.$1,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        RegExp.$1.length == 1 ? o\[k] :  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ("00" + o\[k]).substr(("" + o\[k]).length));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return style;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)* 日历类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)* @param   beginYear 1990  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)* @param   endYear   2010  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)* @param   lang      0(中文)|1(英语) 可自由扩充  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)* @param   dateFormatStyle "yyyy-MM-dd";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)function Calendar(beginYear, endYear, lang, dateFormatStyle)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.beginYear = 1990;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.endYear = 2010;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.lang = 0;            //0(中文) | 1(英文)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.dateFormatStyle = "yyyy-MM-dd";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if (beginYear != null && endYear != null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.beginYear = beginYear;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.endYear = endYear;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if (lang != null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.lang = lang  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if (dateFormatStyle != null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.dateFormatStyle = dateFormatStyle  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.dateControl = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.panel = this.getElementById("calendarPanel");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.container = this.getElementById("ContainerPanel");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.form = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.date = new Date();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.year = this.date.getFullYear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.month = this.date.getMonth();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)this.colors = ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"cur\_word"      : "#FFFFFF", //当日日期文字颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"cur\_bg"        : "#83A6F4", //当日日期单元格背影色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"sel\_bg"        : "#FFCCCC", //已被选择的日期单元格背影色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"sun\_word"      : "#FF0000", //星期天文字颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"sat\_word"      : "#0000FF", //星期六文字颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"td\_word\_light" : "#333333", //单元格文字颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"td\_word\_dark" : "#CCCCCC", //单元格文字暗色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"td\_bg\_out"     : "#EFEFEF", //单元格背影色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"td\_bg\_over"    : "#FFCC00", //单元格背影色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"tr\_word"       : "#FFFFFF", //日历头文字颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"tr\_bg"         : "#666666", //日历头背影色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"input\_border&lt;sp&gt;