実装コードを使用した JS カレンダーの実践_時刻と日付

WBOY
リリース: 2016-05-16 18:56:24
オリジナル
880 人が閲覧しました

プログラムは次のようになります。まず、ユーザーが入力した年と月を収集し、計算によって月の最大日数を取得し、月の最初の日が何曜日であるかを取得します。次に、その月の具体的な情報を表に記入します。


[Ctrl A すべて選択 注: 外部 Js を導入する必要がある場合は、
を実行するために更新する必要があります]<script> function getMonthJuZhen(date){ if(arguments.length == 0){ throw new Error("need a date"); } if(arguments[0] == null){ throw new Error("date is null or undefined"); } if(arguments[0] instanceof Date){ var monthjuzhen = new Array(5); for(var r = 0 ; r < 5 ; r++){ monthjuzhen[r] = [0,0,0,0,0,0,0]; } var maxDay = getMaxDay(arguments[0]); arguments[0].setDate(1); var r = 0; var c = arguments[0].getDay(); for(var d = 1 ; d <= maxDay ; d++){ monthjuzhen[r][c] = d; if(c == 6){ if(r == 4){ r = 0; } else{ r++; } c = 0; } else{ c++; } } return monthjuzhen; } else{ throw new Error("need a date"); } } function getMaxDay(date){ if(arguments.length == 0){ throw new Error("need a date"); } if(arguments[0] == null){ throw new Error("date is null or undefined"); } if(arguments[0] instanceof Date){ var tempDate = new Date(arguments[0].toString()); if(arguments[0].getMonth() == 11){ tempDate.setFullYear((tempDate.getFullYear()+1)); tempDate.setMonth(0); } else{ tempDate.setMonth((tempDate.getMonth()+1)); } return Math.floor((tempDate.getTime()-arguments[0].getTime())/(1000*60*60*24)); } else{ throw new Error("need a date"); } } function updateDate(){ var yearInput = document.getElementById("yearinput"); var monthInput = document.getElementById("monthinput"); try{ var year = parseInt(yearInput.value); var month = parseInt(monthInput.value); if(isNaN(year)){ if(isNaN(month)){ monthInput.value = ""; } yearInput.value = ""; return; } if(isNaN(month)){ monthInput.value = ""; return; } if(month > 12 || month < 1){ monthInput.value = ""; return; } month--; var date = new Date(year,month); var monthjuzhen = getMonthJuZhen(date); var day; var dayvalue; for(var r in monthjuzhen){ for(var c in monthjuzhen[parseInt(r)]){ day = document.getElementById(r+"-"+c); dayvalue = monthjuzhen[parseInt(r)][parseInt(c)]; if(dayvalue == 0){ if(day.hasChildNodes()){ day.removeChild(day.firstChild); } day.onmouseover = null; day.onmouseout = null; day.onclick = null; continue; } if(day.hasChildNodes()){ day.firstChild.nodeValue = dayvalue; } else{ day.appendChild(document.createTextNode(dayvalue)); } day.setAttribute("date",year+"-"+month+"-"+dayvalue); day.onmouseover = function(){this.style.backgroundColor = "black";this.style.color = "white";}; day.onmouseout = function(){this.style.backgroundColor = "white";this.style.color = "black"}; day.onclick = subDate; } } } catch(e){ alert(e); } } function subDate(){ var date = this.getAttribute("date").match(/(\d+)-(\d+)-(\d+)/); alert(new Date(date[1],date[2],date[3])); } </script>
関連ラベル:
js
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!