Last active
September 24, 2015 06:14
-
-
Save urstory/1547b37ad09f7a6dfaaf to your computer and use it in GitHub Desktop.
cal.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> | |
<script> | |
$(document).ready(function () { | |
var date = new Date(); | |
var currentYear = date.getFullYear(); | |
var currentMonth = date.getMonth() + 1; | |
date.setDate(1); | |
// 0:일요일, 6:토요일 | |
var currentDay = date.getDay(); | |
var lastDate = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); | |
if( (currentYear % 4 === 0 && currentYear % 100 !== 0) || currentYear % 400 === 0 ){ | |
lastDate[1] = 29; | |
} | |
// 마지막 일자 구하기 | |
var currentLastDate = lastDate[currentMonth-1]; | |
// 총 몇주인지 구하기 | |
var week = Math.ceil( ( currentDay + currentLastDate ) / 7 ); | |
var calHtml = $('<table></table>'); | |
calHtml.html( | |
'<tr><th>일</th><th>월</th><th>화</th><th>수</th><th>목</th><th>금</th><th>토</th></tr>'); | |
var num = 1; | |
for(var i = 0; i < week; i++){ | |
var trStr = "<tr>"; | |
// 첫주에 해당하는 tr태그 만들기. | |
if(i == 0){ | |
for(var k = 0; k < 7; k++){ | |
trStr = trStr + "<td>"; | |
// 1일이 시작하는 요일부터 일자를 출력. | |
if(k >= currentDay){ | |
trStr = trStr + num; | |
num++; | |
} | |
trStr = trStr + "</td>"; | |
} | |
}else{ // 두번째 부터 tr 태그 만들기 | |
for(var k = 0; k < 7; k++){ | |
trStr = trStr + "<td>"; | |
// 해당 달의 마지막 날 전까지만 일자를 출력. | |
if(num <= currentLastDate){ | |
trStr = trStr + num; | |
num++; | |
} | |
trStr = trStr + "</td>"; | |
} | |
} | |
var trHtml = $(trStr); | |
calHtml.append(trHtml); | |
} | |
$('tr:first', calHtml).css('background', '#000000').css('color','#ffffff'); | |
$('td:nth-child(7n+1)', calHtml).css('color','red'); | |
$('td:nth-child(7n)', calHtml).css('color','blue'); | |
$('body').html(calHtml); | |
}); | |
</script> | |
</head> | |
<body> | |
<div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment