dateTimePicker.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view class="zhezhao" v-show="shixian">
  3. <view class="xuanzeqi">
  4. <view class="quxiaoqueren">
  5. <view class="quniu" @click="quxiaobutton">取消</view>
  6. <view class="queniu" @click="quedingbutton">确定</view>
  7. </view>
  8. <picker-view v-if="visible" class="shigun" :indicator-style="indicatorStyle" :value="values" @change="bindChange">
  9. <picker-view-column>
  10. <view class="itemd" v-for="(item,index) in years" :key="index">{{item}}年</view>
  11. </picker-view-column>
  12. <picker-view-column>
  13. <view class="itemd" v-for="(item,index1) in months" :key="index1">{{item > 9 ? item : '0' + item}}月</view>
  14. </picker-view-column>
  15. <picker-view-column>
  16. <view class="itemd" v-for="(item,index2) in days" :key="index2">{{item > 9 ? item : '0' + item}}日</view>
  17. </picker-view-column>
  18. </picker-view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. shixian : Boolean
  26. },
  27. data() {
  28. const date = new Date();
  29. const years = [];
  30. const year = date.getFullYear()
  31. const months = []
  32. let month = date.getMonth() + 1
  33. const days = []
  34. let day = date.getDate()
  35. for (let i = 1900; i <= date.getFullYear(); i++) {
  36. years.push(i)
  37. }
  38. for (let i = 1; i <= 12; i++) {
  39. months.push(i)
  40. }
  41. for (let i = 1; i <= 31; i++) {
  42. days.push(i)
  43. }
  44. return {
  45. title: 'picker-view',
  46. years,
  47. year,
  48. months,
  49. month,
  50. days,
  51. day,
  52. values: [],
  53. visible: true,
  54. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`
  55. };
  56. },
  57. methods: {
  58. /* 获取当前时间 */
  59. getDate(type) {
  60. const date = new Date();
  61. let year = date.getFullYear();
  62. let month = date.getMonth() + 1;
  63. let day = date.getDate();
  64. if (type === 'start') {
  65. year = year - 60;
  66. } else if (type === 'end') {
  67. year = year + 2;
  68. }else if(type === 'now') {
  69. year = year;
  70. }
  71. month = month > 9 ? month : '0' + month;;
  72. day = day > 9 ? day : '0' + day;
  73. return `${year}-${month}-${day}`;
  74. },
  75. confirm(){
  76. let value = this.getDate({
  77. format: true
  78. });
  79. var dates = value.split("-");
  80. this.values[0] = dates[0] - 1940;
  81. this.values[1] = dates[1] - 1;
  82. this.values[2] = dates[2] - 1;
  83. this.year = dates[0];
  84. this.month = dates[1];
  85. this.day = dates[2];
  86. },
  87. bindChange: function (e) {
  88. const val = e.detail.value
  89. if( val.length > 0 ){
  90. this.year = this.years[val[0]]
  91. // this.month = this.months[val[1]] > 9 ? this.month : '0' + this.months[val[1]];
  92. if (this.months[val[1]] > 9) {
  93. this.month = this.months[val[1]];
  94. }else{
  95. this.month = '0' + this.months[val[1]];
  96. }
  97. // this.day = this.days[val[2]] > 9 ? this.month : '0' + this.days[val[2]];
  98. if (this.days[val[2]] > 9) {
  99. this.day = this.days[val[2]];
  100. }else{
  101. this.day = '0' + this.days[val[2]];
  102. }
  103. }
  104. },
  105. quxiaobutton () {
  106. this.values = [9999, this.month - 1, this.day - 1];
  107. this.$emit('quxiaoButton');
  108. },
  109. quedingbutton () {
  110. this.values = [9999, this.month - 1, this.day - 1];
  111. console.log(this.year,this.month,this.day)
  112. this.$emit('quedingButton',this.year,this.month,this.day);
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. .zhezhao{
  119. position: fixed;
  120. width: 100%;
  121. height: 100%;
  122. top: 0upx;
  123. right: 0upx;
  124. bottom: 0upx;
  125. left: 0upx;
  126. -webkit-transition: 0.15s;
  127. background: rgba(0,0,0,.5);
  128. z-index: 10;
  129. }
  130. .xuanzeqi{
  131. width: 100%;
  132. background-color: #FFF;
  133. height: 500upx;
  134. position: absolute;
  135. bottom: 0upx;
  136. left: 0upx;
  137. z-index: 20;
  138. display: block;
  139. }
  140. .quxiaoqueren{
  141. height: 80upx;
  142. width: 100%;
  143. display: block;
  144. position: relative;
  145. top: 0upx;
  146. left: 0upx;
  147. }
  148. .quniu{
  149. width: 80upx;
  150. border-radius: 6upx;
  151. height:50upx;
  152. line-height: 50upx;
  153. text-align: center;
  154. color: #000;
  155. float: left;
  156. margin-left: 20upx;
  157. font-size: 30upx;
  158. display: block;
  159. margin-top: 15upx;
  160. }
  161. .queniu{
  162. color: #F02929;
  163. float: right;
  164. display: block;
  165. font-size: 30upx;
  166. margin-right: 20upx;
  167. margin-top: 15upx;
  168. }
  169. .shigun{
  170. background-color: #FFF;
  171. width: 80%;
  172. display: block;
  173. height: 500upx;
  174. margin: 0 auto;
  175. }
  176. .itemd{
  177. text-align: center;
  178. justify-content: center;
  179. }
  180. .txt{
  181. color:#019FE8 ;
  182. }
  183. </style>