Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30462425
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-14 11:17:21

  1. // JS日历转化公用类
  2. // @date 2010-06-01

  3. function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13) {
  4.     this.BaseDays = d; /* 1 月 1 日到正月初一的累计日 */
  5.     this.Intercalation = i; /* 闰月月份. 0==此年沒有闰月 */
  6.     this.BaseWeekday = w; /* 此年 1 月 1 日为星期减 1 */
  7.     this.BaseKanChih = k; /* 此年 1 月 1 日之干支序号减 1 */
  8.     this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */
  9. }

  10. // 闰年判断.返回1 或 0
  11. function GetLeap(year) {
  12.     return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1 : 0;
  13. }

  14. // 主类入口
  15. // @param {Date} new Date(y,m,d)
  16. // @param {Boolean} 0: 农历-->公历 1: 公历 --> 农历
  17. // @return {String} 见example
  18. // @example
  19. // CalConvert(new Date(2010,3,19), false) 即农历的四月十九转为公历 结果为 2010-06-01
  20. // CalConvert(new Date(2010,5,1), true) 即公历的6月1日转为农历 结果为 2010年四月十九
  21. //
  22. function CalConvert(date, sign) {
  23.     // 日期上下限
  24.     FIRSTYEAR = 1936;
  25.     LASTYEAR = 2031;
  26.     // 返回结果值
  27.     RESULT = 0;
  28.     // 对照表
  29.     LunarCal = [
  30.         new tagLunarCal(23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), /* 1936 */
  31.         new tagLunarCal( 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ),
  32.         new tagLunarCal( 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ),
  33.         new tagLunarCal( 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  34.         new tagLunarCal( 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 1940 */
  35.         new tagLunarCal( 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  36.         new tagLunarCal( 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
  37.         new tagLunarCal( 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
  38.         new tagLunarCal( 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1944 */
  39.         new tagLunarCal( 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
  40.         new tagLunarCal( 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ),
  41.         new tagLunarCal( 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  42.         new tagLunarCal( 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), /* 1948 */
  43.         new tagLunarCal( 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  44.         new tagLunarCal( 47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 ),
  45.         new tagLunarCal( 36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
  46.         new tagLunarCal( 26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ), /* 1952 */
  47.         new tagLunarCal( 44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 ),
  48.         new tagLunarCal( 33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ),
  49.         new tagLunarCal( 23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
  50.         new tagLunarCal( 42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), /* 1956 */
  51.         new tagLunarCal( 30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
  52.         new tagLunarCal( 48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
  53.         new tagLunarCal( 38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
  54.         new tagLunarCal( 27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 1960 */
  55.         new tagLunarCal( 45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ),
  56.         new tagLunarCal( 35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
  57.         new tagLunarCal( 24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
  58.         new tagLunarCal( 43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), /* 1964 */
  59.         new tagLunarCal( 32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
  60.         new tagLunarCal( 20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
  61.         new tagLunarCal( 39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
  62.         new tagLunarCal( 29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 1968 */
  63.         new tagLunarCal( 47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
  64.         new tagLunarCal( 36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
  65.         new tagLunarCal( 26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ),
  66.         new tagLunarCal( 45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1972 */
  67.         new tagLunarCal( 33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 ),
  68.         new tagLunarCal( 22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
  69.         new tagLunarCal( 41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ),
  70.         new tagLunarCal( 30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ), /* 1976 */
  71.         new tagLunarCal( 48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 ),
  72.         new tagLunarCal( 37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
  73.         new tagLunarCal( 27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
  74.         new tagLunarCal( 46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 1980 */
  75.         new tagLunarCal( 35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 ),
  76.         new tagLunarCal( 24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
  77.         new tagLunarCal( 43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
  78.         new tagLunarCal( 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), /* 1984 */
  79.         new tagLunarCal( 50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  80.         new tagLunarCal( 39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 ),
  81.         new tagLunarCal( 28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ),
  82.         new tagLunarCal( 47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 1988 */
  83.         new tagLunarCal( 36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ),
  84.         new tagLunarCal( 26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 ),
  85.         new tagLunarCal( 45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 ),
  86.         new tagLunarCal( 34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 ), /* 1992 */
  87.         new tagLunarCal( 22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  88.         new tagLunarCal( 40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  89.         new tagLunarCal( 30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 ),
  90.         new tagLunarCal( 49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 ), /* 1996 */
  91.         new tagLunarCal( 37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
  92.         new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ),
  93.         new tagLunarCal( 46, 0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
  94.         new tagLunarCal( 35, 0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
  95.         new tagLunarCal( 23, 4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  96.         new tagLunarCal( 42, 0, 1, 4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  97.         new tagLunarCal( 31, 0, 2, 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  98.         new tagLunarCal( 21, 2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
  99.         new tagLunarCal( 39, 0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
  100.         new tagLunarCal( 28, 7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
  101.         new tagLunarCal( 48, 0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
  102.         new tagLunarCal( 37, 0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
  103.         new tagLunarCal( 25, 5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  104.         new tagLunarCal( 44, 0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  105.         new tagLunarCal( 33, 0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  106.         new tagLunarCal( 22, 4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */

  107.         new tagLunarCal( 40, 0, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
  108.         new tagLunarCal( 30, 9, 2, 7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
  109.         new tagLunarCal( 49, 0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
  110.         new tagLunarCal( 38, 0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
  111.         new tagLunarCal( 27, 6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
  112.         new tagLunarCal( 46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
  113.         new tagLunarCal( 35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
  114.         new tagLunarCal( 24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
  115.         new tagLunarCal( 42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
  116.         new tagLunarCal( 31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
  117.         new tagLunarCal( 21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
  118.         new tagLunarCal( 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
  119.         new tagLunarCal( 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
  120.         new tagLunarCal( 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
  121.         new tagLunarCal( 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
  122.         new tagLunarCal( 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
  123.         new tagLunarCal( 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
  124.         new tagLunarCal( 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
  125.         new tagLunarCal( 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];

  126.         /* 西曆年每月之日數 */
  127.         SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
  128.         /* 西曆年每月之累積日數, 平年與閏年 */
  129.         SolarDays = [
  130.             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
  131.             0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];

  132.         AnimalIdx = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
  133.         LocationIdx = [ "南", "東", "北", "西" ];

  134.         var func = sign ? function(d) {
  135.                 function getBit(m, n) { return (m >> n) & 1; }
  136.                 var mons = "正二三四五六七八九十冬腊", Cal = [0x41A95,0xD4A,0xDA5,0x20B55,0x56A,0x7155B,0x25D,0x92D,0x5192B,0xA95,0xB4A,0x416AA,0xAD5,0x90AB5,0x4BA,0xA5B,0x60A57,0x52B,0xA93,0x40E95];
  137.                 var total, m, n, k, isEnd = false, t = d.getYear();
  138.                 if (t < 1900) t += 1900;
  139.                 total = (t - 2001) * 365 + Math.floor((t - 2001) / 4) + [0,31,59,90,120,151,181,212,243,273,304,334][d.getMonth()] + d.getDate() - 23;
  140.                 if(d.getYear() % 4 == 0 && d.getMonth() > 1) total++;
  141.                 for(m = 0; m < 1000; m++){
  142.                     k = (Cal[m] < 0xfff) ? 11 : 12;
  143.                     for(n = k; n >= 0; n--) {
  144.                         if(total <= 29 + getBit(Cal[m],n)){
  145.                             isEnd=true;
  146.                             break;
  147.                         }
  148.                         total = total - 29 - getBit(Cal[m],n);
  149.                     }
  150.                     if(isEnd) break;
  151.                 }
  152.                 var cYear = 2001 + m, cMonth = k - n + 1;
  153.                 if(k == 12){
  154.                     if(cMonth == Math.floor(Cal[m] / 0x10000) + 1)
  155.                         cMonth = 1 - cMonth;
  156.                     if(cMonth > Math.floor(Cal[m] / 0x10000) + 1)
  157.                         cMonth--;
  158.                 }
  159.                 var t = "";
  160.                 if(cMonth < 1){
  161.                     t += "闰";
  162.                     t += mons.charAt(-cMonth -1);
  163.                 }
  164.                 else t += mons.charAt(cMonth - 1);
  165.                 t += "月";
  166.                 t += (total < 11) ? "初" : ((total < 20) ? "十" :( (total<30) ? "廿" : "卅"));
  167.                 if(total % 10 != 0 || total == 10) t += "一二三四五六七八九十".charAt((total-1) % 10);
  168.                 return t;
  169.         } : function(d) {
  170.                 var LunarYear = d.getFullYear(),
  171.                     LunarMonth = d.getMonth() + 1,
  172.                     LunarDate = d.getDate(),
  173.                     acc = 0,
  174.                     leap, SolarDate, y, im, lm;
  175.                 if (LunarYear < FIRSTYEAR || LunarYear >= LASTYEAR) {
  176.                     alert('只处理1936 - 2031有效年份');
  177.                     return false;
  178.                 }
  179.                 y = LunarYear - FIRSTYEAR;
  180.                 im = LunarCal[y].Intercalation;
  181.                 lm = LunarMonth;
  182.                 if (lm < 0) {
  183.                     if (lm != -im) {
  184.                         alert('月份无效');
  185.                         return false;
  186.                     }
  187.                 }
  188.                 else if (lm < 1 || lm > 12) {
  189.                     alert('月份无效');
  190.                     return false;
  191.                 }
  192.                 if (im != 0) {
  193.                     if (lm > im) {
  194.                         lm++;
  195.                     } else if (lm == -im) {
  196.                         lm = im + 1;
  197.                     }
  198.                 }
  199.                 lm--;

  200.                 if (LunarDate > LunarCal[y].MonthDays[lm] + 29) {
  201.                     alert('农历日期不正确');
  202.                     return false;
  203.                 }

  204.                 for (i = 0; i < lm; i++) {
  205.                     acc += LunarCal[y].MonthDays[i] + 29;
  206.                 }
  207.                 acc += LunarCal[y].BaseDays + LunarDate;

  208.                 leap = GetLeap(LunarYear);
  209.                 for (i = 13; i >= 0; i--) {
  210.                     if (acc > SolarDays[leap * 14 + i])
  211.                         break;
  212.                 }
  213.                 SolarDate = acc - SolarDays[leap * 14 + i];

  214.                 if (i <= 11) {
  215.                     SolarYear = LunarYear;
  216.                     SolarMonth = i + 1;
  217.                 } else {
  218.                     SolarYear = LunarYear + 1;
  219.                     SolarMonth = i - 11;
  220.                 }

  221.                 leap = GetLeap(SolarYear);
  222.                 y = SolarYear - FIRSTYEAR;

  223.                 acc = SolarDays[leap * 14 + SolarMonth - 1] + SolarDate;

  224.                 weekday = (acc + LunarCal[y].BaseWeekDay) % 7;
  225.                 kc = acc + LunarCal[y].BaseKanChih;
  226.                 kan = kc % 10;
  227.                 chih = kc % 12;
  228.                 return SolarYear + '-' + SolarMonth + '-' + SolarDate;
  229.         }

  230.     return func(date);
  231. }

  232. alert(CalConvert(new Date(2010,3,19), false)); // 2010年农历四月十九 -> 2010-06-01
  233. alert(CalConvert(new Date(2010,5,1), true));
最近要用到节气的计算.所以又扩展了一下

  1. /**
  2.  * 计算农历
  3.  * @param {Date} 日期
  4.  * @return {string} [2009,腊月,十二,(是否闰月true/false),己丑,戊寅,丙子,立秋(没有则为空)]
  5.  */
  6. function lunar(t){
  7.     var __nongli = {
  8.         L: [0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0],
  9.         Jq: [[19, 19, 19, 21, 23, 24, 27, 29, 1, 1, 30, 30], [29, 30, 30, 2, 4, 6, 8, 10, 11, 12, 12, 11], [12, 11, 12, 13, 14, 17, 18, 20, 22, 22, 23, 22], [23, 23, 23, 24, 26, 27, 30, 2, 3, 3, 4, 4], [4, 4, 4, 6, 7, 8, 11, 12, 14, 15, 14, 15], [14, 15, 15, 17, 18, 20, 22, 23, 25, 25, 26, 25], [26, 25, 27, 27, 29, 2, 3, 4, 6, 6, 7, 6], [7, 7, 8, 8, 11, 12, 14, 16, 17, 17, 17, 18], [17, 17, 18, 20, 21, 23, 26, 27, 29, 29, 28, 28], [28, 28, 28, 1, 2, 5, 7, 8, 10, 10, 10, 10], [10, 9, 9, 11, 13, 15, 17, 19, 20, 21, 21, 21], [21, 21, 21, 22, 24, 26, 27, 1, 1, 2, 2]],
  10.         G: ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"],
  11.         Z: ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"],
  12.         M: ["正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"],
  13.         TM: "0|gd4|wrn|1d98|1tuh|2akm|2rfn|38g9|3plp|46vz|4o9k|55px|5n73|64o5|6m37|73fd|7kna|81qe|8io7|8zgq|9g4b|9wnk|ad3g|ath2|".split('|'),
  14.         TN: "小寒|大寒|立春|雨水|惊蛰|春分|清明|谷雨|立夏|小满|芒种|夏至|小暑|大暑|立秋|处暑|白露|秋分|寒露|霜降|立冬|小雪|大雪|冬至".split('|'),
  15.         _GT: function(y, m, d) {
  16.             var T = this, _tm = T.TM, _tn = T.TN;
  17.             for (var i = 24; i--;) _tm[i] = parseInt(_tm[i], 36);
  18.             function term(y, n){
  19.                 var d=new Date((31556925974.7*(y-1900)+T.TM[n]*60000)+Date.UTC(1900,0,6,2,5) );
  20.                 return {m:d.getUTCMonth(),d:d.getUTCDate()}
  21.             };
  22.             for (var i=T.TN.length,x;i--;){
  23.                 x=term(y,i);
  24.                 if(x.m==m-1&&x.d==d)return T.TN[i];
  25.             };
  26.             return '';
  27.         },
  28.         _ZG: function(n){
  29.             return (this.G[n % 10] + this.Z[n % 12]);
  30.         },
  31.         _Y: function(y){
  32.             var i, sum = 348;
  33.             for (i = 0x8000; i > 0x8; i >>= 1) {
  34.                 sum += (this.L[y - 1900] & i) ? 1 : 0;
  35.             }
  36.             return (sum + this._D(y));
  37.         },
  38.         _D: function(y){
  39.             if (this._M(y)) {
  40.                 return ((this.L[y - 1900] & 0x10000) ? 30 : 29);
  41.             }
  42.             else {
  43.                 return (0);
  44.             }
  45.         },
  46.         _M: function(y){
  47.             return (this.L[y - 1900] & 0xf);
  48.         },
  49.         _mD: function(y, m){
  50.             return ((this.L[y - 1900] & (0x10000 >> m)) ? 30 : 29);
  51.         },
  52.         _lun: function(t){
  53.             var i, leap = 0, temp = 0, T = this, ofs = Math.floor((t.getTime() + 2206425600000) / 86400000);
  54.             var dayCyl = ofs + 40, monCyl = 14;
  55.             var _yy = t.getFullYear(), _mm = t.getMonth()+1, _dd = t.getDate();
  56.             for (i = 1900; i < 2050 && ofs > 0; i++) {
  57.                 temp = T._Y(i);
  58.                 ofs -= temp;
  59.                 monCyl += 12;
  60.             }
  61.             if (ofs < 0) {
  62.                 ofs += temp;
  63.                 i--;
  64.                 monCyl -= 12;
  65.             }
  66.             year = i;
  67.             yearCyl = i - 1864;
  68.             leap = T._M(i);
  69.             isLeap = false;
  70.             for (i = 1; i < 13 && ofs > 0; i++) {
  71.                 if (leap > 0 && i == (leap + 1) && isLeap == false) {
  72.                     --i;
  73.                     isLeap = true;
  74.                     temp = T._D(year);
  75.                 }
  76.                 else {
  77.                     temp = T._mD(year, i);
  78.                 }
  79.                 if (isLeap == true && i == (leap + 1))
  80.                     isLeap = false;
  81.                 ofs -= temp;
  82.                 if (isLeap == false)
  83.                     monCyl++;
  84.             }
  85.             if (ofs == 0 && leap > 0 && i == leap + 1) {
  86.                 if (isLeap) {
  87.                     isLeap = false;
  88.                 }
  89.                 else {
  90.                     isLeap = true;
  91.                     --i;
  92.                     --monCyl;
  93.                 }
  94.             }
  95.             if (ofs < 0)
  96.                 ofs += temp;
  97.             --i;
  98.             --monCyl;
  99.             day = ofs + 1, month = i;
  100.             var tday = (day < 11) ? "初" : ((day < 20) ? "十" : ((day < 30) ? "廿" : "卅"));
  101.             if (day % 10 != 0 || day == 10)
  102.                 tday += "一二三四五六七八九十".charAt((day - 1) % 10);
  103.             var _k = (day == 1) ? (month + 1) : month, k = T.Jq[year - 1999][_k], l = T.Jq[year - 1999][_k + 1], _m = (k - l);
  104.             if (_m < 0)
  105.                 _m = -_m;
  106.             if ((day >= k && parseInt(_m) < 20) || day == 1)
  107.                 monCyl += 1;
  108.             return [year, T.M[_k - 1] + "月", tday, isLeap, T._ZG(yearCyl), T._ZG(monCyl), T._ZG(dayCyl), T._GT(_yy, _mm, _dd)];
  109.         }
  110.     }
  111.     return __nongli._lun(t);
  112. }

关于节气的计算:

农历中一种日月兼顾的历法, 
月份是以月为历, 
节气是以日为历, 
地公转轨道是一个以太阳为一个焦点的椭圆, 
以近日点为0度,以太阳为中心, 
将地球公转所扫过的角度每十五度的界点所在日期便是一个节气。 
(可以运用立体几何算出,春分秋分大阳直射赤道,地球任意一经纬处日夜时间相等;夏至冬至太阳直射回归线[假设地球是个圆球]) 
以上是基本知识,再分析一下代码。 


代码1。 
var   sTermInfo   =   new  Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758) 
//每年的正小寒点到各节期正节期点(即十五度倍数点)的分种数。 
//地球公转每年都一样 
//由于公转轨道是椭圆,故这个数列并不是准确的等差数列 


代码2 
//=====   某年的第n个节气为几日(从0小寒起算) 
function   sTerm(y,n)   { 
      var   offDate   =   new   Date(   (   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )   +   Date.UTC(1900,0,6,2,5)   ) 
      return(offDate.getUTCDate()) 


31556925974.7为地球公转周期,是毫秒。(如果换算成天,是265.242199天,所以每四百年就有97个闰年,同样,我们也可以看到,如果这样做的话,一百三十万年后,我们的公历算法将有一天的误差) 

(   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )----表示y年的第n个节气点(15倍数度点)距1900年的小寒点的毫秒数。 
Date.UTC(1900,0,6,2,5)----表示1900年一年六日两点五分是正小寒点。 
以上两者相加得到的是:y年的第n个节气点时点的int值 

var   offDate   =   new   Date(   (   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )   +   Date.UTC(1900,0,6,2,5)   )----得出毫秒对应的日期 

 

独立提取的节气计算程序:


  1. var sTermMap = '0|gd4|wrn|1d98|1tuh|2akm|2rfn|38g9|3plp|46vz|4o9k|55px|5n73|64o5|6m37|73fd|7kna|81qe|8io7|8zgq|9g4b|9wnk|ad3g|ath2|'.split('|');
  2. var sTermNames ="小寒|大寒|立春|雨水|惊蛰|春分|清明|谷雨|立夏|小满|芒种|夏至|小暑|大暑|立秋|处暑|白露|秋分|寒露|霜降|立冬|小雪|大雪|冬至".split('|');

  3. for (var i = 24; i--;) sTermMap[i] = parseInt(sTermMap[i], 36);
  4. function getTerm(y,m,d){
  5.     function term(y,n){
  6.         var d=new Date((31556925974.7*(y-1900)+sTermMap[n]*60000)+Date.UTC(1900,0,6,2,5) );
  7.         return {m:d.getUTCMonth(),d:d.getUTCDate()}
  8.     };
  9.     for (var i=sTermNames.length,x;i--;){
  10.         x=term(y,i);
  11.         if(x.m==m-1&&x.d==d)return sTermNames[i];
  12.     };
  13.     return '';
  14. };

阅读(1987) | 评论(0) | 转发(0) |
0

上一篇:parseUrl

下一篇:数组去重

给主人留下些什么吧!~~