date-picker.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div >
  3. <u-popup ref="popup" class="popup" :mask="true" mode="bottom" >
  4. <view >
  5. <view class="popup-title ">
  6. <text >自定义时间</text>
  7. <text @click="$refs.popup.close()">×</text>
  8. </view>
  9. <view style="padding: 15px">
  10. <view class="popup-data dis j-s">
  11. <text :class="startShow? 'active' :''" @click="visible=true;startShow = true;endShow = false">{{ interviewStartTime||'开始时间' }}</text>至
  12. <text :class="endShow? 'active' :''" @click="visible=true;endShow = true;startShow = false">{{ interviewEndTime || '结束时间' }}</text>
  13. </view>
  14. <picker-view v-show="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange"
  15. class="picker-view">
  16. <picker-view-column>
  17. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  21. </picker-view-column>
  22. <picker-view-column>
  23. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  24. </picker-view-column>
  25. </picker-view>
  26. </view>
  27. </view>
  28. <view class="popup-footer dis j-s">
  29. <text @click.stop="reset" >重置</text>
  30. <text @click.stop="confirm" >确定</text>
  31. </view>
  32. </u-popup>
  33. </div>
  34. </template>
  35. <script>
  36. export default {
  37. data() {
  38. return {
  39. startShow: false,
  40. endShow: false,
  41. interviewStartTime:'',
  42. interviewEndTime:'',
  43. years: [],
  44. year: null,
  45. months: [],
  46. month: null,
  47. days: [],
  48. day: null,
  49. hours: [],
  50. hour: null,
  51. minutes: [],
  52. minute: null,
  53. value: [],
  54. indicatorStyle: `height: 50px;color:#2D6DFF `,
  55. visible:false
  56. }
  57. },
  58. props: {
  59. // visible: {
  60. // type: Boolean,
  61. // default: true
  62. // },
  63. // interviewTime: {
  64. // type: String,
  65. // default() {
  66. // return '';
  67. // },
  68. // }
  69. },
  70. mounted() {
  71. this.init();
  72. },
  73. watch: {
  74. // visible: {
  75. // handler(newValue, oldValue) {
  76. // if (newValue) {
  77. // if (this.interviewTime) {
  78. // this.init(this.interviewTime);
  79. // }
  80. // }
  81. // },
  82. // immediate: false,
  83. // deep: true
  84. // }
  85. },
  86. methods: {
  87. open(){
  88. this.$refs.popup.open()
  89. },
  90. bindChange: function (e) {
  91. const val = e.detail.value
  92. let year = this.years[val[0]]
  93. let isDay = 30, days = [];
  94. if (val[1]+1 == 2) {
  95. if((val[0]%4==0 && year%100!=0)||(year%400==0)){
  96. console.log('闰年')
  97. isDay = 29;
  98. }else {
  99. isDay = 28;
  100. console.log('平年')
  101. }
  102. } else if ([1,3,5,7,8,10,12].includes(val[1]+1)) {
  103. isDay = 31;
  104. } else {
  105. isDay = 30;
  106. }
  107. for (let i = 1; i <= isDay; i++) {
  108. days.push(i)
  109. }
  110. this.days = days;
  111. this.year = this.years[val[0]]
  112. this.month = this.months[val[1]]
  113. this.day = this.days[val[2]]
  114. if(this.startShow){
  115. this.interviewStartTime=this.year+'-'+this.month+'-'+this.day
  116. }
  117. if(this.endShow){
  118. this.interviewEndTime=this.year+'-'+this.month+'-'+this.day
  119. }
  120. },
  121. init() {
  122. const date = new Date();
  123. const years = []
  124. const year = date.getFullYear()
  125. const months = []
  126. const month = date.getMonth() + 1
  127. const days = []
  128. const day = date.getDate()
  129. let isDay = 30;
  130. if (month == 2) {
  131. if((year%4==0 && year%100!=0)||(year%400==0)){
  132. console.log('闰年')
  133. isDay = 29;
  134. }else {
  135. isDay = 28;
  136. console.log('平年')
  137. }
  138. } else if ([1,3,5,7,8,10,12].includes(month)) {
  139. isDay = 31;
  140. } else {
  141. isDay = 30;
  142. }
  143. for (let i = date.getFullYear(); i <= date.getFullYear()+12; i++) {
  144. years.push(i)
  145. }
  146. for (let i = 1; i <= 12; i++) {
  147. months.push(i)
  148. }
  149. for (let i = 1; i <= isDay; i++) {
  150. days.push(i)
  151. }
  152. this.years = years
  153. this.year = year
  154. this.months = months
  155. this.month = month
  156. this.days = days
  157. this.day = day
  158. this.value = [0, month-1, day-1]
  159. },
  160. // 补0
  161. padZeroStr(originStr){
  162. if(+originStr < 10){
  163. return String(originStr).padStart(2,'0')
  164. }
  165. return originStr + ''
  166. },
  167. close(){
  168. this.$emit('update:visible', false);
  169. },
  170. reset(){
  171. this.$emit('update:visible', false);
  172. this.interviewStartTime=''
  173. this.interviewEndTime =''
  174. },
  175. confirm() {
  176. // let monthStr = this.padZeroStr(this.month)
  177. // let dayStr = this.padZeroStr(this.day)
  178. // let hourStr = this.padZeroStr(this.hour)
  179. // let minuteStr = this.padZeroStr(this.minute)
  180. // this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
  181. this.$emit('confirmPickDate', this.interviewStartTime,this.interviewEndTime);
  182. this.$refs.popup.close()
  183. // this.$emit('update:visible', false);
  184. },
  185. },
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .popup {
  190. border-radius:10px 10px 0 0 !important;
  191. }
  192. .popup-title{
  193. font-size: 15px;
  194. color: #333333;
  195. padding:10px;
  196. margin-bottom: 10px;
  197. border-bottom: 1px solid #EEEEEE;
  198. position: relative;
  199. text-align: center;
  200. text:first-child {
  201. font-weight: 600;
  202. }
  203. text:last-child {
  204. position: absolute;
  205. right: 10px;
  206. font-size: 21px;
  207. color: #999999;
  208. line-height: 23px;
  209. }
  210. }
  211. .popup-footer{
  212. margin:10px 15px;
  213. text{
  214. font-size: 16px;
  215. padding: 5px 0;
  216. color: #FFFFFF;
  217. display: inline-block;
  218. width: 45%;
  219. text-align: center;
  220. }
  221. text:first-child {
  222. color: #2D6DFF;
  223. background: rgba(45,109,255,0.1);
  224. border-radius: 5px 5px 5px 5px;
  225. }
  226. text:last-child {
  227. background: linear-gradient( 132deg, #2DD9FF 0%, #2D6DFF 100%);
  228. border-radius: 5px 5px 5px 5px;
  229. }
  230. }
  231. .popup-data{
  232. line-height: 25px;
  233. text{
  234. font-size: 14px;
  235. color: #999999 ;
  236. display: inline-block;
  237. width: 45%;
  238. text-align: center;
  239. border: 1px solid #DDDDDD;
  240. border-radius: 2px 2px 2px 2px;
  241. }
  242. .active{
  243. border: 1px solid #2D6DFF;
  244. color: #2D6DFF;
  245. }
  246. }
  247. .picker-view {
  248. width: 100%;
  249. height: 150px;
  250. }
  251. </style>