angular.module('push') .factory('CalenderHomeService', function () { var _monthday = [];//存放日历 var _months = [];//年份下各月天数 var monthnum;//当月天数 var lastMonth; //上一月天数 var nextMounth;//下一月天数 return { //初始化某年每月多少天 initMonths: function (_year) { for (var i = 1; i <= 12; i++) { var date = new Date(_year, i, 1); _months.push((new Date(date.getTime() - 1000 * 60 * 60 * 24)).getDate()); } }, //初始化当月,上月,下月天数 initLastAndNextMonth: function (_year, _month) { monthnum = new Date(parseInt(_year), parseInt(_month), 0).getDate(); if (parseInt(_month) == 1) { lastMonth = new Date(parseInt(_year) - 1, parseInt(12), 0).getDate(); } else { lastMonth = new Date(parseInt(_year), parseInt(_month) - 1, 0).getDate(); } if (parseInt(_month) == 12) { nextMounth = new Date(parseInt(_year) + 1, parseInt(1), 0).getDate(); } else { nextMounth = new Date(parseInt(_year), parseInt(_month) + 1, 0).getDate(); } }, //初始化日历 initCalendar: function (_year, _month, _day) { this.initData(); this.initMonths(_year); this.initLastAndNextMonth(_year, _month); var cal = { year: _year, month: _month, day: 0 }; //本月第一天是周几 var firstweek = this.getMonthFirstDayWeek(_year, _month, 1); firstweek = firstweek == 0 ? 7 : firstweek; //上月空余日期填补 for (j = firstweek - 1; j > 0; j--) { var temp1 = angular.copy(cal); temp1.month = _month - 1 == 0 ? 12 : _month - 1; temp1.year = _month - 1 == 0 ? _year - 1 : _year; temp1.day = lastMonth - j + 1; var temps = temp1.year + "-"; if (temp1.month < 10) { temps = temps + "0" + temp1.month + "-"; } else { temps = temps + temp1.month + "-"; } if (temp1.day < 10) { temps = temps + "0" + temp1.day; } else { temps = temps + temp1.day; } temp1.date = temps; _monthday.push(temp1); } //本月日期填补 for (i = 1; i <= _months[_month - 1]; i++) { var weeknum = this.getMonthWeek(_year, _month, i); var temp2 = angular.copy(cal); temp2.day = i; var tempt = temp2.year + "-"; if (temp2.month < 10) { tempt = tempt + "0" + temp2.month + "-"; } else { tempt = tempt + temp2.month + "-"; } if (temp2.day < 10) { tempt = tempt + "0" + temp2.day; } else { tempt = tempt + temp2.day; } temp2.date = tempt; _monthday.push(temp2); } //本月最后一天是周几 var lastweek = this.getMonthFirstDayWeek(_year, _month, monthnum); //下月空余日期填补 if (lastweek != 0) { for (j = 1; j <= 7 - lastweek; j++) { var temp3 = angular.copy(cal); temp3.month = _month + 1 == 13 ? 1 : _month + 1; temp3.year = _month + 1 == 13 ? _year + 1 : _year; temp3.day = j; var tempn = temp3.year + "-"; if (temp3.month < 10) { tempn = tempn + "0" + temp3.month + "-"; } else { tempn = tempn + temp3.month + "-"; } if (temp3.day < 10) { tempn = tempn + "0" + temp3.day; } else { tempn = tempn + temp3.day; } temp3.date = tempn; _monthday.push(temp3); } } return _monthday; }, //判断某一天是周几 getMonthFirstDayWeek: function (year, month, day) { var date = new Date(year, month - 1, day); return date.getDay(); }, //判断某一天是当月的第几周 getMonthWeek: function (year, month, day) { var date = new Date(year, parseInt(month) - 1, day); var w = date.getDay(); w = w == 0 ? 7 : w; var d = date.getDate(); return Math.ceil( (d + 6 - w) / 7 ); }, //重置所有数据 initData: function () { _monthday = []; _months = []; monthnum = 0; lastMonth = 0; nextMounth = 0; } } });