js获取年月日星期几农历

时间:13-03-26 栏目:Javascript, web前端 作者:zongyan86 评论:0 点击: 3,322 次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function nl() {
//获取当前系统时间
curTime = new Date();
//星期名
var weekName = "星期日,星期一,星期二,星期三,星期四,星期五,星期六".split(/,/g);
//天干名称
//var tianGang = "甲,乙,丙,丁,戊,己,庚,辛,壬,癸".split(/,/g);
//地支名称
//var diZhi = "子,丑,寅,卯,辰,巳,午,未,申,酉,戌,亥".split(/,/g);
//属相名称
//var shuXiang = "鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪".split(/,/g);
//农历日期名
var dayName = "*,初一,初二,初三,初四,初五,初六,初七,初八,初九,初十,十一,十二,十三,十四,十五,十六,十七,十八,十九,二十,廿一,廿二,廿三,廿四,廿五,廿六,廿七,廿八,廿九,三十".split(/,/g);
//农历月份名
var monName = "*,正,二,三,四,五,六,七,八,九,十,十一,腊".split(/,/g);
//公历每月前面的天数
var monthAdd = new Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
//农历数据
var nongliData = new Array(2635, 333387, 1701, 1748, 267701, 694, 2391, 133423, 1175, 396438, 3402, 3749, 331177, 1453, 694, 201326, 2350, 465197, 3221, 3402, 400202, 2901, 1386, 267611, 605, 2349, 137515, 2709, 464533, 1738, 2901, 330421, 1242, 2651, 199255, 1323, 529706, 3733, 1706, 398762, 2741, 1206, 267438, 2647, 1318, 204070, 3477, 461653, 1386, 2413, 330077, 1197, 2637, 268877, 3365, 531109, 2900, 2922, 398042, 2395, 1179, 267415, 2635, 661067, 1701, 1748, 398772, 2742, 2391, 330031, 1175, 1611, 200010, 3749, 527717, 1452, 2742, 332397, 2350, 3222, 268949, 3402, 3493, 133973, 1386, 464219, 605, 2349, 334123, 2709, 2890, 267946, 2773, 592565, 1210, 2651, 395863, 1323, 2707, 265877);
//生成当前公历年、月、日 ==> GongliStr
var curYear = curTime.getFullYear();
var curMonth = curTime.getMonth() + 1;
var curDay = curTime.getDate();
var gongliStr = curYear + "年";
if (curMonth < 10)
gongliStr = gongliStr + "0" + curMonth + "月";
else
gongliStr = gongliStr + curMonth + "月";
if (curDay < 10)
gongliStr = gongliStr + "0" + curDay + "日";
else
gongliStr = gongliStr + curDay + "日";
//生成当前公历星期 ==> WeekdayStr
var curWeekday = curTime.getDay();
var weekdayStr = weekName[curWeekday];
//计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
var theDate = (curYear - 1921) * 365 + Math.floor((curYear - 1921.0) / 4) + curDay + monthAdd[curMonth - 1] - 38;
if ((curYear % 4) == 0 && curMonth > 2)
theDate++;
//计算农历天干、地支、月、日
var isEnd = 0;
var m = 0, k, n;
while (1) {
if (nongliData[m] < 4095) k = 11;
else k = 12;
n = k;
while (1) {
if (n < 0) break;
//获取NongliData[m]的第n个二进制位的值
bit = nongliData[m]
for (var i = 0; i < n; i++)
bit = Math.floor(bit / 2.0);
bit = bit % 2;
if (theDate <= (29 + bit)) {
isEnd = 1
break;
}
theDate -= 29 + bit;
n--;
}
if (isEnd == 1) break;
m++;
}
curYear = 1921 + m;
curMonth = k - n + 1;
curDay = theDate;
if (k == 12) {
var mc = (Math.floor(nongliData[m] / 65536.0) + 1);
if (curMonth == mc)
curMonth = 1 - curMonth;
else if (curMonth > mc) //NongliData(m)
curMonth = curMonth - 1;
}
var o = new Object();
o.year = new Date().getFullYear();
o.month = new Date().getMonth() + 1;
o.date = new Date().getDate();
o.day = new Date().getDay();
o.dayName = weekName[o.day];
//o.nYear = tianGang[((curYear - 4) % 60) % 10] + diZhi[((curYear - 4) % 60) % 12] + "年";
if (curMonth < 1)
o.nMonth = "闰" + monName[-1 * curMonth];
else
o.nMonth = monName[curMonth];
o.nMonth += "月";
o.nDate = dayName[curDay];
//o.nZodiac = shuXiang[((curYear - 4) % 60) % 12];
return o;
}

$(function () {
var ny = nl();
var s ="日期:" +(ny.year) + "年" + (ny.month) + "月" + (ny.date) + "日 " + ny.dayName // + "农历:" + ny.nMonth + ny.nDate
$("#showdate").text(s);
});

web技术分享



声明: 本文由( zongyan86 )原创编译,转载请保留链接: js获取年月日星期几农历

广告虚位以待

关注我们