<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <table id="table">
    	<thead></thead>
        <tbody></tbody>
    </table>
</body>
</html>
<script>
    function drawDateTable() {
	//예시 데이터
        stDate = new Date('2022-09-19');
        endDate = new Date('2022-10-02');
        var diffTime = endDate.getTime() - stDate.getTime();
        var diffDay = diffTime / (1000*60*60*24);

        var conDate = [];
        for(var i=0; i <= diffDay; i++) {
            var nextDate = new Date(
                stDate.getFullYear(),
                stDate.getMonth(),
                stDate.getDate() + i
            );
            var year = nextDate.getFullYear();
            var month = nextDate.getMonth() + 1;
            var day = nextDate.getDate();
            var dateFormat = year + '/' + month + '/' + day;
            dateFormat = dateFormat.substr(2);
            conDate.push(dateFormat);
        }

         //thead
        var html = '';
        html += '<tr>';
        $(conDate).each(function(i, date) {
            html += '<th style="width:2.5%">' + date + '</th>';
        });
        html += '</tr>';
        
        //테이블id
        $("#table thead").empty().append(html);
        
        //tbody
	html = '';
        html += '<tr>';
        $(conDate).each(function(i, date) {
            html += '<td></td>';
        });
        html += '</tr>';

        $("#table tbody").empty().append(html);
    }
</script>

 

  • 예시

- 기간을 함수 파라미터값으로 넣어주면 기간만큼의 날짜가 나열됨

+ Recent posts