JavaScript練習(二)
阿新 • • 發佈:2018-12-25
JavaScript練習二:
需求:計算查詢時段。有訂單查詢頁面,頁面上顯示兩種查詢時段,三天內、七天內。
使用者選擇某種查詢時段,則需要提示查詢的開始日期和結束日期。比如,使用者單擊“三天內”,則彈出從當前時間到三天後的時間資訊。
=========================================答案==========================================
html程式碼:
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> </head> <body> <span style="font:bold"> 2.Date 物件:訂單時段查詢 </span> <br /> <input type="radio" name="r1" onclick="threeday()" />三天內 <input type="radio" name="r1" onclick="week()" />一週內 </body> <script> function threeday(){ alert("開始日期為:"+GetDateStr(0)+"\n"+"結束日期為:"+GetDateStr(3)); } function week(){ alert("開始日期為:"+GetDateStr(0)+"\n"+"結束日期為:"+GetDateStr(7)); } function GetDateStr(AddDay){ var date = new Date(); date.setDate(date.getDate()+AddDay); //獲取幾天後的資訊 var nowmonth = date.getMonth()+1; var nowweek = date.getDay(); var nowyear = date.getFullYear(); var nowday = date.getDate(); switch(nowweek){ case 1:nowweek='Monday'; break; case 2:nowweek='Tuesday'; break; case 3:nowweek='Wednesday'; break; case 4:nowweek='Thursday'; break; case 5:nowweek='Friday'; break; case 6:nowweek='Saturday'; break; case 0:nowweek='Sunday'; break; } return nowweek+","+nowmonth+" "+nowday+","+nowyear; } </script> </html>
效果圖如下: