[转载]获取当前日期和农历的js代码

原文地址:

http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html

获取当前日期时间:

 1 function GetCurrentDateTime() {
 2     var d = new Date();
 3     var year = d.getFullYear();
 4     var month = d.getMonth() + 1;
 5     var date = d.getDate();
 6     var week = d.getDay();
 7     var hours = d.getHours();
 8     var minutes = d.getMinutes();
 9     var seconds = d.getSeconds();
10     var ms = d.getMilliseconds();
11     var curDateTime = year;
12     if (month > 9)
13         curDateTime = curDateTime + "年" + month;
14     else
15         curDateTime = curDateTime + "年0" + month;
16     if (date > 9)
17         curDateTime = curDateTime + "月" + date + "日";
18     else
19         curDateTime = curDateTime + "月0" + date + "日";
20     if (hours > 9)
21         curDateTime = curDateTime + " " + hours;
22     else
23         curDateTime = curDateTime + " 0" + hours;
24     if (minutes > 9)
25         curDateTime = curDateTime + ":" + minutes;
26     else
27         curDateTime = curDateTime + ":0" + minutes;
28     if (seconds > 9)
29         curDateTime = curDateTime + ":" + seconds;
30     else
31         curDateTime = curDateTime + ":0" + seconds;
32     var weekday = "";
33     if (week == 0)
34         weekday = "星期日";
35     else if (week == 1)
36         weekday = "星期一";
37     else if (week == 2)
38         weekday = "星期二";
39     else if (week == 3)
40         weekday = "星期三";
41     else if (week == 4)
42         weekday = "星期四";
43     else if (week == 5)
44         weekday = "星期五";
45     else if (week == 6)
46         weekday = "星期六";
47     curDateTime = curDateTime + " " + weekday;
48     return curDateTime;
49 }

调用GetCurrentDateTime()方法可以获取当前时间

获取当前日期的农历: 

  1 var CalendarData = new Array(100);
  2 var madd = new Array(12);
  3 var tgString = "甲乙丙丁戊己庚辛壬癸";
  4 var dzString = "子丑寅卯辰巳午未申酉戌亥";
  5 var numString = "一二三四五六七八九十";
  6 var monString = "正二三四五六七八九十冬腊";
  7 var weekString = "日一二三四五六";
  8 var sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
  9 var cYear, cMonth, cDay, TheDate;
 10 CalendarData = new Array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95);
 11 madd[0] = 0;
 12 madd[1] = 31;
 13 madd[2] = 59;
 14 madd[3] = 90;
 15 madd[4] = 120;
 16 madd[5] = 151;
 17 madd[6] = 181;
 18 madd[7] = 212;
 19 madd[8] = 243;
 20 madd[9] = 273;
 21 madd[10] = 304;
 22 madd[11] = 334;
 23 
 24 function GetBit(m, n) {
 25     return (m >> n) & 1;
 26 }
 27 function e2c() {
 28     TheDate = (arguments.length != 3) ? new Date() : new Date(arguments[0], arguments[1], arguments[2]);
 29     var total, m, n, k;
 30     var isEnd = false;
 31     var tmp = TheDate.getYear();
 32     if (tmp < 1900) {
 33         tmp += 1900;
 34     }
 35     total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 38;
 36 
 37     if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) {
 38         total++;
 39     }
 40     for (m = 0; ; m++) {
 41         k = (CalendarData[m] < 0xfff) ? 11 : 12;
 42         for (n = k; n >= 0; n--) {
 43             if (total <= 29 + GetBit(CalendarData[m], n)) {
 44                 isEnd = true; break;
 45             }
 46             total = total - 29 - GetBit(CalendarData[m], n);
 47         }
 48         if (isEnd) break;
 49     }
 50     cYear = 1921 + m;
 51     cMonth = k - n + 1;
 52     cDay = total;
 53     if (k == 12) {
 54         if (cMonth == Math.floor(CalendarData[m] / 0x10000) + 1) {
 55             cMonth = 1 - cMonth;
 56         }
 57         if (cMonth > Math.floor(CalendarData[m] / 0x10000) + 1) {
 58             cMonth--;
 59         }
 60     }
 61 }
 62 
 63 function GetcDateString() {
 64     var tmp = "";
 65     tmp += tgString.charAt((cYear - 4) % 10);
 66     tmp += dzString.charAt((cYear - 4) % 12);
 67     tmp += "(";
 68     tmp += sx.charAt((cYear - 4) % 12);
 69     tmp += ")年 ";
 70     if (cMonth < 1) {
 71         tmp += "(闰)";
 72         tmp += monString.charAt(-cMonth - 1);
 73     } else {
 74         tmp += monString.charAt(cMonth - 1);
 75     }
 76     tmp += "月";
 77     tmp += (cDay < 11) ? "初" : ((cDay < 20) ? "十" : ((cDay < 30) ? "廿" : "三十"));
 78     if (cDay % 10 != 0 || cDay == 10) {
 79         tmp += numString.charAt((cDay - 1) % 10);
 80     }
 81     return tmp;
 82 }
 83 
 84 function GetLunarDay(solarYear, solarMonth, solarDay) {
 85     //solarYear = solarYear<1900?(1900+solarYear):solarYear;
 86     if (solarYear < 1921 || solarYear > 2020) {
 87         return "";
 88     } else {
 89         solarMonth = (parseInt(solarMonth) > 0) ? (solarMonth - 1) : 11;
 90         e2c(solarYear, solarMonth, solarDay);
 91         return GetcDateString();
 92     }
 93 }
 94 
 95 var D = new Date();
 96 var yy = D.getFullYear();
 97 var mm = D.getMonth() + 1;
 98 var dd = D.getDate();
 99 var ww = D.getDay();
100 var ss = parseInt(D.getTime() / 1000);
101 if (yy < 100) yy = "19" + yy;
102 function GetCNDate() {
103     return GetLunarDay(yy, mm, dd);
104 }

调用GetCNDate()方法即可获取今日的农历

原文地址:https://www.cnblogs.com/iack/p/3504217.html