js-Date()对象,get/setFullYear(),getDay()编程练习
啥也不说!看代码
主要注意:getday()方法中原理!!!
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Date对象的使用</title> 6 <style> 7 #get_fullyear{ 8 height: 200px; 9 background-color: pink; 10 } 11 </style> 12 <script> 13 // 返回年份 getFullYear() 将获取到的值写入div 14 function getfullyear(){ 15 var mydate = new Date(); 16 var myfullyear = mydate.getFullYear() 17 document.getElementById("get_fullyear").innerHTML=myfullyear; 18 } 19 //设置年份 setFullYear() 将修改后的值写入div 20 function setfullyear() { 21 // 获取文本框(#txt)中的值 22 var mytxtfullyear = parseInt(document.getElementById("txt").value); 23 // 获取对象Date 24 var mydate = new Date(); 25 // 将获取到的值写入 26 mydate.setFullYear(mytxtfullyear); 27 var mysetfullyear = mydate.getFullYear(); 28 document.getElementById("get_fullyear").innerHTML = mysetfullyear; 29 } 30 function getday(){ 31 var mydate = new Date(); 32 var myarrday = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; 33 var mygetday = myarrday[mydate.getDay()]; 34 document.getElementById("get_fullyear").innerHTML=mygetday; 35 } 36 </script> 37 </head> 38 <body> 39 <div id="get_fullyear"> 40 </div> 41 <input type="button" value="点击获取年份!" onclick="getfullyear()"><br /> 42 <input type="text" id="txt"> 43 <!-- 将text的value默认值设置为当前年份 --> 44 <script> 45 var mydate = new Date(); 46 var myfullyear = mydate.getFullYear(); 47 document.getElementById("txt").value = myfullyear; 48 </script> 49 <input type="button" value="点击设置年份" onclick="setfullyear()"><br /> 50 <input type="button" value="点击获取今天的周几?" onclick="getday()"> 51 </body> 52 </html>