Skip to content

Instantly share code, notes, and snippets.

@terry-fei
Created October 28, 2014 11:38
Show Gist options
  • Save terry-fei/cabf291b57670db61acc to your computer and use it in GitHub Desktop.
Save terry-fei/cabf291b57670db61acc to your computer and use it in GitHub Desktop.
阴历
var lunarinfo = [19416, 19168, 42352, 21717, 53856, 55632, 91476, 22176, 39632, 21970, 19168, 42422, 42192, 53840, 119381, 46400, 54944, 44450, 38320, 84343, 18800, 42160, 46261, 27216, 27968, 109396, 11104, 38256, 21234, 18800, 25958, 54432, 59984, 28309, 23248, 11104, 100067, 37600, 116951, 51536, 54432, 120998, 46416, 22176, 107956, 9680, 37584, 53938, 43344, 46423, 27808, 46416, 86869, 19872, 42448, 83315, 21200, 43432, 59728, 27296, 44710, 43856, 19296, 43748, 42352, 21088, 62051, 55632, 23383, 22176, 38608, 19925, 19152, 42192, 54484, 53840, 54616, 46400, 46496, 103846, 38320, 18864, 43380, 42160, 45690, 27216, 27968, 44870, 43872, 38256, 19189, 18800, 25776, 29859, 59984, 27480, 21952, 43872, 38613, 37600, 51552, 55636, 54432, 55888, 30034, 22176, 43959, 9680, 37584, 51893, 43344, 46240, 47780, 44368, 21977, 19360, 42416, 86390, 21168, 43312, 31060, 27296, 44368, 23378, 19296, 42726, 42208, 53856, 60005, 54576, 23200, 30371, 38608, 19415, 19152, 42192, 118966, 53840, 54560, 56645, 46496, 22224, 21938, 18864, 42359, 42160, 43600, 111189, 27936, 44448],
STR1 = "日一二三四五六七八九十".split(""),
STR2 = ["初", "十", "廿", "卅", "\u3000"],
BASEDATE = new Date(1900, 0, 31);
function lyeardays(a) {
var b, c = 348;
for (b = 32768; 8 < b; b >>= 1) c += lunarinfo[a - 1900] & b ? 1 : 0;
return c + leapdays(a)
}
function leapdays(a) {
return leapmonth(a) ? lunarinfo[a - 1900] & 65536 ? 30 : 29 : 0
}
function leapmonth(a) {
return lunarinfo[a - 1900] & 15
}
function monthdays(a, b) {
return lunarinfo[a - 1900] & 65536 >> b ? 30 : 29
}
function Lunar(a) {
this.date = a || new Date;
var b = 0,
c = 0,
d = (this.date - BASEDATE) / 864E5;
this.daycyl = d + 40;
this.moncyl = 14;
for (a = 1900; 2050 > a && 0 < d; a++) c = lyeardays(a), d -= c, this.moncyl += 12;
0 > d && (d += c, a--, this.moncyl -= 12);
this.year = a;
this.yearcyl = a - 1864;
b = leapmonth(a);
this.isleap = !1;
for (a = 1; 13 > a && 0 < d; a++) 0 < b && a == b + 1 && !1 == this.isleap ? (--a, this.isleap = !0, c = leapdays(this.year)) : c = monthdays(this.year, a), !0 == this.isleap && a == b + 1 && (this.isleap = !1), d -= c, !1 == this.isleap && this.moncyl++;
0 == d && (0 < b && a == b + 1) && (this.isleap ?
this.isleap = !1 : (this.isleap = !0, --a, --this.moncyl));
0 > d && (d += c, --a, --this.moncyl);
this.month = a;
this.day = d + 1
}
Lunar.prototype.getLunarMonth = function() {
var a = "",
a = 10 < this.month ? a + ("十" + STR1[this.month - 10]) : a + STR1[this.month],
a = a + "月";
"十二月" == a ? a = "腊月" : "一月" == a && (a = "正月");
return a
};
Lunar.prototype.getLunarDay = function() {
var a = "";
switch (this.day) {
case 10:
a += "初十";
break;
case 20:
a += "二十";
break;
case 30:
a += "三十";
break;
default:
a += STR2[Math.floor(this.day / 10)], a += STR1[Math.floor(this.day % 10)]
}
return a
};
Lunar.prototype.getLunar = function() {
return this.getLunarMonth() + this.getLunarDay()
};
Lunar.prototype.getMOD = function() {
var a = this.getLunarDay();
"初一" == a && (a = this.getLunarMonth());
return a
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment