资讯专栏INFORMATION COLUMN

用JS 重新造了个轮子,农历计算脚本,有详细的注释

zhigoo / 3400人阅读

摘要:在重新造轮子之前,准备对性能优化下。最重要的,只写农历计算相关的计算,其他无功能,如果需要,通过本脚本。为除了闰月外的正常月份是大月还是小月,为天,为天。表示闰月是大月还是小月,仅当存在闰月的情况下有意义。

工作中有时需要农历计算,之前从网上找了个JS版本的(摘自wannianli.htm,网上导出都是),直接调用就可以了,非常方便。有优点就有缺点,该版本文件有点大(20KB以上);有很多代码用不到;代码注释不够直白;理解其原理有点麻烦。

之前用过多次,对原理也不是很清楚,最近项目需要,重新造了一遍轮子。包含源码注释的文件控制在7KB以内,压缩后再3KB以内。

在重新造轮子之前,准备对性能优化下。想到的可以优化的部分如下:

1、在年份计算天数时,每次都要循环计算与 1900 年 的 日期天数,将计算天数改为 2016-2-8(春节)。

2、每次计算 都要重新计算天数,考虑第一次计算当年天数后,加入缓存机制。

3、每次获取 农历 信息时,如果已经根据年份获取到 农历信息,可以传给相关函数,不用重新计算。

4、最重要的,只写农历计算相关的计算,其他无功能,如果需要,通过 requrie 本脚本。

造完轮子之后,发现能优化的程度有限,性能与之前提升不大(计算40次 大约都在 0.003 – 0.005 秒左右),最终 取消了 第2项 缓存(节省内存)。

</>复制代码

  1. /*
  2. 原文地址:http://www.miaoqiyuan.cn/p/lunar
  3. 源代码:http://www.miaoqiyuan.cn/products/lunar/lunar.js
  4. 压缩版:http://www.miaoqiyuan.cn/products/lunar/lunar.min.js
  5. */
  6. var lunar = {
  7. data : [
  8. /*
  9. 二进制形式
  10. xxxx xxxx xxxx xxxx xxxx
  11. 20-17 16-12 12-9 8-5 4-1
  12. 1-4: 表示当年有无闰年,有的话,为闰月的月份,没有的话,为0。
  13. 5-16:为除了闰月外的正常月份是大月还是小月,1为30天,0为29天。
  14. 注意:从1月到12月对应的是第16位到第5位。
  15. 17-20: 表示闰月是大月还是小月,仅当存在闰月的情况下有意义。
  16. 举个例子:
  17. 以我的生日1987年威力,1987年的数据是: 0x0af46
  18. 二进制:0000 1010 1111 0100 0110
  19. 从1月到12月的天数依次为:
  20. 5-16位 1 0 1 0 1 1 1 1 0 1 0 0
  21. 每月日数 30 29 30 29 30 30 30 30 29 30 29 29
  22. 对应月份 1 2 3 4 5 6 7 8 9 10 11 12
  23. 0110 (1-4位) 表示1987年有闰月,闰六月
  24. 0000 (17-20位) 存在闰月,本字段有效,表示闰小月 29天
  25. 补充闰月后的每月日期
  26. 每月日数 30 29 30 29 30 30 29 30 30 29 30 29 29
  27. 对应月份 1 2 3 4 5 6 闰 7 8 9 10 11 12
  28. */
  29. 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, //1900-1909
  30. 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, //1910-1919
  31. 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, //1920-1929
  32. 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, //1930-1939
  33. 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, //1940-1949
  34. 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, //1950-1959
  35. 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, //1960-1969
  36. 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, //1970-1979
  37. 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, //1980-1989
  38. 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, //1990-1999
  39. 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, //2000-2009
  40. 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, //2010-2019
  41. 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, //2020-2029
  42. 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, //2030-2039
  43. 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, //2040-2049
  44. 0x14b63 //2050
  45. ],
  46. //获取农历年份信息
  47. getData : function(year){
  48. return this.data[year - 1900];
  49. },
  50. //返回闰月是哪个月,没有闰月返回0
  51. getLeapMonth : function(year, lunarData){
  52. lunarData = lunarData || this.getData(year); //如果传入lunarData,为了提高性能,不再进行计算
  53. /*
  54. 1-4位表示闰月,例如:0010
  55. lunarData : xxxx xxxx xxxx xxxx 0010
  56. 0xf : 0000 0000 0000 0000 1111
  57. &运算 : 0000 0000 0000 0000 0010
  58. */
  59. return lunarData & 0xf;
  60. },
  61. //返回闰月天数
  62. getLeapDays : function(year, lunarData){
  63. lunarData = lunarData || this.getData(year);
  64. if( this.getLeapMonth(year, lunarData) ){
  65. /*
  66. 17-20位表示闰月是大小月,例如:0001
  67. lunarData : 0000 xxxx xxxx xxxx xxxx
  68. 0x10000 : 0001 0000 0000 0000 0000
  69. &运算 : 0000 0000 0000 0000 0000
  70. */
  71. return (lunarData & 0x10000) ? 30 : 29;
  72. }else{
  73. return 0;
  74. }
  75. },
  76. //农历某年某月的天数
  77. getMonthDays : function(year, month, lunarData){
  78. lunarData = lunarData || this.getData(year);
  79. /*
  80. 5-16位 表示每个月的天数
  81. lunarData : xxxx 0100 0100 0100 xxxx
  82. 0x8000 : 0000 1000 0000 0000 0000
  83. 0x8000>>8 : 0000 0000 0001 0000 0000
  84. &运算 : 0000 0000 0000 0000 0000
  85. */
  86. return (lunarData & 0x8000>>month) ? 30 : 29;
  87. },
  88. //农历年总天数
  89. getLunarDays : function(year, lunarData){
  90. lunarData = lunarData || this.getData(year);
  91. //如果存在 总天数 缓存,则返回缓存
  92. /*
  93. this.cacheLunarDays = this.cacheLunarDays || {};
  94. if( year in this.cacheLunarDays ){
  95. return this.cacheLunarDays[year];
  96. }
  97. */
  98. var days = 348; //本年的12个月,都当作小月处理。 12 x 29 = 348
  99. /*
  100. 5-16位 表示每个月的天数
  101. lunarData : xxxx 0100 0100 0100 xxxx
  102. 0x8000 : 0000 1000 0000 0000 0000
  103. &运算 : 0000 0000 0000 0000 0000
  104. 0x8 : 0000 0000 0000 0000 1000
  105. &运算 : 0000 0000 0000 0000 0000
  106. */
  107. for(var monthIndex = 0x8000; monthIndex > 0x8; monthIndex >>= 1){
  108. days += (lunarData & monthIndex) ? 1 : 0;
  109. }
  110. return days + this.getLeapDays(year, lunarData);
  111. /*
  112. this.cacheLunarDays[year] = days + this.getLeapDays(year, lunarData);
  113. return this.cacheLunarDays[year];
  114. */
  115. },
  116. //传入一个日期,计算农历信息
  117. toLunar : function(date, _date){
  118. //如果传入 _date,则将农历信息添加到 _date中
  119. var _date = _date || {};
  120. var currentYear = 2016; //当前年份
  121. var lunarData = this.getData(currentYear); //缓存 lunarData,节省性能
  122. var lunarDays = this.getLunarDays(currentYear, lunarData); //农历天数
  123. /*
  124. daysOffset == 相差天数
  125. 为了减少不必要的性能浪费(为什么要从1900算到今年),参考日期以2016年春节为准(2016-2-8)
  126. */
  127. var daysOffset = (new Date(date.getFullYear(), date.getMonth(), date.getDate()) - new Date(2016, 1, 8)) / 86400000;
  128. //获取年数
  129. if( daysOffset >= lunarDays ){
  130. //2017年和以后
  131. while( daysOffset >= lunarDays ){
  132. daysOffset -= lunarDays;
  133. lunarData = this.getData(++currentYear);
  134. lunarDays = this.getLunarDays(currentYear, lunarData);
  135. }
  136. }else if( daysOffset < 0 ){
  137. //2016年前
  138. while( daysOffset < 0 ){
  139. lunarData = this.getData(--currentYear);
  140. lunarDays = this.getLunarDays(currentYear, lunarData);
  141. daysOffset += lunarDays;
  142. }
  143. daysOffset++;
  144. }
  145. _date.lunarYear = currentYear;
  146. //本年是否为闰月
  147. var leapMonth = this.getLeapMonth(currentYear, lunarData);
  148. //获取月数
  149. var currentMonth, currentMonthDays;
  150. for(currentMonth = 1; currentMonth < 12 ; currentMonth++ ){
  151. _date.isLeap = false;
  152. ///如果有闰月
  153. if( leapMonth ){
  154. if( currentMonth > leapMonth ){
  155. currentMonth--;
  156. leapMonth = 0;
  157. _date.isLeap = true;
  158. }
  159. }
  160. currentMonthDays = this.getMonthDays(currentYear, currentMonth);
  161. if( daysOffset > currentMonthDays ){
  162. daysOffset -= currentMonthDays;
  163. }else{
  164. break;
  165. }
  166. }
  167. _date.lunarMonth = currentMonth;
  168. //获取日
  169. _date.lunarDay = daysOffset;
  170. return _date;
  171. },
  172. //返回今日信息
  173. today : function(){
  174. return this.toLunar(new Date());
  175. }
  176. };
  177. //如果需要模块导出
  178. //module.exports = lunar;

使用方法:

</>复制代码

  1. lunar.toLunar(new Date())
  2. /*
  3. Object {
  4. lunarYear: 2016, //农历年
  5. isLeap: false, //是否是闰月
  6. lunarMonth: 10, //农历月
  7. lunarDay: 10 //农历日
  8. }
  9. */

还可以传入JS对象,

</>复制代码

  1. var date = new Date();
  2. var _date = {
  3. Date : date,
  4. year : date.getFullYear(),
  5. month : date.getMonth() + 1,
  6. date : date.getDate(),
  7. day : date.getDay()
  8. };
  9. _date.value = [_date.year, _date.month, _date.date].map(function(n){
  10. n = n.toString();
  11. return n[1] ? n : "0" + n;
  12. }).join("-");
  13. /*
  14. Object {
  15. Date: Wed Nov 09 2016 18:56:09 GMT+0800 (中国标准时间),
  16. year: 2016,
  17. month: 11,
  18. date: 9,
  19. day: 3,
  20. value: "2016-11-09"
  21. */
  22. _date = lunar.toLunar(date, _date);
  23. /*
  24. Object {
  25. Date: Wed Nov 09 2016 18:56:09 GMT+0800 (中国标准时间),
  26. year: 2016,
  27. month: 11,
  28. date: 9,
  29. day: 3,
  30. value: "2016-11-09",
  31. isLeap: false,
  32. lunarDay: 10,
  33. lunarMonth: 10,
  34. lunarYear: 2016
  35. */

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/80823.html

相关文章

  • 尝试了个工具类库,名为 Diana

    摘要:的另一种形式测试踩坑之路代码覆盖率单元测试的代码覆盖率统计,是衡量测试用例好坏的一个的方法。 showImg(https://segmentfault.com/img/remote/1460000012564211?w=640&h=280); 项目地址: diana文档地址: http://muyunyun.cn/diana/ 造轮子的意义 为啥已经有如此多的前端工具类库还要自己造轮...

    zhichangterry 评论0 收藏0
  • 不到300行代码构建精简koa和koa-router(mini-koa)

    摘要:详细代码如下追踪赋值里面的是子路由设计子路由设计这个比较简单,每个子路由维护一个路由监听列表,然后通过调用的函数添加到主路由列表上。 showImg(https://segmentfault.com/img/bVbruD0?w=756&h=378); 前言 鉴于之前使用express和koa的经验,这两天想尝试构建出一个koa精简版,利用最少的代码实现koa和koa-router,同时...

    tuomao 评论0 收藏0
  • 原生js+css 实现 material design 点击效果

    摘要:最近想要在按钮上添加的点击效果,看了看,单纯的想要这种效果,而要引入一些不必要的内容觉得不划算,然后自己动手造了个轮子。项目地址上传不了图只能用了效果使用文字只要在内添加并且在内传入便可以实现水波纹效果。 最近想要在按钮上添加material design的点击效果,看了看muse-ui,单纯的想要这种效果,而要引入一些不必要的内容觉得不划算,然后自己动手造了个轮子。项目地址:gith...

    NickZhou 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<