datetime.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <template>
  2. <view name='KXDateTime'>
  3. <popup ref="popup" type="bottom" :zIndex="1000">
  4. <view class="time-panel">
  5. <!-- <image src="/static/perm/cancel.png" mode="aspectFill" class="cancel-img" @click="close"></image> -->
  6. <view class="zicon-quxiao cancel" @click="close"></view>
  7. <view class="but">
  8. <text class="title">选择时间</text>
  9. </view>
  10. <picker-view v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange">
  11. <picker-view-column>
  12. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  19. </picker-view-column>
  20. <picker-view-column v-if="!onlyDate">
  21. <view class="item" v-for="(item,index) in hours" :key="index">{{item}}时</view>
  22. </picker-view-column>
  23. <picker-view-column v-if="!onlyDate">
  24. <view class="item" v-for="(item,index) in mins" :key="index">{{item}}分</view>
  25. </picker-view-column>
  26. </picker-view>
  27. <view class="btn" @click="ok">确定</view>
  28. </view>
  29. </popup>
  30. </view>
  31. </template>
  32. <script>
  33. // import uniPopup from '../uni-popup/uni-popup.vue'
  34. export default {
  35. name: 'KXDateTime',
  36. components: {
  37. // uniPopup
  38. },
  39. props: {
  40. start: '',
  41. end: '',
  42. default: '',
  43. onlyDate:{
  44. type: Boolean,
  45. default: false,
  46. }
  47. },
  48. data() {
  49. // const date = new Date()
  50. // const years = []
  51. // const year = date.getFullYear()
  52. // const months = []
  53. // const month = date.getMonth() + 1
  54. // const days = []
  55. // const day = date.getDate()
  56. // const hours = []
  57. // const hour = date.getHours()
  58. // const mins = []
  59. // const min = date.getMinutes()
  60. // let start;
  61. // if (this.start) {
  62. // start = this.start.replace(/-/g, "/")
  63. // start = new Date(start);
  64. // } else {
  65. // start = new Date(0);
  66. // }
  67. // let starty = start.getFullYear(); //开始年份
  68. // let end;
  69. // if (this.end) {
  70. // end = this.end.replace(/-/g, "/")
  71. // end = new Date(end);
  72. // } else {
  73. // end = new Date();
  74. // }
  75. // let endy = end.getFullYear(); //终止年份
  76. // for (let i = starty; i <= endy; i++) {
  77. // years.push(i)
  78. // }
  79. let defaultvalue = this.default;
  80. let value = [9999, 99, 99, 99, 99];
  81. if (defaultvalue == 'end') {
  82. value = [9999, 99, 99, 99, 99]
  83. } else if (defaultvalue == 'start') {
  84. value = [0, 0, 0, 0, 0]
  85. }
  86. return {
  87. title: 'picker-view',
  88. years: [],
  89. year: '',
  90. months: [],
  91. month: '',
  92. days: [],
  93. day: '',
  94. hours: [],
  95. hour: '',
  96. mins: [],
  97. min: '',
  98. value,
  99. valueStr: '',
  100. visible: true,
  101. strYMDHM: '',
  102. indicatorStyle: `height: 80rpx;`
  103. }
  104. },
  105. methods: {
  106. open() {
  107. let start;
  108. if (this.start) {
  109. start = this.start.replace(/-/g, "/")
  110. start = new Date(start);
  111. } else {
  112. start = new Date(0);
  113. }
  114. let starty = start.getFullYear(); //开始年份
  115. let end;
  116. if (this.end) {
  117. end = this.end.replace(/-/g, "/")
  118. end = new Date(end);
  119. } else {
  120. end = new Date();
  121. }
  122. if (start > end) {
  123. uni.showToast({
  124. title: '时间范围错误!',
  125. icon: 'none',
  126. duration: 2000
  127. });
  128. return false
  129. }
  130. this.$forceUpdate();
  131. if (this.valueStr) {
  132. this.value = JSON.parse(this.valueStr);
  133. setTimeout(this.amend, 100)
  134. this.$refs.popup.show()
  135. } else {
  136. setTimeout(this.amend, 100)
  137. this.$refs.popup.show()
  138. }
  139. },
  140. close() {
  141. this.$refs.popup.hide()
  142. this.$emit("close");
  143. },
  144. ok() {
  145. let day = this.day < 10 ? '0' + this.day : this.day,
  146. month = this.month < 10 ? '0' + this.month : this.month,
  147. hour = this.hour < 10 ? '0' + this.hour : this.hour,
  148. min = this.min < 10 ? '0' + this.min : this.min
  149. let data = this.year + '-' + month + '-' + day + ' ' + hour + ':' + min;
  150. this.$emit("rundata", data)
  151. this.$refs.popup.hide()
  152. },
  153. bindChange: function(e) {
  154. let val = e.detail.value
  155. this.valueStr = JSON.stringify(val);
  156. this.year = this.years[val[0]]
  157. this.month = this.months[val[1]]
  158. this.day = this.days[val[2]]
  159. this.hour = this.hours[val[3]]
  160. this.min = this.mins[val[4]]
  161. },
  162. //数据校正
  163. amend() {
  164. if (this.valueStr) {
  165. let val = JSON.parse(this.valueStr);
  166. this.year = this.years[val[0]]
  167. this.month = this.months[val[1]]
  168. this.day = this.days[val[2]]
  169. this.hour = this.hours[val[3]]
  170. this.min = this.mins[val[4]]
  171. }
  172. let start;
  173. if (this.start) {
  174. start = this.start.replace(/-/g, "/")
  175. start = new Date(start);
  176. } else {
  177. start = new Date(0);
  178. }
  179. let starty = start.getFullYear(); //开始年份
  180. let startm = start.getMonth() + 1; //开始月份
  181. let startd = start.getDate(); //开始天
  182. let starth = start.getHours(); //开始小时
  183. let startmin = start.getMinutes(); //开始分钟
  184. let end;
  185. if (this.end) {
  186. end = this.end.replace(/-/g, "/")
  187. end = new Date(end);
  188. } else {
  189. end = new Date();
  190. }
  191. let endy = end.getFullYear(); //终止年份
  192. let endm = end.getMonth() + 1; //终止月份
  193. let endd = end.getDate(); //终止天
  194. let endh = end.getHours(); //终止小时
  195. let endmin = end.getMinutes(); //终止分钟
  196. //如果选择起始年份
  197. let years = [],
  198. months = [],
  199. days = [],
  200. hours = [],
  201. mins = [];
  202. let month31 = [1, 3, 5, 7, 8, 10, 12],
  203. month30 = [4, 6, 9, 11];
  204. let daysNum;
  205. for (let i = starty; i <= endy; i++) {
  206. years.push(i)
  207. }
  208. if (month31.indexOf(this.month) > -1) {
  209. daysNum = 31
  210. } else if (month30.indexOf(this.month) > -1) {
  211. daysNum = 30
  212. } else {
  213. if (this.year % 4 == 0) {
  214. daysNum = 29
  215. } else {
  216. daysNum = 28
  217. }
  218. }
  219. let defaultvalue = this.default;
  220. let defaulty = endy,
  221. defaultm = endm,
  222. defaultd = endd,
  223. defaulth = endh,
  224. defaultmin = endmin;
  225. if (defaultvalue == 'end') {
  226. defaulty = endy;
  227. defaultm = endm;
  228. defaultd = endd;
  229. defaulth = endh;
  230. defaultmin = endmin;
  231. } else if (defaultvalue == 'start') {
  232. defaulty = starty;
  233. defaultm = startm;
  234. defaultd = startd;
  235. defaulth = starth;
  236. defaultmin = startmin;
  237. }
  238. //当数值异常是设施默认
  239. if (!this.year) {
  240. this.year = defaulty
  241. }
  242. if (!this.month) {
  243. this.month = defaultm
  244. }
  245. if (!this.day) {
  246. this.day = defaultd
  247. }
  248. if (!this.hour && this.hour !== 0) {
  249. this.hour = defaulth
  250. }
  251. if (!this.min && this.min !== 0) {
  252. this.min = defaultmin
  253. }
  254. //判断年份是在起始年
  255. if (this.year == starty) {
  256. //判断起始年份和终止年份是否相等
  257. if (starty == endy) {
  258. //如果等,那么月份取两者中间
  259. for (let i = startm; i <= endm; i++) {
  260. months.push(i)
  261. }
  262. //判断月份是在起始月
  263. if (this.month == startm) {
  264. //判断起始月和终止月是否相等
  265. if (startm == endm) {
  266. //如果等,那么天数取两者中间
  267. for (let i = startd; i <= endd; i++) {
  268. days.push(i)
  269. }
  270. //判断日是在起始日
  271. if (this.day == startd) {
  272. //判断起始ri和终止日是否相等
  273. if (startd == endd) {
  274. //如果等,那么小时取两者中间
  275. for (let i = starth; i <= endh; i++) {
  276. hours.push(i)
  277. }
  278. //判断小时是在起始小时
  279. if (this.hour == starth) {
  280. //判断起始和终止是否相等
  281. if (starth == endh) {
  282. //如果等,那么分钟取两者中间
  283. for (let i = startmin; i <= endmin; i++) {
  284. mins.push(i)
  285. }
  286. } else {
  287. //如果不等,到59
  288. for (let i = startmin; i <= 59; i++) {
  289. mins.push(i)
  290. }
  291. }
  292. } else {
  293. //判断小时是否在截止小时
  294. if (this.hour == endh) {
  295. //终止小时取到截止分钟
  296. for (let i = 0; i <= endmin; i++) {
  297. mins.push(i)
  298. }
  299. }
  300. }
  301. } else {
  302. //如果不等,到23小时
  303. for (let i = starth; i <= 23; i++) {
  304. hours.push(i)
  305. }
  306. //判断小时是在起始小时
  307. if (this.hour == starth) {
  308. for (let i = startmin; i <= 59; i++) {
  309. mins.push(i)
  310. }
  311. }
  312. }
  313. } else {
  314. //判断日是否在截止日
  315. if (this.day == endd) {
  316. //终止日取到截止小时
  317. for (let i = 0; i <= endh; i++) {
  318. hours.push(i)
  319. }
  320. //判断小时是否在截止小时
  321. if (this.hour == endh) {
  322. //终止小时取到截止分钟
  323. for (let i = 0; i <= endmin; i++) {
  324. mins.push(i)
  325. }
  326. }
  327. }
  328. }
  329. } else {
  330. //如果不等,
  331. for (let i = startd; i <= daysNum; i++) {
  332. days.push(i)
  333. }
  334. if (this.day == startd) {
  335. for (let i = starth; i <= 23; i++) {
  336. hours.push(i)
  337. }
  338. //判断小时是在起始小时
  339. if (this.hour == starth) {
  340. for (let i = startmin; i <= 59; i++) {
  341. mins.push(i)
  342. }
  343. }
  344. }
  345. }
  346. } else {
  347. //判断月份是在终止月
  348. if (this.month == endm) {
  349. //终止月取到截止天
  350. for (let i = 1; i <= endd; i++) {
  351. days.push(i)
  352. }
  353. //判断日是否在截止日
  354. if (this.day == endd) {
  355. //终止日取到截止小时
  356. for (let i = 0; i <= endh; i++) {
  357. hours.push(i)
  358. }
  359. //判断小时是否在截止小时
  360. if (this.hour == endh) {
  361. //终止小时取到截止分钟
  362. for (let i = 0; i <= endmin; i++) {
  363. mins.push(i)
  364. }
  365. }
  366. }
  367. }
  368. }
  369. } else {
  370. //如果不等,去开始到12月份
  371. for (let i = startm; i <= 12; i++) {
  372. months.push(i)
  373. }
  374. //判断月份是在起始月
  375. if (this.month == startm) {
  376. //是,取天数之后
  377. for (let i = startd; i <= daysNum; i++) {
  378. days.push(i)
  379. }
  380. //判断日是在起始日
  381. if (this.day == startd) {
  382. //是,qu起始小时之后
  383. for (let i = starth; i <= 23; i++) {
  384. hours.push(i)
  385. }
  386. //判断小时是在起始小时
  387. if (this.hour == starth) {
  388. //是,qu起始分钟之后
  389. for (let i = startmin; i <= 59; i++) {
  390. mins.push(i)
  391. }
  392. }
  393. }
  394. }
  395. }
  396. } else if (this.year == endy) {
  397. //年份中终止年
  398. //月份取到终止月
  399. for (let i = 1; i <= endm; i++) {
  400. months.push(i)
  401. }
  402. //判断月份是在终止月
  403. if (this.month == endm) {
  404. //终止月取到截止天
  405. for (let i = 1; i <= endd; i++) {
  406. days.push(i)
  407. }
  408. //判断日是否在截止日
  409. if (this.day == endd) {
  410. //终止日取到截止小时
  411. for (let i = 0; i <= endh; i++) {
  412. hours.push(i)
  413. }
  414. //判断小时是否在截止小时
  415. if (this.hour == endh) {
  416. //终止小时取到截止分钟
  417. for (let i = 0; i <= endmin; i++) {
  418. mins.push(i)
  419. }
  420. }
  421. }
  422. }
  423. } else {
  424. for (let i = 1; i <= 12; i++) {
  425. months.push(i)
  426. }
  427. for (let i = 1; i <= daysNum; i++) {
  428. days.push(i)
  429. }
  430. for (let i = 0; i <= 23; i++) {
  431. hours.push(i)
  432. }
  433. for (let i = 0; i <= 59; i++) {
  434. mins.push(i)
  435. }
  436. }
  437. if (months.length == 0) {
  438. for (let i = 1; i <= 12; i++) {
  439. months.push(i)
  440. }
  441. }
  442. if (days.length == 0) {
  443. for (let i = 1; i <= daysNum; i++) {
  444. days.push(i)
  445. }
  446. }
  447. if (hours.length == 0) {
  448. for (let i = 0; i <= 23; i++) {
  449. hours.push(i)
  450. }
  451. }
  452. if (mins.length == 0) {
  453. for (let i = 0; i <= 59; i++) {
  454. mins.push(i)
  455. }
  456. }
  457. this.years = years;
  458. this.months = months;
  459. this.days = days;
  460. this.hours = hours;
  461. this.mins = mins;
  462. this.$forceUpdate();
  463. }
  464. },
  465. watch: {
  466. year() {
  467. this.amend();
  468. },
  469. month() {
  470. this.amend();
  471. },
  472. day() {
  473. this.amend();
  474. },
  475. hour() {
  476. this.amend();
  477. },
  478. min() {
  479. this.amend();
  480. },
  481. years(n, m) {
  482. if (n.toString() != m.toString()) {
  483. this.amend();
  484. }
  485. },
  486. months(n, m) {
  487. if (n.toString() != m.toString()) {
  488. this.amend();
  489. }
  490. },
  491. days(n, m) {
  492. if (n.toString() != m.toString()) {
  493. this.amend();
  494. }
  495. },
  496. hours(n, m) {
  497. if (n.toString() != m.toString()) {
  498. this.amend();
  499. }
  500. },
  501. mins(n, m) {
  502. if (n.toString() != m.toString()) {
  503. this.amend();
  504. }
  505. }
  506. }
  507. }
  508. </script>
  509. <style lang="scss" scoped>
  510. .time-panel{
  511. width: 100%;
  512. height: 687rpx;
  513. background: #ffffff;
  514. border-radius: 20rpx 20rpx 0rpx 0rpx;
  515. padding: 45rpx 30rpx 0;
  516. position: relative;
  517. // bottom: var(--window-bottom);
  518. bottom: 0;
  519. .cancel{
  520. width: 88rpx;
  521. height: 88rpx;
  522. padding: 30rpx;
  523. position: absolute;
  524. top:0;
  525. right: 0;
  526. }
  527. }
  528. .cancel-img{
  529. width: 88rpx;
  530. height: 88rpx;
  531. padding: 30rpx;
  532. position: absolute;
  533. right: 0;
  534. top: 0;
  535. z-index: 6;
  536. }
  537. .but {
  538. background: #fff;
  539. // height: 80rpx;
  540. // line-height: 80rpx;
  541. width: 100%;
  542. text-align: left;
  543. .title{
  544. font-size: 36rpx;
  545. font-weight: 700;
  546. color: #333333;
  547. }
  548. }
  549. picker-view {
  550. width: 100%;
  551. background: #fff;
  552. height: 403rpx;
  553. text-align: center;
  554. .item{
  555. height: 80rpx;
  556. line-height: 80rpx;
  557. }
  558. }
  559. .btn{
  560. width: 620rpx;
  561. height: 90rpx;
  562. line-height: 90rpx;
  563. text-align: center;
  564. background: #009ffd;
  565. border-radius: 45rpx;
  566. box-shadow: 5rpx 5rpx 15rpx 0px rgba(0,159,253,0.6);
  567. font-size: 30rpx;
  568. font-weight: 500;
  569. color: #ffffff;
  570. margin:73rpx auto 42rpx;
  571. }
  572. </style>