search
HomeWeb Front-endJS TutorialJS Calendar Recommendation_Time and Date

I wrote a calendar two years ago, but the compatibility was not good. I re-wrote it this time.
Compatible with multiple browsers
I learned a lot about it, especially about W3C standardization.
For example, FF and IE have different understandings of the box model
box.style{width:100;border 1px;}
ie understands it as box.width = 100
ff understands it as box.width = 100 1*2 = 102
You can use this method to allow both browsers to browse normally
box.style{width:100!important; width /**/:120px;border 1px;}
Note width (space)/**/
JS Calendar Recommendation_Time and Date Download this file

Copy the code The code is as follows:

/*********************** 

* 创建对象  var c = new Calendar("c"); document.write(c); 
* 调用方法 c.show(arg1,arg2,arg3) 
* 参数1: 文本输入框(必填). 如 onfocus="c.show(this)"; 
* 参数2: 按钮或其它可用单击事件的HTML元素(如果使用按钮方式则必填). 
     如 onclick="c.show(this,c.getObjById(*))" *=文本输入框名称 
* 参数3: 如果没有文本框没有值则使用该值初始化日历(选填). 
     如 onfocus="c.show(this,'2006-01-01') 

* 注: 参数顺序不分先后. MSIE6/Opera8/FireFox1.5 下测试通过 

  

***如果您使用本日历控件 请保留该信息 谢谢! *****  
* http://2lin.net 
* Email:caoailin@gmail.com 
* QQ:38062022 
* Creation date: 2006-11-22 
************************************/ 

function Calendar(objName)  

this.style = { 

  borderColor    : "#909eff", //边框颜色 
  headerBackColor   : "#909EFF", //表头背景颜色 
  headerFontColor   : "#ffffff", //表头字体颜色 
  bodyBarBackColor  : "#f4f4f4", //日历标题背景色 
  bodyBarFontColor  : "#000000", //日历标题字体色 
  bodyBackColor   : "#ffffff", //日历背景色 
  bodyFontColor      : "#000000", //日历字体色 
  bodyHolidayFontColor   : "#ff0000", //假日字体色 
  watermarkColor   : "#d4d4d4", //背景水印色 
  moreDayColor       : "#cccccc" 

}; 

this.showMoreDay = false; //是否显示上月和下月的日期 

this.Obj = objName; 
this.date = null; 

this.mouseOffset = null; 
this.dateInput = null; 
this.timer = null; 

}; 

Calendar.prototype.toString = function() 
{  
  var str = this.getStyle(); 
  str += '
\n'; 
  str += '
'; 
  str += this.getHeader(); 
  str += this.getBody();  
  str += '
'; 
  return str; 
}; 

Calendar.prototype.getStyle = function() 

  var str = 'n'; 
return str; 

}; 

Calendar.prototype.getHeader = function() 

  var str = 'n'; 
str  = 'n'; 
str  = 'n'; 
str  = 'n'; 
str  = 'n'; 
str  = '
n'; 
str  = '
n'; 
str  = '
00>>>
n'; 
return str; 
}; 

