欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

日历控件

程序员文章站 2024-03-24 12:16:52
...

var DateConversion = (function() {
var constants = {
LUNARINFO : new Array(
/* 1900 */
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554,
0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250,
0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970,
0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570,
0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0,
0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6,
0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950,
0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573,
0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60,
0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558,
0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0,
0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,
0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60,
0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552,
0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0,
0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0,
0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6,
0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0,
0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50,
0x1b255, 0x06d20, 0x0ada0,/* 2049 */
// 下面的是从 2050 到 2100 年的
0x14b63, /* 2050 */
0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, /* 2055 */
0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, 0x0a2e0, /* 2060 */
0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, /* 2065 */
0x05d55, 0x056a0, 0x0a6d0, 0x055d4, 0x052d0, /* 2070 */
0x0a9b8, 0x0a950, 0x0a4a0, 0x0b6a6, 0x0ad50, /* 2075 */
0x055a0, 0x0aba0, 0x0a5b0, 0x052b0, 0x0b273, /* 2080 */
0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, /* 2085 */
0x04b60, 0x0a570, 0x054e4, 0x0d260, 0x0e968, /* 2090 */
0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, /* 2095 */
0x0a9d4, 0x0a4d0, 0x0d150, 0x0f252, 0x0d520 /* 2100 */),
GAN : new Array("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"),
ZHI : new Array("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌",
"亥")
};
var cd = function(strDate) {
var date, year, month, day, displayDay, displayMonth,result,rDay,rMOnth;
this.setDate = function(strDate) {
if (strDate instanceof Date){
date = strDate;
}else if (typeof(strDate) == 'string'){
date = this.strToDate(strDate);
} else {
date = new Date();
}
};
this.getDate = function() {
return date;
};
this.getResult = function(){
return result;
};
this.getRDay = function(){
return rDay;
};
this.getRMonth = function(){
return rMOnth;
}

this.setDate(strDate);
var res =lunar(new Date(date.getFullYear(), date.getMonth(),date.getDate()));
var cl = '<font color="red" STYLE="font-size:9pt;">';
var tt = cyclical(res[0] - 1900 + 36) +'('+res[0] + ')年'+(res[1] ==true? '闰' : '')
+ cDay(res[2], res[3]);
result =cl + tt + '</font>';
var rDay = cRDay(res[3]);
var rMOnth = cRMonth(res[2]);
};
cd.getConstant = function(name) {
return constants[name];
};
// ==== 传入 offset 传回干支, 0=甲子
function cyclical(num) {
return (DateConversion.getConstant('GAN')[num % 10] + DateConversion
.getConstant('ZHI')[num % 12]);
};
// ==== 传回农历 y年的总天数
function lYearDays(y) {
var i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1)
sum += (DateConversion.getConstant('LUNARINFO')[y - 1900] & i)
? 1
: 0;
return (sum + leapDays(y));
};
// ==== 传回农历 y年闰月的天数
function leapDays(y) {
if (leapMonth(y))
return ((DateConversion.getConstant('LUNARINFO')[y - 1900] & 0x10000)
? 30
: 29);
else
return (0);
};
// ==== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
function leapMonth(y) {
return (DateConversion.getConstant('LUNARINFO')[y - 1900] & 0xf);
};
// ====================================== 传回农历 y年m月的总天数
function monthDays(y, m) {
return (DateConversion.getConstant('LUNARINFO')[y - 1900] & (0x10000 >> m))
? 30
: 29;
};
// ==== 中文日期
function cDay(m, d) {
var nStr1 = new Array('日', '一', '二', '三', '四', '五', '六', '七', '八', '九',
'十');
var nStr2 = new Array('初', '十', '廿', '卅', ' ');
var s;
if (m > 10) {
s = '十' + nStr1[m - 10];
} else {
s = nStr1[m];
}
s += '月';
switch (d) {
case 10 :
s += '初十';
break;
case 20 :
s += '二十';
break;
case 30 :
s += '三十';
break;
default :
s += nStr2[Math.floor(d / 10)];
s += nStr1[d % 10];
}
return (s);
};
function cRMonth(m) {
var nStr1 = new Array('日', '一', '二', '三', '四', '五', '六', '七', '八', '九',
'十');
var nStr2 = new Array('初', '十', '廿', '卅', ' ');
var s;
if (m > 10) {
s = '十' + nStr1[m - 10];
} else {
s = nStr1[m];
}
s += '月';
return (s);
};
function cRDay(d) {
var nStr1 = new Array('日', '一', '二', '三', '四', '五', '六', '七', '八', '九',
'十');
var nStr2 = new Array('初', '十', '廿', '卅', ' ');
var s='';
switch (d) {
case 10 :
s += '初十';
break;
case 20 :
s += '二十';
break;
case 30 :
s += '三十';
break;
default :
s += nStr2[Math.floor(d / 10)];
s += nStr1[d % 10];
}
return (s);
};
// ==== 算出农历, 传入日期物件, 传回农历日期物件
// ==== 该物件属性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl
function lunar(objDate) {
var i, leap = 0, temp = 0;
var baseDate = new Date(1900, 0, 31);
var offset = (objDate - baseDate) / 86400000;

// this.dayCyl = offset + 40;
var monCyl = 14;

for (i = 1900; i < 2100 && offset > 0; i++) {
temp = lYearDays(i);
offset -= temp;
monCyl += 12;
}
if (offset < 0) {
offset += temp;
i--;
monCyl -= 12;
}

var year = i;
// this.yearCyl = i - 1864;

leap = leapMonth(i); // 闰哪个月
var isLeap = false;

for (i = 1; i < 13 && offset > 0; i++) {
// 闰月
if (leap > 0 && i == (leap + 1) && isLeap == false) {
--i;
isLeap = true;
temp = leapDays(year);
} else {
temp = monthDays(year, i);
}

// 解除闰月
if (isLeap == true && i == (leap + 1)) {
isLeap = false;
}

offset -= temp;
if (isLeap == false) {
monCyl++;
}
}

if (offset == 0 && leap > 0 && i == leap + 1) {
if (isLeap) {
isLeap = false;
} else {
isLeap = true;
--i;
--monCyl;
}
}

if (offset < 0) {
offset += temp;
--i;
--monCyl;
}
//年(闰)月日
return [year,isLeap,i,offset + 1];
}
return cd;
})();

