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

很全的显示阴历(农历)日期的js代码

程序员文章站 2022-06-19 19:49:36
function runglnl(){ var today=new date(); var d=new array("星期日","星期一","星期二","星期三","星期四...
function runglnl(){
var today=new date();
var d=new array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var dddd=(today.getyear()<100 ?
today.getyear()+1900:today.getyear())+"年"+(today.getmonth()+1)+"月"+today.getdate()+"日";
dddd = dddd + " " + d[today.getday()];
dddd = dddd+ " " + (cndateofdatestr(today));
//dddd = dddd+ " " + solarterm(today);
document.write(dddd);
}
function daysnumberofdate(dategl){
return parseint((date.parse(dategl)-date.parse(dategl.getyear()+"/1/1"))/86400000)+1;
}
function cndateofdate(dategl){
var cndata=new array(
0x16,0x2a,0xda,0x00,0x83,0x49,0xb6,0x05,0x0e,0x64,0xbb,0x00,0x19,0xb2,0x5b,0x00,
0x87,0x6a,0x57,0x04,0x12,0x75,0x2b,0x00,0x1d,0xb6,0x95,0x00,0x8a,0xad,0x55,0x02,
0x15,0x55,0xaa,0x00,0x82,0x55,0x6c,0x07,0x0d,0xc9,0x76,0x00,0x17,0x64,0xb7,0x00,
0x86,0xe4,0xae,0x05,0x11,0xea,0x56,0x00,0x1b,0x6d,0x2a,0x00,0x88,0x5a,0xaa,0x04,
0x14,0xad,0x55,0x00,0x81,0xaa,0xd5,0x09,0x0b,0x52,0xea,0x00,0x16,0xa9,0x6d,0x00,
0x84,0xa9,0x5d,0x06,0x0f,0xd4,0xae,0x00,0x1a,0xea,0x4d,0x00,0x87,0xba,0x55,0x04
);
var cnmonth=new array();
var cnmonthdays=new array();
var cnbeginday;
var leapmonth;
var bytes=new array();
var i;
var cnmonthdata;
var dayscount;
var cndayscount;
var resultmonth;
var resultday;
var yyyy=dategl.getyear();
var mm=dategl.getmonth()+1;
var dd=dategl.getdate();
if(yyyy<100) yyyy+=1900;
if ((yyyy < 1997) || (yyyy > 2020)){
return 0;
}
bytes[0] = cndata[(yyyy - 1997) * 4];
bytes[1] = cndata[(yyyy - 1997) * 4 + 1];
bytes[2] = cndata[(yyyy - 1997) * 4 + 2];
bytes[3] = cndata[(yyyy - 1997) * 4 + 3];
if ((bytes[0] & 0x80) != 0) {cnmonth[0] = 12;}
else {cnmonth[0] = 11;}
cnbeginday = (bytes[0] & 0x7f);
cnmonthdata = bytes[1];
cnmonthdata = cnmonthdata << 8;
cnmonthdata = cnmonthdata | bytes[2];
leapmonth = bytes[3];
for (i=15;i>=0;i--){
cnmonthdays[15 - i] = 29;
if (((1 << i) & cnmonthdata) != 0 ){
cnmonthdays[15 - i]++;}
if (cnmonth[15 - i] == leapmonth ){
cnmonth[15 - i + 1] = - leapmonth;}
else{
if (cnmonth[15 - i] < 0 ){cnmonth[15 - i + 1] = - cnmonth[15 - i] + 1;}
else {cnmonth[15 - i + 1] = cnmonth[15 - i] + 1;}
if (cnmonth[15 - i + 1] > 12 ){ cnmonth[15 - i + 1] = 1;}
}
}
dayscount = daysnumberofdate(dategl) - 1;
if (dayscount <= (cnmonthdays[0] - cnbeginday)){
if ((yyyy > 1901) && (cndateofdate(new date((yyyy - 1)+"/12/31")) < 0)){
resultmonth = - cnmonth[0];}
else {resultmonth = cnmonth[0];}
resultday = cnbeginday + dayscount;
}
else{
cndayscount = cnmonthdays[0] - cnbeginday;
i = 1;
while ((cndayscount < dayscount) && (cndayscount + cnmonthdays[i] < dayscount)){
cndayscount+= cnmonthdays[i];
i++;
}
resultmonth = cnmonth[i];
resultday = dayscount - cndayscount;
}
if (resultmonth > 0){
return resultmonth * 100 + resultday;}
else{return resultmonth * 100 - resultday;}
}
function cnyearofdate(dategl){
var yyyy=dategl.getyear();
var mm=dategl.getmonth()+1;
var cnmm=parseint(math.abs(cndateofdate(dategl))/100);
if(yyyy<100) yyyy+=1900;
if(cnmm>mm) yyyy--;
yyyy-=1864;
return cnera(yyyy)+"年";
}
function cnmonthofdate(dategl){
var cnmonthstr=new array("零","正","二","三","四","五","六","七","八","九","十","十一","腊");
var month;
month = parseint(cndateofdate(dategl)/100);
if (month < 0){return "闰" + cnmonthstr[-month] + "月";}
else{return cnmonthstr[month] + "月";}
}
function cndayofdate(dategl){
var cndaystr=new array("零",
"初一", "初二", "初三", "初四", "初五",
"初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五",
"十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五",
"廿六", "廿七", "廿八", "廿九", "三十");
var day;
day = (math.abs(cndateofdate(dategl)))%100;
return cndaystr[day];
}
function daysnumberofmonth(dategl){
var mm1=dategl.getyear();
mm1<100 ? mm1+=1900:mm1;
var mm2=mm1;
mm1+="/"+(dategl.getmonth()+1);
mm2+="/"+(dategl.getmonth()+2);
mm1+="/1";
mm2+="/1";
return parseint((date.parse(mm2)-date.parse(mm1))/86400000);
}
function cnera(yyyy){
var tiangan=new array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
//var dizhi=new array("子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)","巳(蛇)",
//"午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)");
var dizhi=new array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥");
return tiangan[yyyy%10]+dizhi[yyyy%12];
}
function cndateofdatestr(dategl){
if(cnmonthofdate(dategl)=="零月") return " 请调整您的计算机日期!";
else return "农历:"+cnyearofdate(dategl)+ " " + cnmonthofdate(dategl) + cndayofdate(dategl);
}
function solarterm(dategl){
var solartermstr=new array(
"小寒","大寒","立春","雨水","惊蛰","春分",
"清明","谷雨","立夏","小满","芒种","夏至",
"小暑","大暑","立秋","处暑","白露","秋分",
"寒露","霜降","立冬","小雪","大雪","冬至");
var differenceinmonth=new array(
1272060,1275495,1281180,1289445,1299225,1310355,
1321560,1333035,1342770,1350855,1356420,1359045,
1358580,1355055,1348695,1340040,1329630,1318455,
1306935,1297380,1286865,1277730,1274550,1271556);
var differenceinyear=31556926;
var begintime=new date(1901/1/1);
begintime.settime(947120460000);
for(;dategl.getyear()<begintime.getyear();){
begintime.settime(begintime.gettime()-differenceinyear*1000);
}
for(;dategl.getyear()>begintime.getyear();){
begintime.settime(begintime.gettime()+differenceinyear*1000);
}
for(var m=0;dategl.getmonth()>begintime.getmonth();m++){
begintime.settime(begintime.gettime()+differenceinmonth[m]*1000);
}
if(dategl.getdate()>begintime.getdate()){
begintime.settime(begintime.gettime()+differenceinmonth[m]*1000);
m++;
}
if(dategl.getdate()>begintime.getdate()){
begintime.settime(begintime.gettime()+differenceinmonth[m]*1000);
m==23?m=0:m++;
}
var jq;
if(dategl.getdate()==begintime.getdate()){
jq="  今天是<font color='#ff9999'><b>"+solartermstr[m] + "</b></font>";
}
else if(dategl.getdate()==begintime.getdate()-1){
jq="  明天是<font color='#ff9999'><b>"+solartermstr[m] + "</b></font>";
}
else if(dategl.getdate()==begintime.getdate()-2){
jq="  后天是<font color='#ff9999'><b>"+solartermstr[m] + "</b></font>";
}
else{
jq=" "
if(dategl.getmonth()==begintime.getmonth()){
jq+="  本月";
}
else{
jq+="  下月";
}
jq+=begintime.getdate()+"日"+"<font color='#ff9999'><b>"+solartermstr[m]+"</b></font>";
}
return jq;
}
function cal()
{}
runglnl();