본문 바로가기
웹 개발/Classic ASP

[ASP] 현재 일자 표시하는 달력 만들기 (전체 코드 포함) / ASP Calendar

by 알쓸전잡 2025. 8. 21.
반응형

Classic ASP에서 간단하게 달력을 만드는 예제입니다.

CSS는 간단하게 적용했으니 원하는 디자인으로 커스텀하시면 됩니다.

ASP 달력 결과물
ASP 달력 결과물

<html>
	<head>
		<title>ASP 달력 만들기</title>
		<style>
			table, td {
				border : 1px, solid black;
				border-collapse : collapse;
			}
		</style>
	</head>
	<body>

<%

'-------------------------------------------------------------
' 달력만드는 순서
' 1. 이번달 1일의 요일을 계산한다.
' 2. 이번달의 마지막 날짜를 계산한다. (이번달 날짜수 = 다음달 1일 - 이번달 1일)
' 3. 이번 달 1일의 요일을 계산해서 해당셀에 1부터 차례대로 날짜수 만큼 뿌린다.
'-------------------------------------------------------------

'이번달 1일의 요일계산
'-------------------------------------------------------------
	NowYear = Request("select_y")
	NowMonth = Request("select_m")
	
	If NowYear = "" Then
		NowYear  = year(now)    '금년도	
	End If
	If NowMonth = "" Then
		NowMonth = month(now)   '금월	
	End If
	
	NYear = year(now)
	NMonth = month(now)
	NDay = Day(now)
	
%>
	<center>
	<form name="Q_form" method="post" action="calendar_basic.asp">
		<select name="select_y">
			<option value="" >년도</option>
			<option value="2023" <% if NowYear = "2023" then %> selected<% end if %>>2023</option>
			<option value="2024" <% if NowYear = "2024" then %> selected<% end if %>>2024</option>
			<option value="2025" <% if NowYear = "2025" then %> selected<% end if %>>2025</option>
			<option value="2026" <% if NowYear = "2026" then %> selected<% end if %>>2026</option>
			<option value="2027" <% if NowYear = "2027" then %> selected<% end if %>>2027</option>
		</select>

		<select name="select_m">
			<option value="">월</option>
			<option value="1" <% if NowMonth = "1" then %> selected<% end if %>>1월</option>
			<option value="2" <% if NowMonth = "2" then %> selected<% end if %>>2월</option>
			<option value="3" <% if NowMonth = "3" then %> selected<% end if %>>3월</option>
			<option value="4" <% if NowMonth = "4" then %> selected<% end if %>>4월</option>
			<option value="5" <% if NowMonth = "5" then %> selected<% end if %>>5월</option>
			<option value="6" <% if NowMonth = "6" then %> selected<% end if %>>6월</option>
			<option value="7" <% if NowMonth = "7" then %> selected<% end if %>>7월</option>
			<option value="8" <% if NowMonth = "8" then %> selected<% end if %>>8월</option>
			<option value="9" <% if NowMonth = "9" then %> selected<% end if %>>9월</option>
			<option value="10" <% if NowMonth = "10" then %> selected<% end if %>>10월</option>
			<option value="11" <% if NowMonth = "11" then %> selected<% end if %>>11월</option>
			<option value="12" <% if NowMonth = "12" then %> selected<% end if %>>12월</option>
		</select>
		<input type="submit" name="b2" value="조회">
	</form>

<%

Yoil = weekday(NowYear & "-" & NowMonth & "-1")    '1:일요일 2:월요일 ....7:토요일

'이번달 마지막날 찾기
'----------------------------------------------------------------------
'이번달 1일
NowMonthFirst = NowYear & "-" & Right(("00" & Nowmonth),2) & "-01"     

'다음달 1일
NextMonthFirst = DateAdd("m",1,NowMonthFirst)

'(다음달 1일) - (이번달 1일 ) = 이번달 마지막날짜
LastNowMonth = DateDiff("y", NowMonthFirst, NextMonthFirst) 

'DB 날짜 조회용 변수
'-------------------------------------------------------------
	q1 = DateSerial(NowYear, NowMonth, 1)
	q2 = DateSerial(NowYear, NowMonth, LastNowMonth)
	
%>
	<table>
		<tr height="40" align="center">
			<td width="200"><font size="4" color="red"><b>일</b></font></td>
			<td width="200"><font size="4"><b>월</b></font></td>
			<td width="200"><font size="4"><b>화</b></font></td>
			<td width="200"><font size="4"><b>수</b></font></td>
			<td width="200"><font size="4"><b>목</b></font></td>
			<td width="200"><font size="4"><b>금</b></font></td>
			<td width="200"><font size="4" color="blue"><b>토</b></font></td>
		</tr>
		<tr height="130">
<%
'1일이 시작하기 전 빈칸 생성
For weekdaycount = 1 to Yoil-1
   Response.write "<td></td>"&chr(13)
Next

'1일부터 마지막날까지 달력생성
'------------------------------

For i = 1 to LastNowMonth
   If Yoil = 1 Then

%> 
	<% If Cdbl(NowYear) = Cdbl(NYear) and Cdbl(NowMonth) = Cdbl(NMonth) and Cdbl(i) = Cdbl(NDay) Then%>
		<td bgcolor="#90EE90">
	<% Else %>
		<td>
	<% End If %>
			<font color='red'>
				<b>
					&nbsp;<% = i %>
				</b>
			</font>
			<br><br><br><br>
		</td>
<% ElseIf Yoil = 7 Then %> 
	<% If Cdbl(NowYear) = Cdbl(NYear) and Cdbl(NowMonth) = Cdbl(NMonth) and Cdbl(i) = Cdbl(NDay) Then%>
		<td bgcolor="#90EE90">
	<% Else %>
		<td>
	<% End If %>
			<font color='blue'>
				<b>
					&nbsp;<% = i %>
				</b>
			</font>
			<br><br><br><br>
		</td>
<% Else %> 
	<% If Cdbl(NowYear) = Cdbl(NYear) and Cdbl(NowMonth) = Cdbl(NMonth) and Cdbl(i) = Cdbl(NDay) Then%>
		<td bgcolor="#90EE90">
	<% Else %>
		<td>
	<% End If %>
			<b>
				&nbsp;<% = i %>
			</b>
			<br><br><br><br>
		</td>
<%

   end if

    '이번달 첫번째 요일 숫자를 증가시켜서 7이 넘으면 줄바꾸기를 하고 요일 숫자를 1로 바꾼다.
    Yoil = Yoil + 1
    'Response.write Yoil
    if Yoil > 7 then
       response.write " </tr>" & chr(13)
       Response.write " <tr  height='130'>"
       Yoil = 1
    end if  
Next


'마지막날 이후 달력 끝날때까지 빈칸 생성(토요일로 끝나면 출력안됨)
'------------------------------------------
If Yoil > 1 Then
   for i = Yoil to 7
      response.write "  <td>  </td>" 
   next
End If

'테이블 닫기
response.write " </tr>" & chr(13) & "</table>"

%>

   </body>
</html>

 

calendar_basic.asp
0.01MB

반응형