date-picker.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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="startClick()">{{ interviewStartTime||'开始时间' }}</text>至
  12. <text :class="endShow? 'active' :''" @click="endClick()">{{ 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. isDay = 29;
  133. }else {
  134. isDay = 28;
  135. }
  136. } else if ([1,3,5,7,8,10,12].includes(month)) {
  137. isDay = 31;
  138. } else {
  139. isDay = 30;
  140. }
  141. for (let i = date.getFullYear(); i >= 1900; i--) {
  142. years.push(i)
  143. }
  144. for (let i = 1; i <= 12; i++) {
  145. months.push(i)
  146. }
  147. for (let i = 1; i <= isDay; i++) {
  148. days.push(i)
  149. }
  150. this.years = years
  151. this.year = year
  152. this.months = months
  153. this.month = month
  154. this.days = days
  155. this.day = day
  156. this.value = [0, month-1, day-1]
  157. },
  158. // 补0
  159. padZeroStr(originStr){
  160. if(+originStr < 10){
  161. return String(originStr).padStart(2,'0')
  162. }
  163. return originStr + ''
  164. },
  165. close(){
  166. this.$emit('update:visible', false);
  167. },
  168. reset(){
  169. this.$emit('update:visible', false);
  170. this.interviewStartTime=''
  171. this.interviewEndTime =''
  172. },
  173. startClick(){
  174. // this.interviewStartTime=''
  175. console.log(this.year ,this.month );
  176. this.interviewStartTime = this.year+'-'+this.month+'-'+this.day
  177. this.visible=true
  178. this.startShow = true
  179. this.endShow = false
  180. },
  181. endClick(){
  182. this.interviewEndTime= this.year+'-'+this.month+'-'+this.day
  183. this.visible=true
  184. this.startShow = false
  185. this.endShow = true
  186. },
  187. confirm() {
  188. // let monthStr = this.padZeroStr(this.month)
  189. // let dayStr = this.padZeroStr(this.day)
  190. // let hourStr = this.padZeroStr(this.hour)
  191. // let minuteStr = this.padZeroStr(this.minute)
  192. // this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
  193. this.$emit('confirmPickDate', this.interviewStartTime,this.interviewEndTime);
  194. this.$refs.popup.close()
  195. },
  196. },
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .popup {
  201. border-radius:10px 10px 0 0 !important;
  202. }
  203. .popup-title{
  204. font-size: 15px;
  205. color: #333333;
  206. padding:10px;
  207. margin-bottom: 10px;
  208. position: relative;
  209. text-align: center;
  210. text:first-child {
  211. font-weight: 600;
  212. }
  213. text:last-child {
  214. position: absolute;
  215. right: 10px;
  216. font-size: 21px;
  217. color: #999999;
  218. line-height: 23px;
  219. }
  220. }
  221. .popup-footer{
  222. margin:10px 15px;
  223. text{
  224. font-size: 16px;
  225. padding: 5px 0;
  226. color: #FFFFFF;
  227. display: inline-block;
  228. width: 45%;
  229. text-align: center;
  230. }
  231. text:first-child {
  232. color: #2D6DFF;
  233. background: rgba(45,109,255,0.1);
  234. border-radius: 5px 5px 5px 5px;
  235. }
  236. text:last-child {
  237. background: linear-gradient( 132deg, #2DD9FF 0%, #2D6DFF 100%);
  238. border-radius: 5px 5px 5px 5px;
  239. }
  240. }
  241. .popup-data{
  242. line-height: 25px;
  243. text{
  244. font-size: 14px;
  245. color: #999999 ;
  246. display: inline-block;
  247. width: 45%;
  248. text-align: center;
  249. border: 1px solid #DDDDDD;
  250. border-radius: 2px 2px 2px 2px;
  251. }
  252. .active{
  253. border: 1px solid #2D6DFF;
  254. color: #2D6DFF;
  255. }
  256. }
  257. .picker-view {
  258. width: 100%;
  259. height: 150px;
  260. }
  261. </style>