相信有人會在頁面中中選擇日期時,需要知道選擇日期的農曆!如果是Java的話,在後台一下子就可以給轉換掉,但是頁面上,通過JavaScript來轉換的話,不知道大伙兒有沒有好的想法呢?剛好,前一段時間來,朋友問了這麼一個問題,然後就寫了一個demo給她,還行吧!希望能幫到有需要的各位,好啦,直接上代碼,不想閑扯:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>mydate.jsp</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="My97DatePicker/skin/WdatePicker.css"> <style type="text/css"> body { font-family: 微軟雅黑; font-size: 14px; } </style> <script type="text/javascript" src="My97DatePicker/calendar-converter.js"></script> <script type="text/javascript" src="My97DatePicker/WdatePicker.js"></script> <script type="text/javascript" src="jQuery/jquery-1.6.js"></script> </head> <body> <div id="calendar"> <div id="showCalendar"></div> <div><span id="showSolarCalendar"></span>[<span id="showLunarCalendar"></span>]</div> </div> </body> <script type="text/javascript"> function getLunarCalendar(date){ var cc = new CalendarConverter; var result = cc.solar2lunar(date); return result.cYear + "(" + result.lunarYear + ")年" + (result.isLeap?"閏":"") + result.lunarMonth + "月" + result.lunarDay + " " + result.lunarFestival + " 星期" + result.week; } var nowDate = new Date(); document.getElementById('showSolarCalendar').innerHTML = nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate(); document.getElementById('showLunarCalendar').innerHTML = getLunarCalendar(nowDate); WdatePicker({ eCont:'showCalendar', onpicked:function(dp){ document.getElementById('showSolarCalendar').innerHTML = dp.cal.getDateStr(); document.getElementById('showLunarCalendar').innerHTML = getLunarCalendar(new Date(dp.cal.getDateStr())); } }); </script> </html>
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/
代碼就已經貼上了,關鍵這裡有一個js:calendar-converter.js,主要是用來西曆轉換農曆的!附上該資源下載地址:http://download.csdn.net/download/luo201227/7616913
最後給大伙兒貼上幾張效果圖:
1:
2:
3: