본문 바로가기

'.' Programs/JavaScript

자바스크립트 금주 , 금월 구하기

<HTML> <HEAD> <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <SCRIPT LANGUAGE="JavaScript"> var now = new Date(); var nowDayOfWeek = now.getDay(); var nowDay = now.getDate(); var nowMonth = now.getMonth(); var nowYear = now.getYear(); nowYear += (nowYear < 2000) ? 1900 : 0; var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)); function formatDate(date) { var mymonth = date.getMonth()+1; var myweekday = date.getDate(); return (mymonth + "-" + myweekday); } function printWeek() { var sDate = formatDate(weekStartDate); var eDate = formatDate(weekEndDate); sDate = sDate.split("-"); if(sDate[1].length==1){sDate[1] = "0"+sDate[1];} eDate = eDate.split("-"); if(eDate[1].length==1){eDate[1] = "0"+eDate[1];} sDate = sDate[0]+"-"+sDate[1]; eDate = eDate[0]+"-"+eDate[1]; $("#sDate").val(nowYear+"-"+sDate); $("#eDate").val(nowYear+"-"+eDate); } function printMonth(){ var sDate = new Date(nowYear, nowMonth); var eDate = new Date(now.getYear(), now.getMonth()+1,0); sDate = formatDate(sDate); eDate = formatDate(eDate);


sDate = sDate.split("-"); if(sDate[1].length==1){sDate[1] = "0"+sDate[1];} eDate = eDate.split("-"); if(eDate[1].length==1){eDate[1] = "0"+eDate[1];} sDate = sDate[0]+"-"+sDate[1]; eDate = eDate[0]+"-"+eDate[1]; $("#sDate").val(nowYear+"-"+sDate); $("#eDate").val(nowYear+"-"+eDate); } </script> <!------- 여기까지 ----->

<style> body,td {font-size:12px} </style>

</HEAD>
<BODY>

<input type="text" id="sDate"> - <input type="text" id="eDate"> <input type="button" onclick="printWeek();" value="금주"> <input type="button" onclick="printMonth();" value="금월"> </BODY>

</HTML>