/**
* 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;
Javascript日历,布布扣,bubuko.com
Javascript日历