DateConversion.prototype = {
strToDate : function(dateStr,separator){
if(!separator){
separator="-";
}
var dateArr = dateStr.split(separator);
var year = parseInt(dateArr[0]);
var month;
//处理月份为04这样的情况
if(dateArr[1].indexOf("0") == 0){
month = parseInt(dateArr[1].substring(1));
}else{
month = parseInt(dateArr[1]);
}
var day = parseInt(dateArr[2]);
var date = new Date(year,month -1,day);
return date;
}
};


<!DOCTYPE HTML>
<html>
<head>
<title>每日签到</title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="DateConversion.js"></script>
<style type="text/css">
.table {
margin-bottom: 0px;
border: 0px;
width: 100%;
}

.table>tbody>tr>td {
height: 46px;
//width: 40px;
padding: 4px;
font-size: 16px;
}

.score {
float: right;
font-size: 10px;
color: #a0a0a0
}

.table-bordered>thead>tr>th {
background-color: #FF8888;
border: 0px;
}

.prevMonth,.nextMonth {
color: #a0a0a0;
}

.thisDay {
background-color: #FFC8C8
}
.dailyAttendance{
background-color: #c8ffc8
}

b.rtop,b.rbottom {
display: block;
background: #FFF
}

b.rtop b {
display: block;
height: 1px;
overflow: hidden;
background: #FF8888;
}

b.rbottom b {
display: block;
height: 1px;
overflow: hidden;
background: #DDDDDD;
}

b.r1 {
margin: 0 5px
}

b.r2 {
margin: 0 3px
}

b.r3 {
margin: 0 2px
}

