富贵资源网 Design By www.hznty.com
本文实例讲述了JavaScript实现公历转农历功能。分享给大家供大家参考,具体如下:
完整代码(该源码使用在线工具http://tools.jb51.net/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月是大月还是小月"请输入年 月 日,并以空格隔开!", ""); 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) ""; 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.农历每个月的大小"#00ff00"/> <input type="button" value="开始转换" onclick="cnCanlender()"/> <div id="strcnCanlender" name="strcnCanlender"> </div> </body> </html>
运行效果图如下:
PS:这里再为大家推荐几款日历相关在线工具供大家参考:
网页万年历日历:
http://tools.jb51.net/bianmin/webwannianli
在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli
在线万年历日历:
http://tools.jb51.net/bianmin/wannianli
在线万年历黄历flash版:
http://tools.jb51.net/bianmin/flashwnl
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...