js阳历历阴历转化简化修改

<SCRIPT language=JavaScript>
//农历小数数组
//农历大数数组
var nStr1 = new Array('十','一', '二', '三', '四', '五', '六', '七', '八', '九');
var nStr2 = new Array('初', '十', '廿', '卅', ' ');

//农历月份数组
var mon = new Array(' ','一', '二', '三', '四', '五', '六', '七', '八', '九','十','十一','十二');

//***天文台农历数据
var lunarInfo=new Array(
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,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,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,
0x14b63);


//***农历节日
var nftv = new Array(
"0101 春节",
"0202 龙抬头",
"0115 元宵节",
"0505 端午节",
"0707 七夕情人节",
"0715 中元节",
"0815 中秋节",
"0909 重阳节",
"1208 腊八节",
"1224 小年",
"0100 除夕"
);

//***阳日节日
var yftv = new Array(
"0101 元旦",
"0214 情人节",
"0308 妇女节",
"0309 偶今天又长一岁拉",
"0312 植树节",
"0315 消费者权益日",
"0401 愚人节",
"0418 MM的生日",
"0501 劳动节",
"0504 青年节",
"0512 护士节",
"0601 儿童节",
"0701 建党节 香港回归纪念",
"0801 建军节",
"0808 父亲节",
"0909 毛席逝世纪念",
"0910 教师节",
"0928 孔子诞辰",
"1001*国庆节",
"1006 老人节",
"1024 联合国日",
"1112 孙中山诞辰",
"1220 澳门回归纪念",
"1225 圣诞节",
"1226 毛席诞辰"
);

//*******************************格式化时间 返回时间数组 /年/月/日/星期
function gettime(y,m,d){
var d = new Date(y,m-1,d);
return d;
}


function lYearDays(y) {
var i, sum = 348;
for(i=0x8000; i>0x8; i>>=1) sum += (lunarInfo[y-1900] & i)? 1: 0;
return(sum+leapDays(y));
}

//********************************** 返回农历 y年闰月的天数
function leapDays(y) {
if(leapMonth(y)) return((lunarInfo[y-1900] & 0x10000)? 30: 29);
else return(0);
}

//******************************** 返回农历 y年闰哪个月 1-12 , 没闰返回 0
function leapMonth(y) {
return(lunarInfo[y-1900] & 0xf);
}

//********************************* 返回农历 y年m月的总天数
function monthDays(y,m) {
return( (lunarInfo[y-1900] & (0x10000>>m))? 30: 29 );
}


//*************************************** 算出农历, 传入日期控件, 返回农历日期控件
//*************************************** 该控件属性有 .year .month .day .isLeap
function Lunar(objDate) {

var i, leap=0, temp=0;
var offset = (Date.UTC(objDate.getFullYear(),objDate.getMonth(),objDate.getDate()) - Date.UTC(1900,0,31))/86400000;

for(i=1900; i<2050 && offset>0; i++) { temp=lYearDays(i); offset-=temp; }

if(offset<0) { offset+=temp; i--; }

this.year = i;

leap = leapMonth(i); //******************闰哪个月
this.isLeap = false;

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

//*****解除闰月
if(this.isLeap==true && i==(leap+1)) this.isLeap = false;

offset -= temp;
}

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

if(offset<0){ offset += temp; --i; }

this.month = i;
this.day = offset + 1;
return new Array(this.year,this.month,this.day);
}


//**************************格式化农历日期**返回字符串**传入农历数组
function getallmon(arr){
var a = arr;
for(var i=0;i<a.length;i++){
if(a[i]-0>9){}
else{
a[i]='0'+a[i];
}
}
var str = a[1]+''+a[2];
for(var j=0; j<nftv.length;j++)
{
var njie = nftv[j].split(' ');
if(str==njie[0]){
str = njie[1];
break;
}
}

if(str == a[1]+''+a[2]){
str = '';
var stt = '';
if((a[1]+'').substr(0,1)-0==0){
str+=mon[(a[1]+'').substr(1,1)-0]+'月';
}else{
str+=mon[a[1]-0]+'月';
}
stt = stt + nStr2[(a[2]+'').substr(0,1)-0];
stt = stt + nStr1[(a[2]+'').substr(1,1)-0];
if(stt=='十十'){
stt='初十';
}

}
if(stt==undefined)
return str;
else
return str+stt;
}


//************************************返回某月最大天数
function getmaxdays(year,month){
month = parseInt(month,10);
var temp = new Date(year,month,0);
return temp.getDate();
}


//调用方法
function show(yy,mm){
var days = getmaxdays(yy,mm);
for(var i=1;i<=days;i++){
var arr = Lunar(gettime(yy,mm,i));
alert(i+getallmon(arr));
}
}


show(2013,3);

</script>

posted @ 2013-03-18 21:45  @specter  阅读(343)  评论(0编辑  收藏  举报