graceDateBetween.nvue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view>
  3. <view class="gui-date-bt-block" :style="{background:weekBg}">
  4. <text class="gui-date-bt-item gui-date-bt-week">一</text>
  5. <text class="gui-date-bt-item gui-date-bt-week">二</text>
  6. <text class="gui-date-bt-item gui-date-bt-week">三</text>
  7. <text class="gui-date-bt-item gui-date-bt-week">四</text>
  8. <text class="gui-date-bt-item gui-date-bt-week">五</text>
  9. <text class="gui-date-bt-item gui-date-bt-week">六</text>
  10. <text class="gui-date-bt-item gui-date-bt-week">日</text>
  11. </view>
  12. <view v-for="(days, daysIndex) in daysData" :key="daysIndex">
  13. <view class="gui-date-bt-month-w">
  14. <text class="gui-date-bt-month" :style="{backgroundColor:startBg}">{{days[0][0]}}{{unit[0]}}{{days[0][1]}}{{unit[1]}}</text>
  15. </view>
  16. <view class="gui-date-bt-block">
  17. <text class="gui-date-bt-item gui-date-bt-days" v-for="(item, index) in days[1]" :key="index"
  18. :style="{backgroundColor:item[1] ? sedBg : itemBg, color:item[1]?sedColor:color}"
  19. :data-dt="item[0] == '' ? '' : days[0][0]+''+days[0][1]+''+item[0]" @tap.stop="selectDay">{{item[0]}}<text class="gui-date-bt-start" v-if="sedDays[0] == days[0][0]+''+days[0][1]+''+item[0] || sedDays[1] == days[0][0]+''+days[0][1]+''+item[0]"
  20. :style="{backgroundColor:sedDays[1] == days[0][0]+''+days[0][1]+''+item[0] ? endBg : startBg, color:sedColor}">{{item[0]}}</text></text>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. /* 组件百分百原创 拒绝任何代码抄袭及思路抄袭 */
  27. export default{
  28. props:{
  29. weekBg:{type:String, default:'#F8F8F8'},
  30. unit:{type:Array,default:function(){return [' 年 ',' 月'];}},
  31. sedBg:{type:String, default:'rgba(54,136,255,0.88)'},
  32. itemBg:{type:String, default:'#FFFFFF'},
  33. color:{type:String, default:'#323232'},
  34. sedColor:{type:String, default:'#FFFFFF'},
  35. startBg:{type:String, default:'rgba(54,136,255,1)'},
  36. endBg:{type:String,default:'rgba(54,136,255,1)'},
  37. monthNumber:{type:Number,default:2}
  38. },
  39. data() {
  40. return {
  41. startYear:'1900',
  42. startMonth:"01",
  43. endYear:'1900',
  44. endMonth:"01",
  45. nextdays:[],
  46. sedDays:[0,0],
  47. btDays :[],
  48. nextbtDays:[],
  49. daysData : []
  50. }
  51. },
  52. created:function(){
  53. this.setMonth(this.currentMonth());
  54. },
  55. methods:{
  56. setBetween:function(days){
  57. if(days[1] == 0){days[1] = days[0];}
  58. days[0] = Number(days[0]);
  59. days[1] = Number(days[1]);
  60. this.sedDays = days;
  61. var countNumber = 0;
  62. var daysNew = [];
  63. this.daysData.forEach((itm)=>{
  64. var year = itm[0][0];
  65. var month = itm[0][1];
  66. var daysIn = [];
  67. itm[1].forEach((item)=>{
  68. var cDay = year+''+month+''+item[0];
  69. cDay = Number(cDay);
  70. if(cDay >= days[0] && cDay <= days[1]){
  71. item[1] = true;
  72. countNumber++;
  73. }else{
  74. item[1] = false;
  75. }
  76. daysIn.push(item);
  77. });
  78. daysNew.push([[year, month], daysIn]);
  79. });
  80. this.daysData = daysNew;
  81. this.$emit('selectDate', [days, countNumber]);
  82. },
  83. setMonth:function(month){
  84. var reg = /^([0-9]{4}).*([0-9]{2}).*$/;
  85. var res = month.match(reg);
  86. if(res == null){month = this.currentMonth(); res = month.match(reg);}
  87. this.setMonthBase(res);
  88. },
  89. setMonthBase : function(res){
  90. var daysData = [];
  91. if(res[2].substr(0,1) == '0'){res[2] = res[2].substr(1);}
  92. res[1] = Number(res[1]);
  93. res[2] = Number(res[2]);
  94. for(let i = 0; i < this.monthNumber; i++){
  95. var year = res[1];
  96. var month = res[2] + i;
  97. if(month > 12){month = 1; year += 1;}
  98. if(month < 10){month = '0'+month;}
  99. daysData[i] = [];
  100. daysData[i].push([year, month]);
  101. var days = this.getDays(year,month);
  102. var daysList = [];
  103. for (let ii = (0 - days[1]); ii < days[0]; ii++){
  104. if(ii >= 0){
  105. daysList.push([ii >= 9 ? ii + 1 : '0' + (ii+1), false]);
  106. }else{
  107. daysList.push(['',false]);
  108. }
  109. }
  110. daysData[i].push(daysList);
  111. }
  112. this.daysData = daysData;
  113. },
  114. currentMonth : function () {
  115. var date = new Date();
  116. var y = date.getFullYear();
  117. var m = date.getMonth() + 1;
  118. m = m < 10 ? ('0' + m) : m;
  119. return y + '年' + m + '月';
  120. },
  121. getDays : function(year,month){
  122. var d = new Date(year, month, 0);
  123. var days = d.getDate();
  124. var d2 = new Date(year, month - 1, 0);
  125. var startWeek = d2.getDay();
  126. return [days, startWeek];
  127. },
  128. selectDay:function(e){
  129. var day = e.currentTarget.dataset.dt;
  130. if(day == ''){return ;}
  131. day = Number(day);
  132. if(this.sedDays[0] == 0){ this.sedDays[0] = day; }
  133. else if(this.sedDays[1] == 0 || this.sedDays[0] == this.sedDays[1]){
  134. if(day > this.sedDays[0]){
  135. this.sedDays[1] = day;
  136. }else if(day < this.sedDays[0]){
  137. this.sedDays[1] = this.sedDays[0];
  138. this.sedDays[0] = day;
  139. }
  140. }else{
  141. this.sedDays[0] = day;
  142. this.sedDays[1] = 0;
  143. }
  144. this.setBetween(this.sedDays);
  145. }
  146. }
  147. }
  148. </script>
  149. <style>
  150. .gui-date-bt-block{width:700rpx; flex-direction:row; flex-wrap:wrap;}
  151. .gui-date-bt-item{width:100rpx; text-align:center; position:relative;}
  152. .gui-date-bt-week{line-height:66rpx; height:66rpx; font-size:28rpx;}
  153. .gui-date-bt-month-w{flex-direction:row; flex-wrap:nowrap; margin-top:15px; margin-bottom:15px;}
  154. .gui-date-bt-month{line-height:50rpx; text-align:center; color:#FFFFFF; width:258rpx; border-radius:30rpx; font-size:26rpx;}
  155. .gui-date-bt-days{line-height:100rpx; height:100rpx; font-size:28rpx;}
  156. .gui-date-bt-start{position:absolute; font-size:28rpx; width:100rpx; height:100rpx; text-align:center; line-height:100rpx; z-index:1; left:0; top:0;}
  157. </style>