calenderHomeService.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. angular.module('push')
  2. .factory('CalenderHomeService', function () {
  3. var _monthday = [];//存放日历
  4. var _months = [];//年份下各月天数
  5. var monthnum;//当月天数
  6. var lastMonth; //上一月天数
  7. var nextMounth;//下一月天数
  8. return {
  9. //初始化某年每月多少天
  10. initMonths: function (_year) {
  11. for (var i = 1; i <= 12; i++) {
  12. var date = new Date(_year, i, 1);
  13. _months.push((new Date(date.getTime() - 1000 * 60 * 60 * 24)).getDate());
  14. }
  15. },
  16. //初始化当月,上月,下月天数
  17. initLastAndNextMonth: function (_year, _month) {
  18. monthnum = new Date(parseInt(_year), parseInt(_month), 0).getDate();
  19. if (parseInt(_month) == 1) {
  20. lastMonth = new Date(parseInt(_year) - 1, parseInt(12), 0).getDate();
  21. } else {
  22. lastMonth = new Date(parseInt(_year), parseInt(_month) - 1, 0).getDate();
  23. }
  24. if (parseInt(_month) == 12) {
  25. nextMounth = new Date(parseInt(_year) + 1, parseInt(1), 0).getDate();
  26. } else {
  27. nextMounth = new Date(parseInt(_year), parseInt(_month) + 1, 0).getDate();
  28. }
  29. },
  30. //初始化日历
  31. initCalendar: function (_year, _month, _day) {
  32. this.initData();
  33. this.initMonths(_year);
  34. this.initLastAndNextMonth(_year, _month);
  35. var cal = {
  36. year: _year,
  37. month: _month,
  38. day: 0
  39. };
  40. //本月第一天是周几
  41. var firstweek = this.getMonthFirstDayWeek(_year, _month, 1);
  42. firstweek = firstweek == 0 ? 7 : firstweek;
  43. //上月空余日期填补
  44. for (j = firstweek - 1; j > 0; j--) {
  45. var temp1 = angular.copy(cal);
  46. temp1.month = _month - 1 == 0 ? 12 : _month - 1;
  47. temp1.year = _month - 1 == 0 ? _year - 1 : _year;
  48. temp1.day = lastMonth - j + 1;
  49. var temps = temp1.year + "-";
  50. if (temp1.month < 10) {
  51. temps = temps + "0" + temp1.month + "-";
  52. } else {
  53. temps = temps + temp1.month + "-";
  54. }
  55. if (temp1.day < 10) {
  56. temps = temps + "0" + temp1.day;
  57. } else {
  58. temps = temps + temp1.day;
  59. }
  60. temp1.date = temps;
  61. _monthday.push(temp1);
  62. }
  63. //本月日期填补
  64. for (i = 1; i <= _months[_month - 1]; i++) {
  65. var weeknum = this.getMonthWeek(_year, _month, i);
  66. var temp2 = angular.copy(cal);
  67. temp2.day = i;
  68. var tempt = temp2.year + "-";
  69. if (temp2.month < 10) {
  70. tempt = tempt + "0" + temp2.month + "-";
  71. } else {
  72. tempt = tempt + temp2.month + "-";
  73. }
  74. if (temp2.day < 10) {
  75. tempt = tempt + "0" + temp2.day;
  76. } else {
  77. tempt = tempt + temp2.day;
  78. }
  79. temp2.date = tempt;
  80. _monthday.push(temp2);
  81. }
  82. //本月最后一天是周几
  83. var lastweek = this.getMonthFirstDayWeek(_year, _month, monthnum);
  84. //下月空余日期填补
  85. if (lastweek != 0) {
  86. for (j = 1; j <= 7 - lastweek; j++) {
  87. var temp3 = angular.copy(cal);
  88. temp3.month = _month + 1 == 13 ? 1 : _month + 1;
  89. temp3.year = _month + 1 == 13 ? _year + 1 : _year;
  90. temp3.day = j;
  91. var tempn = temp3.year + "-";
  92. if (temp3.month < 10) {
  93. tempn = tempn + "0" + temp3.month + "-";
  94. } else {
  95. tempn = tempn + temp3.month + "-";
  96. }
  97. if (temp3.day < 10) {
  98. tempn = tempn + "0" + temp3.day;
  99. } else {
  100. tempn = tempn + temp3.day;
  101. }
  102. temp3.date = tempn;
  103. _monthday.push(temp3);
  104. }
  105. }
  106. return _monthday;
  107. },
  108. //判断某一天是周几
  109. getMonthFirstDayWeek: function (year, month, day) {
  110. var date = new Date(year, month - 1, day);
  111. return date.getDay();
  112. },
  113. //判断某一天是当月的第几周
  114. getMonthWeek: function (year, month, day) {
  115. var date = new Date(year, parseInt(month) - 1, day);
  116. var w = date.getDay();
  117. w = w == 0 ? 7 : w;
  118. var d = date.getDate();
  119. return Math.ceil(
  120. (d + 6 - w) / 7
  121. );
  122. },
  123. //重置所有数据
  124. initData: function () {
  125. _monthday = [];
  126. _months = [];
  127. monthnum = 0;
  128. lastMonth = 0;
  129. nextMounth = 0;
  130. }
  131. }
  132. });