graceDateTimeBetweenBase.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <picker-view :indicator-style="indicatorStyle" class="graceDateTime-main" :value="defaultVal" @change="change">
  3. <picker-view-column>
  4. <text class="graceDateTime-item" v-for="(item, index) in sDate[0]" :key="index">{{item}}{{units[0]}}</text>
  5. </picker-view-column>
  6. <picker-view-column>
  7. <text class="graceDateTime-item" v-for="(item, index) in sDate[1]" :key="index">{{item}}{{units[1]}}</text>
  8. </picker-view-column>
  9. <picker-view-column>
  10. <text class="graceDateTime-item" v-for="(item, index) in sDate[2]" :key="index">{{item}}{{units[2]}}</text>
  11. </picker-view-column>
  12. <picker-view-column v-if="isTime">
  13. <text class="graceDateTime-item" v-for="(item, index) in sDate[3]" :key="index">{{item}}{{units[3]}}</text>
  14. </picker-view-column>
  15. <picker-view-column v-if="isTime">
  16. <text class="graceDateTime-item" v-for="(item, index) in sDate[4]" :key="index">{{item}}{{units[4]}}</text>
  17. </picker-view-column>
  18. <picker-view-column v-if="isTime && isSecond">
  19. <text class="graceDateTime-item" v-for="(item, index) in sDate[5]" :key="index">{{item}}{{units[5]}}</text>
  20. </picker-view-column>
  21. </picker-view>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. value : { type : String , default:''},
  27. isTime : {type : Boolean, default : true},
  28. isSecond : {type : Boolean, default : true},
  29. startYear : {type : Number, default : 1980},
  30. endYear : {type : Number, default : 2050},
  31. units : {type : Array , default:function(){return new Array('年','月','日','时','分','秒')}},
  32. height:{ type : String, default : '300rpx' }
  33. },
  34. data() {
  35. return {
  36. indicatorStyle : 'height:35px',
  37. defaultVal : [0,0,0,0,0,0],
  38. sDate:[[],[],[],[],[],[]]
  39. }
  40. },
  41. created() {this.init();},
  42. methods: {
  43. now : function () {
  44. var date = new Date();
  45. var y = date.getFullYear();
  46. var m = date.getMonth() + 1;
  47. m = m < 10 ? ('0' + m) : m;
  48. var d = date.getDate();
  49. d = d < 10 ? ('0' + d) : d;
  50. var h = date.getHours();
  51. h = h < 10 ? ('0' + h) : h;
  52. var minute = date.getMinutes();
  53. var second = date.getSeconds();
  54. minute = minute < 10 ? ('0' + minute) : minute;
  55. second = second < 10 ? ('0' + second) : second;
  56. return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;
  57. },
  58. arrayIndexOf : function(arr, needFind){
  59. var index = -1;
  60. for(let i = 0; i < arr.length; i++){if(arr[i] == needFind){index = i; return i;}}
  61. return index;
  62. },
  63. setValue : function (val) {
  64. if(val == ''){val = this.now();}
  65. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  66. var res = val.match(reg);
  67. if(res == null){
  68. reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
  69. res = val.match(reg);
  70. if(res == null){
  71. this.setValue(this.now());
  72. return ;
  73. }
  74. res[4] = '00';
  75. res[5] = '00';
  76. res[6] = '00';
  77. }
  78. this.setDefaults([res[1],res[2],res[3],res[4],res[5],res[6]]);
  79. },
  80. setDefaults : function (res) {
  81. for(let i = 0; i < res.length; i++){
  82. var index = this.arrayIndexOf(this.sDate[i], res[i]);
  83. if(index == -1){index = 0;}
  84. this.defaultVal.splice(i, 1, index);
  85. }
  86. this.changeBase(this.defaultVal);
  87. },
  88. // 初始化组件
  89. init:function(){
  90. if(this.endYear < this.startYear){this.endYear = this.startYear + 10;}
  91. var years = new Array();
  92. for(let i = this.startYear; i <= this.endYear; i++){years.push(i);}
  93. var months = new Array();
  94. for(let i = 1; i <= 12; i++){if(i < 10){months.push('0'+i);}else{months.push(i);}}
  95. var days = new Array();
  96. for(let i = 1; i <= 31; i++){if(i < 10){days.push('0'+i);}else{days.push(i);}}
  97. var hours = new Array();
  98. for(let i = 0; i < 24; i++){if(i < 10){hours.push('0'+i);}else{hours.push(i);}}
  99. var minutes = new Array();
  100. var seconds = new Array();
  101. for(let i = 0; i < 60; i++){
  102. if(i < 10){minutes.push('0'+i); seconds.push('0'+i);}else{minutes.push(i); seconds.push(i);}
  103. }
  104. this.sDate = [years, months, days, hours, minutes, seconds];
  105. this.$nextTick(()=>{setTimeout(()=>{ this.setValue(this.value);},800);});
  106. },
  107. change : function (res) {
  108. this.changeBase(res.detail.value);
  109. },
  110. changeBase:function(res){
  111. var date = new Date(this.sDate[0][res[0]], this.sDate[1][res[1]], 0);
  112. var days = date.getDate();
  113. var daysOut = new Array();
  114. for(let i = 1; i <= days; i++){if(i < 10){daysOut.push('0'+i);}else{daysOut.push(i);}}
  115. if(res[2] + 1 > days){res[2] = days - 1;}
  116. this.sDate.splice(2, 1, daysOut);
  117. this.defaultVal = res;
  118. if(this.isTime){
  119. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  120. this.sDate[1][this.defaultVal[1]],
  121. this.sDate[2][this.defaultVal[2]],
  122. this.sDate[3][this.defaultVal[3]],
  123. this.sDate[4][this.defaultVal[4]],
  124. this.sDate[5][this.defaultVal[5]]);
  125. }else{
  126. var resdata = new Array(this.sDate[0][this.defaultVal[0]],
  127. this.sDate[1][this.defaultVal[1]],
  128. this.sDate[2][this.defaultVal[2]])
  129. }
  130. this.$emit('change', resdata);
  131. },
  132. confirm:function () {
  133. if(this.isTime){
  134. var res = new Array(this.sDate[0][this.defaultVal[0]],
  135. this.sDate[1][this.defaultVal[1]],
  136. this.sDate[2][this.defaultVal[2]],
  137. this.sDate[3][this.defaultVal[3]],
  138. this.sDate[4][this.defaultVal[4]],
  139. this.sDate[5][this.defaultVal[5]]);
  140. }else{
  141. var res = new Array(this.sDate[0][this.defaultVal[0]],
  142. this.sDate[1][this.defaultVal[1]],
  143. this.sDate[2][this.defaultVal[2]])
  144. }
  145. this.$emit('confirm', res);
  146. }
  147. }
  148. }
  149. </script>
  150. <style scoped>
  151. .graceDateTime-main{width:100%; height:300rpx;}
  152. .graceDateTime-item{display:block; width:100%; height:35px; font-size:28rpx; line-height:35px; overflow:hidden; text-align:center;}
  153. </style>