Calendar.prototype.getBody = function() 

  var n = 0; 
  var str = this.getBodyBar(); 
  str  = 'n';  
  for(i = 0; i {   
  str  = ''; 
for(j = 0; j { 
str  = 'n'; 

str  = ''; 

  str  = '
n'; 
  str  = '
今天:' new Date().toFormatString("yyyy年mm月dd日") '
n'; 
  return str; 
}; 

Calendar.prototype.getBodyBar = function() 

  var str = 'n'; 
  var day = new Array('日','一','二','三','四','五','六'); 
  for(i = 0; i   { 
   str  = 'n';   
  } 
  str  = '
'   day[i]   '
'; 
  return str;  


Calendar.prototype.getYearMenu = function(year) 


  var str = 'n'; 
  for(i = 0; i {   
  var _year = year   i; 
var _date = new Date(_year,this.date.getMonth(),this.date.getDate()); 

  str  = 'n'; 
str  = ''; 


  str  = 'n'; 
  str  = '
if(this.date.getFullYear() != _year) 

str  = 'onmouseover="this.className='menuOver'" onmouseout="this.className=''" '; 

else 

  str  = 'class="menuOver"'; 

str  = 'onclick="'   this.Obj   '.bindDate(''   _date.toFormatString("-")   '')">'   _year   '年
n'; 
  str  = 'n'; 
  str  = '
n'; 
  str  = '
>>
'; 

  var _menu = getObjById("cdrMenu"); 
_menu.innerHTML = str; 

}; 

Calendar.prototype.getMonthMenu = function() 


  var str = 'n'; 
  for(i = 1; i {  
var _date = new Date(this.date.getFullYear(),i-1,this.date.getDate()); 

str  = 'n'; 

  str  = '
if(this.date.getMonth()   1 != i) 

str  = 'onmouseover="this.className='menuOver'" onmouseout="this.className=''" '; 

else 

  str  = 'class="menuOver"'; 

str  = 'onclick="'   this.Obj   '.bindDate(''   _date.toFormatString("-")   '')">' i '月
'; 

  var _menu = getObjById("cdrMenu"); 
_menu.innerHTML = str;  
}; 

Calendar.prototype.show = function() 

  if (arguments.length > 3 || arguments.length == 0) 
  { 
   alert("对不起!传入参数不对!" ); 
 return; 
}  

  var _date = null; 
  var _evObj = null; 
  var _initValue = null  

  for(i = 0; i   { 
   if(typeof(arguments[i]) == "object" && arguments[i].type == "text") 
  {_date = arguments[i];} 
 else if(typeof(arguments[i]) == "object") 
  {_evObj = arguments[i];} 
 else if(typeof(arguments[i]) == "string") 
  {_initValue = arguments[i];}  
  } 
  _evObj = _evObj || _date; 
inputObj = _date; 
targetObj = _evObj 

if(!_date){alert("传入参数错误!"); return;} 

  this.dateInput = _date; 

  _date = _date.value; 

  if(_date == "" && _initValue) _date = _initValue;  

  this.bindDate(_date);     

  var _target = getPosition(_evObj);  
  var _obj = getObjById("Calendar"); 
  _obj.style.display = ""; 

  
  _obj.style.left = _target.x   'px'; 
  if((document.body.clientHeight - (_target.y   _evObj.clientHeight)) >= _obj.clientHeight) 
  {     
   _obj.style.top = (_target.y   _evObj.clientHeight)   'px'; 

else 
{   
    _obj.style.top = (_target.y - _obj.clientHeight)   'px'; 


}; 


Calendar.prototype.hide = function() 

  var obj = getObjById("Calendar"); 
obj.style.display = "none";  
}; 

Calendar.prototype.bindDate = function(date) 


var _monthDays = new Array(31,30,31,30,31,30,31,31,30,31,30,31); 
var _arr = date.split('-'); 
  var _date = new Date(_arr[0],_arr[1]-1,_arr[2]); 
if(isNaN(_date)) _date = new Date(); 
this.date = _date; 
this.bindHeader(); 

var _year = _date.getFullYear(); 
var _month = _date.getMonth(); 
var _day = 1; 

var _startDay = new Date(_year,_month,1).getDay(); 
var _previYear = _month == 0 ? _year - 1 : _year; 
var _previMonth = _month == 0 ? 11 : _month - 1; 
var _previDay = _monthDays[_previMonth]; 
if (_previMonth == 1) _previDay =((_previYear%4==0)&&(_previYear0!=0)||(_previYear@0==0))?29:28; 
_previDay -= _startDay - 1; 
var _nextDay = 1; 

_monthDays[1] = ((_year%4==0)&&(_year0!=0)||(_year@0==0))?29:28; 

for(i = 0; i { 
  var _dayElement = getObjById("cdrDay"   i); 

_dayElement.onmouseover = Function(this.Obj   ".onMouseOver(this)"); 
_dayElement.onmouseout = Function(this.Obj   ".onMouseOut(this)"); 
_dayElement.onclick = Function(this.Obj   ".onClick(this)"); 
this.onMouseOut(_dayElement); 
_dayElement.style.color = ""; 

if(i { 
  //获取上一个月的日期 
if(this.showMoreDay) 

  var _previDate = new Date(_year,_month - 1,_previDay); 
  _dayElement.innerHTML = _previDay; 
  _dayElement.title = _previDate.toFormatString("yyyy年mm月dd日"); 
    _dayElement.value = _previDate.toFormatString("-"); 
  _dayElement.style.color = this.style.moreDayColor; 

    _previDay ; 

}else 

  _dayElement.innerHTML = ""; 
    _dayElement.title = ""; 



else if(_day > _monthDays[_month]) 

  //获取下个月的日期 
  if(this.showMoreDay) 

 var _nextDate = new Date(_year,_month   1,_nextDay); 
 _dayElement.innerHTML = _nextDay; 
 _dayElement.title = _nextDate.toFormatString("yyyy年mm月dd日"); 
   _dayElement.value = _nextDate.toFormatString("-"); 
 _dayElement.style.color = this.style.moreDayColor; 

   _nextDay ;   
}else 

  _dayElement.innerHTML = ""; 
    _dayElement.title = ""; 


else if(i >= new Date(_year,_month,1).getDay() && _day { 
      //获取本月日期 
  _dayElement.innerHTML = _day; 

  if(_day == _date.getDate()) 
  { 
   this.onMouseOver(_dayElement); 
 _dayElement.onmouseover = Function("");  
   _dayElement.onmouseout = Function("");      
  } 

   
  if(this.isHoliday(_year,_month,_day)) 
  { 
   _dayElement.style.color = this.style.bodyHolidayFontColor;   
  } 
  var _curDate = new Date(_year, _month, _day); 
  _dayElement.title = _curDate.toFormatString("yyyy年mm月dd日"); 
  _dayElement.value = _curDate.toFormatString("-"); 

  _day ; 


else 

  _dayElement.innerHTML = ""; 
  _dayElement.title = ""; 




var _menu = getObjById("cdrMenu"); 
_menu.style.display = "none"; 

}; 


Calendar.prototype.bindHeader = function() 

  var _curYear = getObjById("currentYear"); 
  var _curMonth = getObjById("currentMonth"); 
  var _watermark = getObjById("cdrWatermark"); 

  _curYear.innerHTML = this.date.toFormatString("yyyy年"); 
  _curMonth.innerHTML = this.date.toFormatString("mm月"); 
  _watermark.innerHTML = this.date.getFullYear();   

}; 

Calendar.prototype.getToday = function() 

  var _date = new Date(); 
  this.bindDate(_date.toFormatString("-")); 
}; 

Calendar.prototype.isHoliday = function(year,month,date) 

  var _date = new Date(year,month,date); 
  return (_date.getDay() == 6 || _date.getDay() == 0); 
}; 

Calendar.prototype.onMouseOver = function(obj) 

  obj.className = "dayOver"; 
}; 


Calendar.prototype.onMouseOut = function(obj) 

  obj.className = "dayOut"; 
}; 

Calendar.prototype.onClick = function(obj) 
{  
 if(obj.innerHTML != "") this.dateInput.value = obj.value; 
 this.hide(); 
}; 

Calendar.prototype.onChangeYear = function(isnext) 

  var _year = this.date.getFullYear(); 
  var _month = this.date.getMonth()   1; 
  var _date = this.date.getDate(); 

  if(_year > 999 && _year   { 
    if(isnext){_year ;}else{ _year --;} 
  } 
  else 
  { 
   alert("年份超出范围(1000-9999)!"); 
  } 
  this.bindDate(_year   '-'   _month   '-'   _date); 
}; 

Calendar.prototype.onChangeMonth = function(isnext) 

  var _year = this.date.getFullYear(); 
  var _month = this.date.getMonth()   1; 
  var _date = this.date.getDate(); 

  if(isnext){ _month  ;} else {_month--;} 

  if(_year > 999 && _year   { 
    if(_month if(_month > 12) {_month = 1; _year ;} 
  } 
  else 
  { 
   alert("年份超出范围(1000-9999)!"); 
  }  

  this.bindDate(_year   '-'   _month   '-'   _date); 
}; 

Calendar.prototype.showMenu = function(isyear) 

 var _menu = getObjById("cdrMenu"); 

 if(isyear != null) 
 { 

var _obj = (isyear)? getObjById("currentYear") : getObjById("currentMonth"); 

if(isyear) 

  this.getYearMenu(this.date.getFullYear() - 5);   

else 

  this.getMonthMenu();   


_menu.style.top = (_obj.offsetTop   _obj.offsetHeight)   'px'; 
_menu.style.left = _obj.offsetLeft   'px'; 
_menu.style.width = _obj.offsetWidth   'px'; 

  } 

if (this.timer != null) clearTimeout(this.timer); 

_menu.style.display=""; 



Calendar.prototype.hideMenu = function() 

  var _obj = getObjById("cdrMenu"); 
  this.timer = window.setTimeout(function(){_obj.style.display='none';},500); 


Number.prototype.NaN0 = function() 

  return isNaN(this) ? 0 : this; 


Date.prototype.toFormatString = function(fs) 

  if(fs.length == 1) 
  { 
   return this.getFullYear()   fs   (this.getMonth()   1)   fs   this.getDate(); 
  } 
  fs = fs.replace("yyyy",this.getFullYear()); 
  fs = fs.replace("mm",(this.getMonth()   1)); 
  fs = fs.replace("dd",this.getDate()); 
  return fs; 


/********************************************Public methods and variables****** **************************************************/ 
var inputObj = null; //输入对象 
var targetObj = null; //单击目标对象 
var dragObj = null; //拖动目标对象 
var mouseOffset = null; //拖动目标的位置 

//获取对象 
function getObjById(obj) 

  if(document.getElementById) 
  { 
   return document.getElementById(obj); 
  } 
  else 
  { 
   alert("浏览器不支持!"); 
  } 


//获取鼠标位置 
function mouseCoords(ev) 

if(ev.pageX || ev.pageY){ 
return {x:ev.pageX, y:ev.pageY}; 

return { 
x:ev.clientX   document.body.scrollLeft - document.body.clientLeft, 
y:ev.clientY   document.body.scrollTop - document.body.clientTop 
}; 


//获取目标的绝对位置 
function getPosition(e) 

var left = 0; 
var top = 0; 
while (e.offsetParent){ 
left  = e.offsetLeft   (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0); 
top  = e.offsetTop   (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0); 
e   = e.offsetParent; 


left  = e.offsetLeft   (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0); 
top  = e.offsetTop   (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0); 

return {x:left, y:top}; 

//MouseOffset 
function getMouseOffset(target, ev) 

ev = ev || window.event; 
var docPos = getPosition(target); 
var mousePos = mouseCoords(ev); 
return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; 


//关闭日历 
function closeCalendar(evt){ 

evt = evt || window.event; 
var _target= evt.target || evt.srcElement; 

if(!_target.getAttribute("Author") && _target != inputObj && _target != targetObj) 

 getObjById("Calendar"). style.display ="noplay";   
}  

//拖动日历开始 
function dragStart(evt){ 

 evt = evt || window.event; 
var _target= evt.target || evt.srcElement; 

if(_target.getAttribute("Author") == "alin_bar") 
{   
 dragObj = getObjById("Calendar");   
  mouseOffset = getMouseOffset(dragObj, evt); 

}  

//拖动日历中 
function drag(evt) 

 evt = evt || window.event; 
if(dragObj) 
{   
 var mousePos = mouseCoords(evt); 
 dragObj.style.left = (mousePos.x - mouseOffset.x)   'px'; 
 dragObj.style.top = (mousePos.y - mouseOffset.y)   'px';   


//拖动结束 
function dragEnd(evt) 

  dragObj = null;   


/*****end public method*****************/ 


document.onclick = closeCalendar; 

document.onmousedown = dragStart; 

document.onmousemove = drag; 

document.onmouseup = dragEnd; 


/*****************Finish********************/

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool