get-date.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.getDateRange(2);
  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. formatDateT: function ( data, i) {
  99. var nd = new Date(),
  100. myMonth = data.getMonth() + 1,
  101. myDate = data.getDate();
  102. if (myMonth < 10) {
  103. myMonth = '0' + myMonth;
  104. }
  105. if (i == 1) {
  106. myMonth += '月'
  107. } else {
  108. var m1 = nd.getMonth() + 1,
  109. m2 = data.getMonth() + 1;
  110. if (!this.isNextM) {
  111. if (m1 < m2) {
  112. this.isNextM = true;
  113. myMonth += '月'
  114. } else {
  115. myMonth = '';
  116. }
  117. } else {
  118. myMonth = '';
  119. }
  120. }
  121. if (myDate < 10) {
  122. myDate = '0' + myDate;
  123. }
  124. return myMonth + myDate;
  125. },
  126. formatDate: function (date) {
  127. var myYear = date.getFullYear(),
  128. myMonth = date.getMonth() + 1,
  129. myDate = date.getDate();
  130. if (myMonth < 10) {
  131. myMonth = '0' + myMonth;
  132. }
  133. if (myDate < 10) {
  134. myDate = '0' + myDate;
  135. }
  136. return myYear + '-' + myMonth + '-' + myDate;
  137. }
  138. };
  139. w.$d = d;
  140. })( jQuery, window);