首页 > 代码库 > Javascript日历
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;
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。