共 3 条评论关于"Javascript时间戳不用插件完美转换成阴历农历格式"
最新评论
似乎在实际项目中我们不常见需要将时间转换成阴历,但存在必有道理,下面说下怎么转换成阴历
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | function showCal() { var D = new Date(); var yy = D.getFullYear(); var mm = D.getMonth() + 1; var dd = D.getDate(); var ww = D.getDay(); var ss = parseInt(D.getTime() / 1000); if (yy < 100) yy = '19' + yy; return GetLunarDay(yy, mm, dd); } //定义全局变量 var CalendarData = new Array(100); var madd = new Array(12); var tgString = '甲乙丙丁戊己庚辛壬癸'; var dzString = '子丑寅卯辰巳午未申酉戌亥'; var numString = '一二三四五六七八九十'; var monString = '正二三四五六七八九十冬腊'; var weekString = '日一二三四五六'; var sx = '鼠牛虎兔龙蛇马羊猴鸡狗猪'; var cYear, cMonth, cDay, TheDate; CalendarData = new Array( 0xa4b, 0x5164b, 0x6a5, 0x6d4, 0x415b5, 0x2b6, 0x957, 0x2092f, 0x497, 0x60c96, 0xd4a, 0xea5, 0x50da9, 0x5ad, 0x2b6, 0x3126e, 0x92e, 0x7192d, 0xc95, 0xd4a, 0x61b4a, 0xb55, 0x56a, 0x4155b, 0x25d, 0x92d, 0x2192b, 0xa95, 0x71695, 0x6ca, 0xb55, 0x50ab5, 0x4da, 0xa5b, 0x30a57, 0x52b, 0x8152a, 0xe95, 0x6aa, 0x615aa, 0xab5, 0x4b6, 0x414ae, 0xa57, 0x526, 0x31d26, 0xd95, 0x70b55, 0x56a, 0x96d, 0x5095d, 0x4ad, 0xa4d, 0x41a4d, 0xd25, 0x81aa5, 0xb54, 0xb6a, 0x612da, 0x95b, 0x49b, 0x41497, 0xa4b, 0xa164b, 0x6a5, 0x6d4, 0x615b4, 0xab6, 0x957, 0x5092f, 0x497, 0x64b, 0x30d4a, 0xea5, 0x80d65, 0x5ac, 0xab6, 0x5126d, 0x92e, 0xc96, 0x41a95, 0xd4a, 0xda5, 0x20b55, 0x56a, 0x7155b, 0x25d, 0x92d, 0x5192b, 0xa95, 0xb4a, 0x416aa, 0xad5, 0x90ab5, 0x4ba, 0xa5b, 0x60a57, 0x52b, 0xa93, 0x40e95 ); madd[0] = 0; madd[1] = 31; madd[2] = 59; madd[3] = 90; madd[4] = 120; madd[5] = 151; madd[6] = 181; madd[7] = 212; madd[8] = 243; madd[9] = 273; madd[10] = 304; madd[11] = 334; function GetBit(m, n) { return (m >> n) & 1; } //农历转换 function e2c() { TheDate = arguments.length != 3 ? new Date() : new Date(arguments[0], arguments[1], arguments[2]); var total, m, n, k; var isEnd = false; var tmp = TheDate.getYear(); if (tmp < 1900) { tmp += 1900; } total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 38; if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) { total++; } for (m = 0;; m++) { k = CalendarData[m] < 0xfff ? 11 : 12; for (n = k; n >= 0; n--) { if (total <= 29 + GetBit(CalendarData[m], n)) { isEnd = true; break; } total = total - 29 - GetBit(CalendarData[m], n); } if (isEnd) break; } cYear = 1921 + m; cMonth = k - n + 1; cDay = total; if (k == 12) { if (cMonth == Math.floor(CalendarData[m] / 0x10000) + 1) { cMonth = 1 - cMonth; } if (cMonth > Math.floor(CalendarData[m] / 0x10000) + 1) { cMonth--; } } } function GetcDateString() { var tmp = ''; /*显示农历年:( 如:甲午(马)年 )*/ /*tmp+=tgString.charAt((cYear-4)); tmp+=dzString.charAt((cYear-4)); tmp+="("; tmp+=sx.charAt((cYear-4)); tmp+=")年 ";*/ if (cMonth < 1) { tmp += '(闰)'; tmp += monString.charAt(-cMonth - 1); } else { tmp += monString.charAt(cMonth - 1); } tmp += '月'; tmp += cDay < 11 ? '初' : cDay < 20 ? '十' : cDay < 30 ? '廿' : '三十'; if (cDay % 10 != 0 || cDay == 10) { tmp += numString.charAt((cDay - 1) % 10); } return tmp; } function GetLunarDay(solarYear, solarMonth, solarDay) { //solarYear = solarYear<1900?(1900+solarYear):solarYear; if (solarYear < 1921 || solarYear > 2020) { return ''; } else { solarMonth = parseInt(solarMonth) > 0 ? solarMonth - 1 : 11; e2c(solarYear, solarMonth, solarDay); return '阴历' + GetcDateString(); } } this.lunarCalendar = '农历' + showCal() + ' '; |
上面代码本来是用在vue.js项目中的,直接丢到函数中,然后去调用即可,通过this.lunarCalendar就能拿到当前时间转换好的阴历格式,你也可以改成自己去传参形式得到你想转换的时间。
上一篇:wordPress阿里云虚拟主机升级php7.x报错
下一篇:Vue.js项目Vue-Router有多少种路由重定向方式?
最新评论
支付宝扫一扫打赏
微信扫一扫打赏
if (solarYear 2020) {
return '';
}
这里为啥要大于2020 return ''呢
@fuTure时间区间问题,因为目前是2020年,你所选不可能超出这个时间吧,当然,代码是根据你的业务来,你可以去掉限制
文章很不错!