tools.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var tools = (function () {
  2. var toolsObj = {
  3. // 渲染弹出框
  4. renderPopup: function (festival, obj, str) {
  5. var title = obj.querySelector('.title')
  6. var date = obj.querySelector('.date')
  7. var lunar = obj.querySelector('.lunar')
  8. var dateFormat = tools.strFormatDate(str)
  9. var lunarObj = ChineseCalendar.date2lunar(dateFormat);
  10. console.log(dateFormat)
  11. title.innerHTML = festival.innerHTML
  12. date.innerHTML = dateFormat.getFullYear() + '年' + (dateFormat.getMonth() + 1) + '月' + dateFormat.getDate() + '日'
  13. lunar.innerHTML = lunarObj.lunarMonthChiness + lunarObj.lunarDayChiness + ' · ' + lunarObj.gzY + '年' + lunarObj.gzM + '月' + lunarObj.gzD + '日'
  14. },
  15. // 侧边栏及全年月份数据返回
  16. renderDay: function (year, n) {
  17. var year = year
  18. var month = n
  19. var firstDay = new Date(year, n, 1) // 月份列表第一天
  20. var _hmtl = ``
  21. for (var i = 0; i < 42; i++) {
  22. var allDay = new Date(year, month, i + 1 - firstDay.getDay());
  23. var allDay_str = tools.returnDateStr(allDay)
  24. var firstDay_str = tools.returnDateStr(firstDay)
  25. var first_lunarday = ChineseCalendar.date2lunar(allDay).lunarDayChiness
  26. var lunarJanuary_month = ChineseCalendar.date2lunar(allDay).lunarMonthChiness // 正月
  27. if (tools.returnDateStr(new Date()) === allDay_str) { // 当天
  28. if (first_lunarday == '初一') { // 区分初一那天
  29. if (lunarJanuary_month == '正月') { // 区分春节那天
  30. _hmtl += `<li data-time="${allDay_str}" class="cur-day lunar-january">${allDay.getDate()}</li>`
  31. } else {
  32. _hmtl += `<li data-time="${allDay_str}" class="cur-day lunar-first">${allDay.getDate()}</li>`
  33. }
  34. } else {
  35. _hmtl += `<li data-time="${allDay_str}" class="cur-day">${allDay.getDate()}</li>`
  36. }
  37. } else if (firstDay_str.substr(0, 6) === allDay_str.substr(0, 6)) { // 当前月份
  38. if (first_lunarday == '初一') {
  39. if (lunarJanuary_month == '正月') {
  40. _hmtl += `<li data-time="${allDay_str}" class="cur-month lunar-january">${allDay.getDate()}</li>`
  41. } else {
  42. _hmtl += `<li data-time="${allDay_str}" class="cur-month lunar-first">${allDay.getDate()}</li>`
  43. }
  44. } else {
  45. _hmtl += `<li data-time="${allDay_str}" class="cur-month">${allDay.getDate()}</li>`
  46. }
  47. } else {
  48. if (first_lunarday == '初一') {
  49. if (lunarJanuary_month == '正月') {
  50. _hmtl += `<li data-time="${allDay_str}" class="lunar-january">${allDay.getDate()}</li>`
  51. } else {
  52. _hmtl += `<li data-time="${allDay_str}" class="lunar-first">${allDay.getDate()}</li>`
  53. }
  54. } else {
  55. _hmtl += `<li data-time="${allDay_str}">${allDay.getDate()}</li>`
  56. }
  57. }
  58. }
  59. return _hmtl
  60. },
  61. // 月份详情
  62. renderDetailMonth: function (dayWrapper, recivedYear, recivedMonth) {
  63. var array = []
  64. var recivedDate = new Date()
  65. var _html = ``
  66. var date = new Date(recivedYear, recivedMonth, 1)
  67. date.setDate(1)
  68. var week = date.getDay()
  69. date.setDate(1 - week)
  70. var month = date.getMonth()
  71. for (var i = 0; i < 42; i++) {
  72. if (month !== recivedMonth) {
  73. if (date.getDay() === 0 || date.getDay() === 6) {
  74. array.push({
  75. day: date.getDate(),
  76. lunar: ChineseCalendar.lunarTime(date),
  77. state: 'weekend',
  78. festival: ChineseCalendar.lunarFestival(date),
  79. term: ChineseCalendar.lunarTerm(date),
  80. dateStr: tools.returnDateStr(date)
  81. })
  82. } else {
  83. array.push({
  84. day: date.getDate(),
  85. lunar: ChineseCalendar.lunarTime(date),
  86. state: '',
  87. festival: ChineseCalendar.lunarFestival(date),
  88. term: ChineseCalendar.lunarTerm(date),
  89. dateStr: tools.returnDateStr(date)
  90. })
  91. }
  92. } else if(tools.curDay(date, recivedDate)) {
  93. if (date.getDay() === 0 || date.getDay() === 6) {
  94. array.push({
  95. day: date.getDate(),
  96. lunar: ChineseCalendar.lunarTime(date),
  97. state: 'weekend cur-day',
  98. festival: ChineseCalendar.lunarFestival(date),
  99. term: ChineseCalendar.lunarTerm(date),
  100. dateStr: tools.returnDateStr(date)
  101. })
  102. } else {
  103. array.push({
  104. day: date.getDate(),
  105. lunar: ChineseCalendar.lunarTime(date),
  106. state: 'cur-day',
  107. festival: ChineseCalendar.lunarFestival(date),
  108. term: ChineseCalendar.lunarTerm(date),
  109. dateStr: tools.returnDateStr(date)
  110. })
  111. }
  112. } else {
  113. if (date.getDay() === 0 || date.getDay() === 6) {
  114. array.push({
  115. day: date.getDate(),
  116. lunar: ChineseCalendar.lunarTime(date),
  117. state: 'weekend cur-month',
  118. festival: ChineseCalendar.lunarFestival(date),
  119. term: ChineseCalendar.lunarTerm(date),
  120. dateStr: tools.returnDateStr(date)
  121. })
  122. } else {
  123. array.push({
  124. day: date.getDate(),
  125. lunar: ChineseCalendar.lunarTime(date),
  126. state: 'cur-month',
  127. festival: ChineseCalendar.lunarFestival(date),
  128. term: ChineseCalendar.lunarTerm(date),
  129. dateStr: tools.returnDateStr(date)
  130. })
  131. }
  132. }
  133. date.setDate(date.getDate() + 1)
  134. month = date.getMonth()
  135. }
  136. for (var j = 0; j < array.length; j++) {
  137. var festival_state = array[j].festival ? 'festival show' : 'festival'
  138. var term_state = array[j].term ? 'term show' : 'term'
  139. var first_lunarday = array[j].lunar == '初一' ?
  140. ChineseCalendar.date2lunar(date).lunarMonthChiness + array[j].lunar:
  141. array[j].lunar
  142. if (array[j].lunar == '初一') {
  143. if (ChineseCalendar.date2lunar(date).lunarMonthChiness == '正月') {
  144. var lunar_state = 'lunar first-lunarJanuary'
  145. } else {
  146. var lunar_state = 'lunar first-lunarday'
  147. }
  148. } else {
  149. var lunar_state = 'lunar'
  150. }
  151. _html += `<li data-time="${array[j].dateStr}" class="${array[j].state}">
  152. <p class="info">
  153. <span class="${lunar_state}">${first_lunarday}</span>
  154. <span class="date"><em>${array[j].day}</em>日</span>
  155. </p>
  156. <p class="${festival_state}">${array[j].festival}</p>
  157. <p class="${term_state}">${array[j].term}</p>
  158. </li>`
  159. }
  160. dayWrapper.innerHTML = _html
  161. },
  162. nowDate: function () {
  163. return new Date()
  164. },
  165. returnDateStr: function (date) {
  166. var year = date.getFullYear();
  167. var month = date.getMonth();
  168. var day = date.getDate();
  169. month = month <= 9 ? ('0' + month) : ('' + month);
  170. day = day <= 9 ? ('0' + day) : ('' + day);
  171. return year + month + day;
  172. },
  173. // 是否是当天
  174. curDay: function (oldTime, nowTime) {
  175. return oldTime.getFullYear() === nowTime.getFullYear() &&
  176. oldTime.getMonth() === nowTime.getMonth() &&
  177. oldTime.getDate() === nowTime.getDate()
  178. },
  179. // 格式化日期 (如果这里传入的是字符串,那么得到的值不会按照下标去算)
  180. strFormatDate: function (str) {
  181. var date = new Date(parseInt(str.substr(0, 4)), parseInt(str.substr(4, 2)), parseInt(str.substr(6)))
  182. console.log(str)
  183. return date
  184. }
  185. }
  186. return toolsObj
  187. }())