b.rtop b.r4,b.rbottom b.r4 {
margin: 0 1px;
height: 2px
}
</style>
<SCRIPT LANGUAGE="JavaScript">
$(function() {
var Calendar = (function(){
return function(strDate){
var date, year, month,result,startDate,endDate,curDate;
this.getYear = function(){
return year;
};
this.setYear = function(v_year){
year = v_year;
};
this.getMonth = function(){
return month;
};
this.setMonth = function(v_month){
month = v_month;
};
this.getDate = function(){
return date;
};
this.setResult = function(v_result){
result = v_result;
};
this.getResult = function(){
return result;
};
this.setStartDate = function(v_startDate){
startDate = v_startDate;
};
this.getStartDate = function(){
return startDate;
};
this.setEndDate = function(v_endDate){
endDate = v_endDate;
};
this.getEndDate = function(){
return endDate;
};
this.setCurDate = function(v_curDate){
curDate = v_curDate;
};
this.getCurDate = function(){
return curDate;
}
//如果日期不存在,就默认为当前日期
if(!strDate){
var now = new Date();
date = this.strToDate((now.getYear()+1900)+'-'+now.getMonth()+'-'+now.getDate());
}else{
date = this.strToDate(strDate);
}
//设置年月
this.setYear( date.getYear() + 1900 );
this.setMonth(date.getMonth());
};

})();
Calendar.prototype = {
strToDate : function(str) {
var arys = str.split('-');
var date = new Date(arys[0], arys[1], arys[2]);
return date;
},
isFourDigitYear : function(){

},
getDaysInMonth : function (year,month) {
var days;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
days = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
days = 30;
else if (month == 2) {
if (this.isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
}
return (days);
},
isLeapYear: function (year) {
if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
return true;
} else {
return false;
}
},

checkMinDate : function(year,month){
// var minYear = this.getResult().minYear + 1900;
// var minMonth = this.getResult().minMonth;
// if(year >= minYear && month >= minMonth ){
// return true;
// }
return true;
},
setPreviousMonth : function () {
var month = this.getMonth();
var year = this.getYear();
if (month == 0) {
month = 11;
//if (this.getYear() > 1000) {
year--;
//}
} else {
month--;
}
if(this.checkMinDate(year,month)){
this.setYear( year);
this.setMonth(month);
this.displayCalendar();
}
},
checkMaxDate : function(year,month){
// var maxDate = this.strToDate(year+'-'+month+'-'+1);
// var nextDate = this.strToDate(this.getDate().getFullYear()+'-'+(this.getDate().getMonth()+1*1)+'-'+1)
// if(nextDate >= maxDate){
// return true;
// }
return true;
},
setNextMonth : function () {
var month = this.getMonth();
var year = this.getYear();
if (month == 11) {
month = 0;
year ++;
} else {
month++;
}
if(this.checkMaxDate(year,month)){
this.setYear(year);
this.setMonth(month);
this.displayCalendar();
}
},

createTd : function(className,text,wePoint){
//var thisDate = (this.getDate().getYear()+ 1900*1)+'-'+this.getDate().getMonth()+'-'+this.getDate().getDate();
//var thisDate = this.getYear()+'-'+this.getMonth()+'-'+text;
console.log(this.getCurDate());
var obj = new DateConversion(this.getCurDate());
console.log(obj.getResult());
var nl = obj.getRDay();
if(nl == '初一'){
nl = obj.getRMonth();
}
return '<td class="'+className+'"><div >'+text+'</div><div class="score">'+ nl +'</div></td>';
},

previousDays : function(){
var month = this.getMonth();
var year = this.getYear();
if (month == 0) {
month = 11;
//if (year > 1000) {
year--;
//}
} else {
month--;
}
var days = this.getDaysInMonth(year, month+1);
return {'year':year,'month':month,'days':days};
},
getWePoint : function(ymd,day){
var wePoint = '';
// var cdate = this.strToDate( ymd.year+'-'+ymd.month+'-'+day);
// //
// if(this.getDate() < this.getStartDate() ){
// wePoint = 0;//this.getWePointBySignInRule(cdate,day);
// }else if(this.getEndDate() > this.getDate() && this.getDate() > this.getStartDate()) {
// if(cdate <= this.getDate() ){
// var das = this.getResult().histories;
// for(var i = 0 ; i < das.length;i++){
// if(day == das[i].day
// && ymd.year == das[i].year + 1900
// && ymd.month == das[i].month){
// wePoint = das[i].wePoint;
// }
// }
// }else{
// wePoint = this.getWePointBySignInRule(cdate,day);
// }

// }else{
// var das = this.getResult().histories;
// for(var i = 0 ; i < das.length;i++){
// if(day == das[i].day
// && ymd.year == das[i].year + 1900
// && ymd.month == das[i].month ){
// wePoint = das[i].wePoint;
// }
// }
// }
return wePoint;
},
getWePointBySignInRule : function(cdate,day){
//var cdate = this.strToDate( ymd.year+'-'+ymd.month+'-'+day);
var csr = this.getResult().customerSignInRule;
var no = Math.abs(cdate.getTime() - this.getDate().getTime())/(24*60*60*1000);
no += csr.no;
var signInRule = this.getResult().inRule;
var signInRuleDetails = signInRule.details;
if(no > signInRuleDetails.length){
return signInRuleDetails[signInRuleDetails.length-1].wePoint;
}
for(var i = 0 ;i < signInRuleDetails.length ; i++){
if(signInRuleDetails[i].no == no){
return signInRuleDetails[i].wePoint;
}
}
throw new Error('没有找到相应的做签到规则!');

},
previousMonth : function(startingPos){
var ymd = this.previousDays();
var days = ymd.days;
var tr ='<tr>';
for (var i = 0; i < startingPos; i++) {
var className='prevMonth';
var wePoint = 0;//this.getWePoint(ymd,days-startingPos+i+1) ;
var thisDate = this.strToDate((this.getDate().getYear()+ 1900*1)+'-'+this.getDate().getMonth()+'-'+this.getDate().getDate());
var assignDate = this.strToDate(ymd.year+'-'+ymd.month+'-'+(days-startingPos+i+1*1) );
if(wePoint && thisDate > assignDate ){
className ='dailyAttendance';
}
this.setCurDate(ymd.year+'-'+(ymd.month+1)+'-'+(days-startingPos+i+1*1));
tr += this.createTd(className,days-startingPos+i+1,wePoint);
}
return tr;
},
thisMonth : function(startingPos,days){
var tr ='';
for (var i = startingPos; i < days; i++) {
if (i % 7 == 0) {
tr += "</tr><tr>"
}
var className = '';
if(this.getDate().getDate() == i - startingPos + 1
&& this.getDate().getMonth() == this.getMonth()
&& this.getDate().getYear()+ 1900 == this.getYear() ){
className = 'thisDay';
}
var displayDay = '';
if(i == startingPos){
displayDay = (this.getMonth()+1)+"月";
}else{
displayDay = i - startingPos + 1;
}
var wePoint =0;//= this.getWePoint({'year':this.getYear(),'month':this.getMonth()},i-startingPos+1) ;
var thisDate = this.strToDate((this.getDate().getYear()+ 1900*1)+'-'+this.getDate().getMonth()+'-'+this.getDate().getDate());
var assignDate = this.strToDate(this.getYear()+'-'+this.getMonth()+'-'+(i - startingPos + 1*1) );
if(wePoint && thisDate > assignDate ){
className ='dailyAttendance';
}
this.setCurDate(this.getYear()+'-'+(this.getMonth()+1)+'-'+(i - startingPos + 1*1));
tr += this.createTd(className,displayDay,wePoint);
}
return tr;
},
nextDays : function(){
var month = this.getMonth();
var year = this.getYear();
if (month == 11) {
month = 0;
year ++;
} else {
month++;
}
var days = this.getDaysInMonth(year, month+1);
return {'year':year,'month':month,'days':days};
},
endDays : function(days){
var endDay = 28;
if(days > 35){
endDay =42;
}else if(days > 28 ){
endDay = 35;
}
return endDay;
},
nextMonth : function(startingPos,days){
var tr = '';
var nextDay = 1;
var ymd = this.nextDays();
var displayDay = (ymd.month+1)+"月"
var endDay = this.endDays(days);
for (i = days; i < endDay; i++) {
var className ='nextMonth';
var wePoint = this.getWePoint(ymd,nextDay) ;
var thisDate = this.strToDate((this.getDate().getYear()+ 1900*1)+'-'+this.getDate().getMonth()+'-'+this.getDate().getDate());
var assignDate = this.strToDate(ymd.year+'-'+ymd.month+'-'+ nextDay );
if(wePoint && thisDate > assignDate ){
className ='dailyAttendance';
}
this.setCurDate(ymd.year+'-'+(ymd.month+1)+'-'+nextDay);
tr += this.createTd(className,displayDay,wePoint);
displayDay = ++nextDay;
}
return tr+= "</tr>";
},
displayCalendar : function () {

var days = this.getDaysInMonth(this.getYear(), this.getMonth()+1);
var firstOfMonth = new Date(this.getYear(), this.getMonth(), 1);
var startingPos = firstOfMonth.getDay();
days += startingPos;

var preDays = this.previousDays();
var day = preDays.days-startingPos+1
var startDateStr = preDays.year+'-'+(preDays.month +1)+'-'+day;

var nextDays = this.nextDays();
var endDay = this.endDays(days) - days ;
var endDateStr = nextDays.year+'-'+(nextDays.month +1)+'-'+endDay;



var url = '${rc.contextPath}/dailyAttendanceWechat/getDailyAttendance';
var param = { 'startDate' : startDateStr, 'endDate' : endDateStr }
var startDate = this.strToDate(preDays.year+'-'+preDays.month +'-'+day);
var endDate = this.strToDate(nextDays.year+ '-' +nextDays.month +'-'+endDay);
this.setStartDate(startDate);
this.setEndDate(endDate);
$('#crtDate').html(this.getYear()+'年'+(this.getMonth()+1*1)+'月');
var tr = this.previousMonth(startingPos)+this.thisMonth(startingPos,days)+this.nextMonth(startingPos,days);
var tb = $('table > tbody ');
tb.children().remove();
tb.append(tr);
if(this.getDate() < startDate ){
var tr = this.previousMonth(startingPos)+this.thisMonth(startingPos,days)+this.nextMonth(startingPos,days);
var tb = $('table > tbody ');
tb.children().remove();
tb.append(tr);
}else{
var that = this;
var tr = that.previousMonth(startingPos)+that.thisMonth(startingPos,days)+that.nextMonth(startingPos,days);
var tb = $('table > tbody ');
tb.children().remove();
tb.append(tr);
// base.postNoAlert( url,param, function(result) {
// that.setResult(result);
// $('#countWePoint').html('签到共计:<span style="color:#F96B57;">'+result.totalWePoint+'分</span>');
// $('#daCount').html('连续签到:<span style="color:#F96B57;">'+result.customerSignInRule.no+'天</span>');
// var tr = that.previousMonth(startingPos)+that.thisMonth(startingPos,days)+that.nextMonth(startingPos,days);
// var tb = $('table > tbody ');
// tb.children().remove();
// tb.append(tr);
// $('#signInRuleDetail').html('<b>签到规则如下:</b><br/>'+result.inRule.declaration);
// });
}
}
};
var calendar = new Calendar();
calendar.displayCalendar();

$('#preMonth').click(function(){
calendar.setPreviousMonth();
});
$('#nextMonth').click(function(){
calendar.setNextMonth();
});
});

</SCRIPT>
</head>
<body>
<div class="row" style="margin: 0px;">
<div id="daCount" class="col-xs-4" style="padding: 0px;"></div>
<div id="crtDate" class="col-xs-4" style="padding: 0px;text-align: center;"></div>
<div id="countWePoint" class="col-xs-4" style="padding: 0px;text-align: right;"></div>
</div>
<div style="clear: both;">
<b class="rtop"><b class="r1"></b><b class="r2"></b> <b class="r3"></b><b class="r4"></b></b>
<table class="table table-bordered">
<thead>
<tr>
<th>日</th>
<th>一</th>
<th>二</th>
<th>三</th>
<th>四</th>
<th>五</th>
<th>六</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<b class="rbottom"><b class="r4"></b><b class="r3"></b> <b class="r2"></b><b class="r1"></b></b>
</div>
<a href="javascript:void(0)" id="preMonth">前一个月</a>
<a href="javascript:void(0)" id="nextMonth">后一个月</a>
</body>
</html>