rinald_未来往事

带动态时间/公历/农历日期的js效果代码

WEB前端
显示动态时间 公历 农历日期的js效果代码

效果图

HTML & Javascript代码
  1. <div class="time" id="date"></div>
  1. <script type="text/javascript">
  2. //日历
  3. var CalendarData=new Array(20);
  4. var madd=new Array(12);
  5. var TheDate=new Date();
  6. var tgString="甲乙丙丁戊己庚辛壬癸";
  7. var dzString="子丑寅卯辰巳午未申酉戌亥";
  8. var numString="一二三四五六七八九十";
  9. var monString="正二三四五六七八九十冬腊";
  10. var weekString="日一二三四五六";
  11. var sx="鼠牛虎兔龙蛇马羊猴鸡狗猪";
  12. var cYear;
  13. var cMonth;
  14. var cDay;
  15. var cHour;
  16. var cDateString;
  17. var DateString;
  18. var Browser=navigator.appName;
  19. function init(){
  20. CalendarData[0]=0x41A95;
  21. CalendarData[1]=0xD4A;
  22. CalendarData[2]=0xDA5;
  23. CalendarData[3]=0x20B55;
  24. CalendarData[4]=0x56A;
  25. CalendarData[5]=0x7155B;
  26. CalendarData[6]=0x25D;
  27. CalendarData[7]=0x92D;
  28. CalendarData[8]=0x5192B;
  29. CalendarData[9]=0xA95;
  30. CalendarData[10]=0xB4A;
  31. CalendarData[11]=0x416AA;
  32. CalendarData[12]=0xAD5;
  33. CalendarData[13]=0x90AB5;
  34. CalendarData[14]=0x4BA;
  35. CalendarData[15]=0xA5B;
  36. CalendarData[16]=0x60A57;
  37. CalendarData[17]=0x52B;
  38. CalendarData[18]=0xA93;
  39. CalendarData[19]=0x40E95;
  40. madd[0]=0;
  41. madd[1]=31;
  42. madd[2]=59;
  43. madd[3]=90;
  44. madd[4]=120;
  45. madd[5]=151;
  46. madd[6]=181;
  47. madd[7]=212;
  48. madd[8]=243;
  49. madd[9]=273;
  50. madd[10]=304;
  51. madd[11]=334;
  52. }
  53. function GetBit(m,n){
  54. return (m>>n)&1;
  55. }
  56. function e2c(){
  57. var totalmnk;
  58. var isEnd=false;
  59. var tmp=TheDate.getYear();
  60. if (tmp<1900) tmp+=1900;
  61. total=(tmp-2001)*365
  62. +Math.floor((tmp-2001)/4)
  63. +madd[TheDate.getMonth()]
  64. +TheDate.getDate()
  65. -23;
  66. if (TheDate.getYear()%4==0&&TheDate.getMonth()>1)
  67. total++;
  68. for(m=0;;m++)
  69. {
  70. k=(CalendarData[m]<0xfff)?11:12;
  71. for(n=k;n>=0;n--)
  72. {
  73. if(total<=29+GetBit(CalendarData[m],n))
  74. {
  75. isEnd=true;
  76. break;
  77. }
  78. total=total-29-GetBit(CalendarData[m],n);
  79. }
  80. if(isEnd)break;
  81. }
  82. cYear=2001 + m;
  83. cMonth=k-n+1;
  84. cDay=total;
  85. if(k==12)
  86. {
  87. if(cMonth==Math.floor(CalendarData[m]/0x10000)+1)
  88. cMonth=1-cMonth;
  89. if(cMonth>Math.floor(CalendarData[m]/0x10000)+1)
  90. cMonth--;
  91. }
  92. cHour=Math.floor((TheDate.getHours()+3)/2);
  93. }
  94. function GetcDateString(){
  95. var tmp="";
  96. tmp+=tgString.charAt((cYear-4)%10); //年干
  97. tmp+=dzString.charAt((cYear-4)%12); //年支
  98. tmp+="年(";
  99. tmp+=sx.charAt((cYear-4)%12);
  100. tmp+=") ";
  101. if(cMonth<1)
  102. {
  103. tmp+="闰";
  104. tmp+=monString.charAt(-cMonth-1);
  105. }
  106. else
  107. tmp+=monString.charAt(cMonth-1);
  108. tmp+="月";
  109. tmp+=(cDay<11)?"初":((cDay<20)?"十":((cDay<30)?"廿":"卅"));
  110. if(cDay%10!=0||cDay==10)
  111. tmp+=numString.charAt((cDay-1)%10);
  112. tmp+=" ";
  113. if(cHour==13)tmp+="夜";
  114. tmp+=dzString.charAt((cHour-1)%12);
  115. tmp+="时";
  116. cDateString=tmp;
  117. return tmp;
  118. }
  119. function GetDateString(){
  120. var tmp="";
  121. var t1=TheDate.getYear();
  122. if (t1<1900)t1+=1900;
  123. tmp+=t1
  124. +"/"
  125. +(TheDate.getMonth()+1)+"/"
  126. +TheDate.getDate()+' '
  127. +'<span id="time"></span>'
  128. +" 星期"+weekString.charAt(TheDate.getDay());
  129. DateString=tmp;
  130. return tmp;
  131. }
  132. init();
  133. e2c();
  134. GetDateString();
  135. GetcDateString();
  136. var today='今天是:'+DateString+" 农历 "+cDateString;
  137. document.getElementById("date").innerHTML = today;
  138. <!--十,分,秒-->
  139. function showLocale(obj){
  140. var str;
  141. var hh = obj.getHours();
  142. if(hh<10)
  143. hh = '0' + hh;
  144. var mm = obj.getMinutes();
  145. if(mm<10) mm = '0' + mm;
  146. var ss = obj.getSeconds();
  147. if(ss<10) ss = '0' + ss;
  148. str = hh + ":" + mm + ":" + ss;
  149. return(str);
  150. }
  151. function tick(){
  152. var today;
  153. today = new Date();
  154. document.getElementById("time").innerHTML = showLocale(today);
  155. window.setTimeout("tick()", 1000);
  156. }
  157. tick();
  158. </script>
原文作者:rinald
原文地址:http://fity.cn/post/538.html
互联网技术更新较快,本站很多文章具有实效性,我会及时更新原文,但转载的文章无法通知更新。为了不给读者造成困惑或误导,请您在转载时保留此出处信息,尊重别人也是尊重自己。

已有1位网友发表了看法:

1L太古神王 2015-08-25 13:54:31 回复
[给力]

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。