本文實例講述了JavaScript實現公歷轉農歷功能。分享給大家供大家參考,具體如下:
完整代碼(該源碼使用在線工具http://tools.VeVB.COm/code/js進行了格式化處理,以便于讀者閱讀):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>公歷轉農歷</title><script type="text/javascript" language="javascript" >var CalendarData = new Array(20);var madd = new Array(12);var TheDate;var tgString = "甲乙丙丁戊己庚辛壬癸";var dzString = "子丑寅卯辰巳午未申酉戌亥";var numString = "一二三四五六七八九十";var monString = "正二三四五六七八九十冬臘";var weekString = "日一二三四五六";var sx = "鼠牛虎兔龍蛇馬羊猴雞狗豬";var cYear;var cMonth;var cDay;var cHour;var cDateString;var DateString;var Browser = navigator.appName;function init() {  CalendarData[0] = 0x41A95; //公元2001年;  CalendarData[1] = 0xD4A;  CalendarData[2] = 0xDA5;  CalendarData[3] = 0x20B55;  CalendarData[4] = 0x56A;  CalendarData[5] = 0x7155B;  CalendarData[6] = 0x25D;  CalendarData[7] = 0x92D;  CalendarData[8] = 0x5192B;  CalendarData[9] = 0xA95;  CalendarData[10] = 0xB4A;  CalendarData[11] = 0x416AA;  CalendarData[12] = 0xAD5;  CalendarData[13] = 0x90AB5;  CalendarData[14] = 0x4BA;  CalendarData[15] = 0xA5B;  CalendarData[16] = 0x60A57;  CalendarData[17] = 0x52B;  CalendarData[18] = 0xA93;  CalendarData[19] = 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;} //n月是大月還是小月?function getInfo() { //用戶輸入日期!  var arr = [];  strData = prompt("請輸入年 月 日,并以空格隔開!", "");  if (strData == null || strData == "") {    alert("輸入錯誤!請刷新!");    return false;  }  arr = strData.split(" ");  y = arr[0];  m = arr[1] - 1;  d = arr[2];  TheDate = new Date(y, m, d);} //getinfo();function e2c() {  getInfo();  var total, m, n, k;  var isEnd = false;  var tmp = TheDate.getYear();  if (tmp < 1900) tmp += 1900; //無用!  total = (tmp - 2001) * 365 + Math.floor((tmp - 2001) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 23; //2001年1月23是除夕;該句計算到起始年正月初一的天數  if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) total++; //當年是閏年且已過2月再加一天!  for (m = 0;; m++) {    k = (CalendarData[m] < 0xfff) ? 11 : 12; //起始年+m閏月嗎?    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 = 2001 + 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--; //該月是閏月后某個月!  }  cHour = Math.floor((TheDate.getHours() + 1) / 2);}function GetcDateString() {  var tmp = "";  tmp += tgString.charAt((cYear - 4) % 10);  //年干  tmp += dzString.charAt((cYear - 4) % 12); //年支  tmp += "年(";  tmp += sx.charAt((cYear - 4) % 12);  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);  if (cHour == 12) tmp += "夜";  tmp += dzString.charAt((cHour) % 12);  tmp += "時"; //幾時!  cDateString = tmp;  return tmp;}function GetDateString() //公歷時間!{  var tmp = "";  var t1 = TheDate.getYear();  if (t1 < 1900) t1 += 1900;  tmp += t1 + "-" + (TheDate.getMonth() + 1) + "-" + TheDate.getDate() + " " + TheDate.getHours() + ":" + ((TheDate.getMinutes() < 10) ? "0": "") + TheDate.getMinutes() + " 星期" + weekString.charAt(TheDate.getDay());  DateString = tmp;  return tmp;}function cnCanlender() {  init();  e2c();  GetDateString();  GetcDateString();  strcnCanlender.innerText = DateString + "/n" + cDateString; //顯示!}</script></head><body>要保存一年的信息,其實只要兩個信息就可以了:1.農歷每個月的大小?<br/>2.今年是否有閏月.<br/>3.閏幾月以及閏月的大小.<br/><br/>用一個整數來保存這些信息就足夠了.<br/>具體的方法是:用一位來表示一個月的大?<br/> 笤錄俏?,小月記為0,這樣就用掉12 位(無閏月)或13 位(有閏月),再用高4位來表示閏月的月份,沒有閏月記為0.<br/>比如說,2 0 0 0 年的信息數據是0 x C 9 6 ,化成二進制就是110010010110B,表示的含義是指1、2、5、8、10、11月大,其余月小.<br/>2001 年的農歷信息數據是0x41A95,其中4表示今年閏4月,月份大小信息就是0x1A95(因為閏月,所以有13 位),具體的就是1、2、4、5、8、10、12月大,其余月份?<br/>?x1A95=1101010010101B),4月的后面那一個0表示的是閏4月?<br/> 幼諾哪歉?表示5月大.<br/>這樣就可以用一個數組來保存這些信息.<br/>在我的JavaScript腳本程序中是用CalendarData[]這個數組來保存這些信息的.<br/>下面就是公歷轉換成農歷的具體算法:1.計算出所求時間到起始年正月初一的天數.<br/>2.從起始年份開始,減去每一月的天數,一直到剩余天數沒有下一個月多為止.<br/>此時,CalendarData[]的下標到了多少,就是減去了多少年,用起始年份加上這個下標就可以得到農歷年份,然后看減去了幾個月.<br/>如果本年不閏月或者閏月還在后面,就可以直接得到農歷月份,如果在閏月月份數后面一個月,則這個月就是閏月,如果在閏月的后面,則要減去1 才能得到月份數.<br/>剩余的天數就是農歷日,農歷時用(公歷時+1)/2 就可以簡單地得到了.<br/> <hr color="#00ff00"/><input type="button" value="開始轉換" onclick="cnCanlender()"/><div id="strcnCanlender" name="strcnCanlender"></div></body></html>運行效果圖如下:

PS:這里再為大家推薦幾款日歷相關在線工具供大家參考:
網頁萬年歷日歷:
http://tools.VeVB.COm/bianmin/webwannianli
在線陰歷/陽歷轉換工具:
http://tools.VeVB.COm/bianmin/yinli2yangli
在線萬年歷日歷:
http://tools.VeVB.COm/bianmin/wannianli
在線萬年歷黃歷flash版:
http://tools.VeVB.COm/bianmin/flashwnl
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript時間與日期操作技巧總結》《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答