欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js简单日历

程序员文章站 2022-03-03 21:55:13
...
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Js日历</title>
  6. <style type="text/css">
  7. .evrday{
  8. width:14%;
  9. float:left;
  10. color:li
  11. }
  12. #calendar{
  13. background: rgb(231, 228, 228);
  14. width:400px;
  15. height: 300px;
  16. margin:auto;
  17. }
  18. button{
  19. width:25%;
  20. height:40px;
  21. float:left;
  22. }
  23. div{
  24. margin-top:10px;
  25. margin-bottom:5px;
  26. text-align:center;
  27. }
  28. #month{
  29. width:50%;
  30. float:left;
  31. }
  32. </style>
  33. </head>
  34. <body onLoad="showday()">
  35. <div id="calendar">
  36. <div>
  37. <button onClick="shang()">上个月</button>
  38. <div id="month"></div>
  39. <button onClick="next()" id="nav">下个月</button>
  40. </div>
  41. <div>
  42. <div class="evrday">周日</div>
  43. <div class="evrday">周一</div>
  44. <div class="evrday">周二</div>
  45. <div class="evrday">周三</div>
  46. <div class="evrday">周四</div>
  47. <div class="evrday">周五</div>
  48. <div class="evrday">周六</div>
  49. </div>
  50. <div id="day" style="color:black"></div>
  51. </div>
  52. <script>
  53. var today=new Date();//获取当前时间
  54. var year=today.getFullYear();//获取当前的年份
  55. var month=today.getMonth()+1;//获取当前月
  56. var day=today.getDate();//获取当前月天
  57. var allday=0;
  58. function showmonth(){
  59. var year_month=year+"年"+month+"月";
  60. document.getElementById("month").innerHTML=year_month;//显示当前的年月日
  61. }
  62. function count(){
  63. if(month!=2)
  64. {
  65. if(month==4||month==6||month==9||month==11)//判断是否是相同天数的几个月,二月除外
  66. allday=30;
  67. else
  68. allday=31;
  69. }
  70. else
  71. {
  72. if((year%4==0&&year%100!=0)||(year%400==0))//判断是否是闰年,进行相应的改变
  73. allday=29;
  74. else
  75. allday=28;}
  76. }
  77. //显示相应月份的天数
  78. function showday(){
  79. showmonth()
  80. count();
  81. var fistdate=new Date(year,month-1,1);
  82. var xinqi=fistdate.getDay();
  83. var daterow=document.getElementById("day");
  84. for(var i=0;i<xinqi;i++)//循环输出天数
  85. {
  86. var dayElement=document.createElement("div");
  87. dayElement.className="evrday";
  88. daterow.appendChild(dayElement);
  89. }
  90. for(var j=1;j<=allday;j++)
  91. {
  92. var dayElement=document.createElement("div");
  93. dayElement.innerHTML=j;
  94. dayElement.className="evrday";
  95. if(j==day)//当前日,标红
  96. dayElement.style.color="red";
  97. daterow.appendChild(dayElement);
  98. }
  99. }
  100. //点击下个月
  101. function next(){
  102. document.getElementById('day').innerHTML ="";
  103. if(month<12){
  104. month=month+1;
  105. }else{
  106. month=1;
  107. year=year+1;
  108. }
  109. document.getElementById('div').innerHTML =showday();
  110. }
  111. //点击上个月
  112. function shang(){
  113. document.getElementById('day').innerHTML ="";
  114. if(month>1){
  115. month=month-1;
  116. }else{
  117. month=12;
  118. year=year-1;
  119. }
  120. document.getElementById('div').innerHTML =showday();
  121. }
  122. </script>
  123. </body>
  124. </html>

js简单日历