日期:2014-05-16  浏览次数:20307 次

js日历控件-calendar.js
calendar.js

var cal;
var isFocus=false; // 是否为焦点
var pickMode ={
    "second":1,
    "minute":2,
    "hour":3,
    "day":4,
    "month":5,
    "year":6  };
    
var topY=0,leftX=0;  // 自定义定位偏移量 2007-02-11 由 寒羽枫添加
// 选择日期 → 由 寒羽枫 2007-06-10 添加,通过 ID 来选日期
function SelectDateById(id,strFormat,x,y)
{
	var obj = document.getElementById(id);
	if(obj == null){return false;}
	obj.focus();
	if(obj.onclick != null){obj.onclick();}
	else if(obj.click != null){obj.click();}
	else{SelectDate(obj,strFormat,x,y)}
}

// 选择日期 → 由 寒羽枫 2006-06-25 添加
function SelectDate(obj,strFormat,x,y)
{

	leftX =(x == null) ? leftX : x;
	topY  =(y == null) ? topY : y;// 自定义定位偏移量 2007-02-11 由 寒羽枫添加
	if(document.getElementById("ContainerPanel")==null){InitContainerPanel();}
    var date = new Date();
    var by = date.getFullYear()-50;  // 最小值 → 50 年前
    var ey = date.getFullYear()+50;  // 最大值 → 50 年后
    // cal = new Calendar(by, ey,1,strFormat); //初始化英文版,0 为中文版
    cal = (cal==null) ? new Calendar(by, ey, 0) : cal;    // 不用每次都初始化
															// 2006-12-03 修正
    cal.DateMode =pickMode["second"]; // 复位
      if(strFormat.indexOf('s')< 0) {cal.DateMode =pickMode["minute"];}// 精度为分
      if(strFormat.indexOf('m')< 0) {cal.DateMode =pickMode["hour"];}// 精度为时
      if(strFormat.indexOf('h')< 0) {cal.DateMode =pickMode["day"];}// 精度为日
      if(strFormat.indexOf('d')< 0) {cal.DateMode =pickMode["month"];}// 精度为月
      if(strFormat.indexOf('M')< 0) {cal.DateMode =pickMode["year"];}// 精度为年
      if(strFormat.indexOf('y')< 0) {cal.DateMode =pickMode["second"];}// 默认精度为秒
    cal.dateFormatStyleOld = cal.dateFormatStyle;
    cal.dateFormatStyle = strFormat;
    cal.show(obj);
}
/**//**//**//**//**//**//**//**
							 * 返回日期
							 * 
							 * @param d
							 *            the delimiter
							 * @param p
							 *            the pattern of your date 2006-06-25 由
							 *            寒羽枫 修改为根据用户指定的 style 来确定;
							 */
String.prototype.toDate = function(style) {
  var y = this.substring(style.indexOf('y'),style.lastIndexOf('y')+1);// 年
  var M = this.substring(style.indexOf('M'),style.lastIndexOf('M')+1);// 月
  var d = this.substring(style.indexOf('d'),style.lastIndexOf('d')+1);// 日
  var h = this.substring(style.indexOf('h'),style.lastIndexOf('h')+1);// 时
  var m = this.substring(style.indexOf('m'),style.lastIndexOf('m')+1);// 分
  var s = this.substring(style.indexOf('s'),style.lastIndexOf('s')+1);// 秒

  if(s == null ||s == "" || isNaN(s)) {s = new Date().getSeconds();}
  if(m == null ||m == "" || isNaN(m)) {m = new Date().getMinutes();}
  if(h == null ||h == "" || isNaN(h)) {h = new Date().getHours();}
  if(d == null ||d == "" || isNaN(d)) {d = new Date().getDate();}
  if(M == null ||M == "" || isNaN(M)) {M = new Date().getMonth()+1;}
  if(y == null ||y == "" || isNaN(y)) {y = new Date().getFullYear();}
  var dt ;
  eval ("dt = new Date('"+ y+"', '"+(M-1)+"','"+ d+"','"+ h+"','"+ m+"','"+ s +"')");
  return dt;
}

/**//**//**//**//**//**//**//**
							 * 格式化日期
							 * 
							 * @param d
							 *            the delimiter
							 * @param p
							 *            the pattern of your date
							 * @author meizz
							 */
Date.prototype.format = function(style) {
  var o = {
    "M+" : this.getMonth() + 1, // month
    "d+" : this.getDate(),      // day
    "h+" : this.getHours(),     // hour
    "m+" : this.getMinutes(),   // minute
    "s+" : this.getSeconds(),   // second
    "w+" : "天一二三四五六".charAt(this.getDay()),   // week
    "q+" : Math.floor((this.getMonth() + 3) / 3),  // quarter
    "S"  : this.getMilliseconds() // millisecond
  }
  if(/(y+)/.test(style)) {
    style = style.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for(var k in o){
    if(new RegExp("("+ k +")").test(style)){
      style = style.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return st