Javascript日历
/** * Created by YCXJ-wanglihui on 2014/7/25. * @module utils */ ‘use strict‘; /** * @class 日历类 Calendar * @constructor * @param {Number} year * @param {Number} month */ function Calendar(year, month) { this.year = year; this.month = month; } /** * @method 打印月份日历 * @param {Number} year * @param {Number} month * @returns {string} */ Calendar.prototype.printMonth = function(year, month) { var year = year || this.year; var month = month || this.month; var d = new Date(year, month-1, 1); var prevMonth = month-1; var prevMonthYear = year; if (month == 1) { prevMonth = 12; prevMonthYear = year - 1; }; var days = 30; var preMonthDays = 30; days = getMonthDays(year, month); preMonthDays = getMonthDays(prevMonthYear, prevMonth); var html = "<table style=‘width:300px;‘><tr><th colspan=‘7‘>"+month+"月份</th></tr>"; html += ‘<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>‘; var emptyNumber = d.getDay(); var i=1; var y = 1; var x = 1; while(true) { if (i % 7 === 1) { html += "<tr>"; } if (i<=emptyNumber) { html+="<td>"+(preMonthDays-(emptyNumber-i))+"</td>"; } else if (y>days){ html += "<td>"+(x++)+"</td>" } else{ html+="<td style=‘color:red‘>"+y+"</td>"; y++; } if (i % 7 === 0) { html += ‘</tr>‘; } if (y>days && i % 7 == 0) { break; } i++; } html += "</table>"; return html; } function getMonthDays(year, month) { var days = 30; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { days = 31; } else if (month == 2) { if (year % 4 ==0 && year % 100 !=0 ){ days = 29 } else { days = 28; } } return days; } module.exports = Calendar;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。