get-date.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. (function ( $, w) {
  2. var d = {
  3. nowYear: '',
  4. nowMonth: '',
  5. nowDay: '',
  6. nowDayOfWeek: '',
  7. isNextM: false,
  8. init: function (type) {
  9. var now = new Date(),
  10. getDate = '';
  11. this.isNextM = false;
  12. this.nowYear = now.getFullYear();
  13. this.nowMonth = now.getMonth();
  14. this.nowDay = now.getDate();
  15. this.nowDayOfWeek = now.getDay()==0?7:now.getDay();
  16. switch (type) {
  17. case 1:
  18. getDate = this.getWeekStartDate();
  19. break;
  20. case 2:
  21. getDate = this.getWeekEndDate();
  22. break;
  23. case 3:
  24. getDate = this.getMonthStartDate();
  25. break;
  26. case 4:
  27. getDate = this.getMonthEndDate();
  28. break;
  29. case 5:
  30. getDate = this.getDateRange(1);
  31. break;
  32. case 6:
  33. getDate = this.getDateRangeNew();
  34. break;
  35. }
  36. return getDate;
  37. },
  38. getWeekStartDate: function () { //获得本周的开始日期
  39. var weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1);
  40. return this.formatDate(weekStartDate);
  41. },
  42. getWeekEndDate: function () { //获得本周的结束日期
  43. var weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek + 1));
  44. return this.formatDate(weekEndDate);
  45. },
  46. getMonthStartDate: function (){//获得本月的开始日期
  47. var monthStartDate = new Date(this.nowYear, this.nowMonth, 1);
  48. return this.formatDate(monthStartDate);
  49. },
  50. getMonthEndDate: function () {//获得本月的结束日期
  51. var monthEndDate = new Date(this.nowYear, this.nowMonth, this.getMonthDays(this.nowMonth));
  52. return this.formatDate(monthEndDate);
  53. },
  54. getMonthDays: function (myMonth) { //获得某月的天数
  55. var monthStartDate = new Date(this.nowYear, myMonth, 1);
  56. var monthEndDate = new Date(this.nowYear, myMonth + 1, 1);
  57. var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
  58. return days;
  59. },
  60. getWeeksNum: function (){//获取一个月有几周
  61. var day=new Date(),
  62. lastCurDay = new Date(day.getFullYear(), day.getMonth() + 1, 0),//获取当前月最后一天时间
  63. a = lastCurDay.getFullYear(),
  64. b = lastCurDay.getMonth(),
  65. c = lastCurDay.getDate();
  66. var date = new Date(a, b, c),
  67. w = date.getDay(),
  68. d = date.getDate();
  69. return Math.ceil(
  70. (d + 6 - w) / 7
  71. );
  72. },
  73. getYear: function () {//获取签约年
  74. var nd = new Date(),
  75. year = nd.getFullYear(),
  76. cd = new Date( year + '', '06', '30', '23', '59', '59');
  77. if (nd.getTime() <= cd.getTime()) {
  78. year -= 1;
  79. }
  80. return year;
  81. },
  82. getDateRange: function (n) {//获取本周的所有日期
  83. var startDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek),
  84. arr = [];
  85. for (var i = 1; i <= 7; i++) {
  86. startDate.setDate(startDate.getDate() + 1);
  87. var d = [];
  88. if (n == 2) {
  89. d = this.formatDateT( startDate, i);
  90. }
  91. if (n == 1) {
  92. d = this.formatDate( startDate, i);
  93. }
  94. arr.push(d);
  95. }
  96. return arr;
  97. },
  98. getDateRangeNew: function () {//获取本周的所有日期
  99. var startDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek),
  100. arr = [];
  101. for (var i = 1; i <= 7; i++) {
  102. startDate.setDate(startDate.getDate() + 1);
  103. var d = [];
  104. d = this.formatDateNew( startDate, i);
  105. arr.push(d);
  106. }
  107. return arr;
  108. },
  109. formatDateNew: function ( data, i) {
  110. var nd = new Date(),
  111. myMonth = data.getMonth() + 1,
  112. myDate = data.getDate();
  113. if (myMonth < 10) {
  114. myMonth = '0' + myMonth;
  115. }
  116. if (myDate < 10) {
  117. myDate = '0' + myDate;
  118. }
  119. return myMonth +"-"+ myDate;
  120. },
  121. formatDateT: function ( data, i) {
  122. var nd = new Date(),
  123. myMonth = data.getMonth() + 1,
  124. myDate = data.getDate();
  125. if (myMonth < 10) {
  126. myMonth = '0' + myMonth;
  127. }
  128. if (i == 1) {
  129. myMonth += '月'
  130. } else {
  131. var m1 = nd.getMonth() + 1,
  132. m2 = data.getMonth() + 1;
  133. if (!this.isNextM) {
  134. if (m1 < m2) {
  135. this.isNextM = true;
  136. myMonth += '月'
  137. } else {
  138. myMonth = '';
  139. }
  140. } else {
  141. myMonth = '';
  142. }
  143. }
  144. if (myDate < 10) {
  145. myDate = '0' + myDate;
  146. }
  147. return myMonth + myDate;
  148. },
  149. formatDate: function (date) {
  150. var myYear = date.getFullYear(),
  151. myMonth = date.getMonth() + 1,
  152. myDate = date.getDate();
  153. if (myMonth < 10) {
  154. myMonth = '0' + myMonth;
  155. }
  156. if (myDate < 10) {
  157. myDate = '0' + myDate;
  158. }
  159. return myYear + '-' + myMonth + '-' + myDate;
  160. }
  161. };
  162. w.$d = d;
  163. })( jQuery, window);