time-picker.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <template>
  2. <view class="uni-datetime-picker">
  3. <view @click="initTimePicker">
  4. <slot>
  5. <view class="uni-datetime-picker-timebox-pointer"
  6. :class="{'uni-datetime-picker-disabled': disabled, 'uni-datetime-picker-timebox': border}">
  7. <text class="uni-datetime-picker-text">{{ time }}</text>
  8. <view v-if="!time" class="uni-datetime-picker-time">
  9. <text class="uni-datetime-picker-text">{{ selectTimeText }}</text>
  10. </view>
  11. </view>
  12. </slot>
  13. </view>
  14. <view v-if="visible" id="mask" class="uni-datetime-picker-mask" @click="tiggerTimePicker"></view>
  15. <view v-if="visible" class="uni-datetime-picker-popup" :class="[dateShow && timeShow ? '' : 'fix-nvue-height']"
  16. :style="fixNvueBug">
  17. <view class="uni-title">
  18. <text class="uni-datetime-picker-text">{{ selectTimeText }}</text>
  19. </view>
  20. <view v-if="dateShow" class="uni-datetime-picker__container-box">
  21. <picker-view class="uni-datetime-picker-view" :indicator-style="indicatorStyle" :value="ymd"
  22. @change="bindDateChange">
  23. <picker-view-column>
  24. <view class="uni-datetime-picker-item" v-for="(item,index) in years" :key="index">
  25. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  26. </view>
  27. </picker-view-column>
  28. <picker-view-column>
  29. <view class="uni-datetime-picker-item" v-for="(item,index) in months" :key="index">
  30. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  31. </view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="uni-datetime-picker-item" v-for="(item,index) in days" :key="index">
  35. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  36. </view>
  37. </picker-view-column>
  38. </picker-view>
  39. <!-- 兼容 nvue 不支持伪类 -->
  40. <text class="uni-datetime-picker-sign sign-left">-</text>
  41. <text class="uni-datetime-picker-sign sign-right">-</text>
  42. </view>
  43. <view v-if="timeShow" class="uni-datetime-picker__container-box">
  44. <picker-view class="uni-datetime-picker-view" :class="[hideSecond ? 'time-hide-second' : '']"
  45. :indicator-style="indicatorStyle" :value="hms" @change="bindTimeChange">
  46. <picker-view-column>
  47. <view class="uni-datetime-picker-item" v-for="(item,index) in hours" :key="index">
  48. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  49. </view>
  50. </picker-view-column>
  51. <picker-view-column>
  52. <view class="uni-datetime-picker-item" v-for="(item,index) in minutes" :key="index">
  53. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  54. </view>
  55. </picker-view-column>
  56. <picker-view-column v-if="!hideSecond">
  57. <view class="uni-datetime-picker-item" v-for="(item,index) in seconds" :key="index">
  58. <text class="uni-datetime-picker-item">{{ lessThanTen(item) }}</text>
  59. </view>
  60. </picker-view-column>
  61. </picker-view>
  62. <!-- 兼容 nvue 不支持伪类 -->
  63. <text class="uni-datetime-picker-sign" :class="[hideSecond ? 'sign-center' : 'sign-left']">:</text>
  64. <text v-if="!hideSecond" class="uni-datetime-picker-sign sign-right">:</text>
  65. </view>
  66. <view class="uni-datetime-picker-btn">
  67. <view @click="clearTime">
  68. <text class="uni-datetime-picker-btn-text">{{ clearText }}</text>
  69. </view>
  70. <view class="uni-datetime-picker-btn-group">
  71. <view class="uni-datetime-picker-cancel" @click="tiggerTimePicker">
  72. <text class="uni-datetime-picker-btn-text">{{ cancelText }}</text>
  73. </view>
  74. <view @click="setTime">
  75. <text class="uni-datetime-picker-btn-text">{{ okText }}</text>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- #ifdef H5 -->
  81. <!-- <keypress v-if="visible" @esc="tiggerTimePicker" @enter="setTime" /> -->
  82. <!-- #endif -->
  83. </view>
  84. </template>
  85. <script>
  86. // #ifdef H5
  87. import keypress from './keypress'
  88. // #endif
  89. import {initVueI18n} from '@dcloudio/uni-i18n'
  90. import messages from './i18n/index.js'
  91. const {t} = initVueI18n(messages)
  92. /**
  93. * DatetimePicker 时间选择器
  94. * @description 可以同时选择日期和时间的选择器
  95. * @tutorial https://ext.dcloud.net.cn/plugin?id=xxx
  96. * @property {String} type = [datetime | date | time] 显示模式
  97. * @property {Boolean} multiple = [true|false] 是否多选
  98. * @property {String|Number} value 默认值
  99. * @property {String|Number} start 起始日期或时间
  100. * @property {String|Number} end 起始日期或时间
  101. * @property {String} return-type = [timestamp | string]
  102. * @event {Function} change 选中发生变化触发
  103. */
  104. export default {
  105. name: 'UniDatetimePicker',
  106. components: {
  107. // #ifdef H5
  108. keypress
  109. // #endif
  110. },
  111. data() {
  112. return {
  113. indicatorStyle: `height: 50px;`,
  114. visible: false,
  115. fixNvueBug: {},
  116. dateShow: true,
  117. timeShow: true,
  118. title: '日期和时间',
  119. // 输入框当前时间
  120. time: '',
  121. // 当前的年月日时分秒
  122. year: 1920,
  123. month: 0,
  124. day: 0,
  125. hour: 0,
  126. minute: 0,
  127. second: 0,
  128. // 起始时间
  129. startYear: 1920,
  130. startMonth: 1,
  131. startDay: 1,
  132. startHour: 0,
  133. startMinute: 0,
  134. startSecond: 0,
  135. // 结束时间
  136. endYear: 2120,
  137. endMonth: 12,
  138. endDay: 31,
  139. endHour: 23,
  140. endMinute: 59,
  141. endSecond: 59,
  142. }
  143. },
  144. props: {
  145. type: {
  146. type: String,
  147. default: 'datetime'
  148. },
  149. value: {
  150. type: [String, Number],
  151. default: ''
  152. },
  153. modelValue: {
  154. type: [String, Number],
  155. default: ''
  156. },
  157. start: {
  158. type: [Number, String],
  159. default: ''
  160. },
  161. end: {
  162. type: [Number, String],
  163. default: ''
  164. },
  165. returnType: {
  166. type: String,
  167. default: 'string'
  168. },
  169. disabled: {
  170. type: [Boolean, String],
  171. default: false
  172. },
  173. border: {
  174. type: [Boolean, String],
  175. default: true
  176. },
  177. hideSecond: {
  178. type: [Boolean, String],
  179. default: false
  180. }
  181. },
  182. watch: {
  183. value: {
  184. handler(newVal, oldVal) {
  185. if (newVal) {
  186. this.parseValue(this.fixIosDateFormat(newVal)) //兼容 iOS、safari 日期格式
  187. this.initTime(false)
  188. } else {
  189. this.time = ''
  190. this.parseValue(Date.now())
  191. }
  192. },
  193. immediate: true
  194. },
  195. type: {
  196. handler(newValue) {
  197. if (newValue === 'date') {
  198. this.dateShow = true
  199. this.timeShow = false
  200. this.title = '日期'
  201. } else if (newValue === 'time') {
  202. this.dateShow = false
  203. this.timeShow = true
  204. this.title = '时间'
  205. } else {
  206. this.dateShow = true
  207. this.timeShow = true
  208. this.title = '日期和时间'
  209. }
  210. },
  211. immediate: true
  212. },
  213. start: {
  214. handler(newVal) {
  215. this.parseDatetimeRange(this.fixIosDateFormat(newVal), 'start') //兼容 iOS、safari 日期格式
  216. },
  217. immediate: true
  218. },
  219. end: {
  220. handler(newVal) {
  221. this.parseDatetimeRange(this.fixIosDateFormat(newVal), 'end') //兼容 iOS、safari 日期格式
  222. },
  223. immediate: true
  224. },
  225. // 月、日、时、分、秒可选范围变化后,检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  226. months(newVal) {
  227. this.checkValue('month', this.month, newVal)
  228. },
  229. days(newVal) {
  230. this.checkValue('day', this.day, newVal)
  231. },
  232. hours(newVal) {
  233. this.checkValue('hour', this.hour, newVal)
  234. },
  235. minutes(newVal) {
  236. this.checkValue('minute', this.minute, newVal)
  237. },
  238. seconds(newVal) {
  239. this.checkValue('second', this.second, newVal)
  240. }
  241. },
  242. computed: {
  243. // 当前年、月、日、时、分、秒选择范围
  244. years() {
  245. return this.getCurrentRange('year')
  246. },
  247. months() {
  248. return this.getCurrentRange('month')
  249. },
  250. days() {
  251. return this.getCurrentRange('day')
  252. },
  253. hours() {
  254. return this.getCurrentRange('hour')
  255. },
  256. minutes() {
  257. return this.getCurrentRange('minute')
  258. },
  259. seconds() {
  260. return this.getCurrentRange('second')
  261. },
  262. // picker 当前值数组
  263. ymd() {
  264. return [this.year - this.minYear, this.month - this.minMonth, this.day - this.minDay]
  265. },
  266. hms() {
  267. return [this.hour - this.minHour, this.minute - this.minMinute, this.second - this.minSecond]
  268. },
  269. // 当前 date 是 start
  270. currentDateIsStart() {
  271. return this.year === this.startYear && this.month === this.startMonth && this.day === this.startDay
  272. },
  273. // 当前 date 是 end
  274. currentDateIsEnd() {
  275. return this.year === this.endYear && this.month === this.endMonth && this.day === this.endDay
  276. },
  277. // 当前年、月、日、时、分、秒的最小值和最大值
  278. minYear() {
  279. return this.startYear
  280. },
  281. maxYear() {
  282. return this.endYear
  283. },
  284. minMonth() {
  285. if (this.year === this.startYear) {
  286. return this.startMonth
  287. } else {
  288. return 1
  289. }
  290. },
  291. maxMonth() {
  292. if (this.year === this.endYear) {
  293. return this.endMonth
  294. } else {
  295. return 12
  296. }
  297. },
  298. minDay() {
  299. if (this.year === this.startYear && this.month === this.startMonth) {
  300. return this.startDay
  301. } else {
  302. return 1
  303. }
  304. },
  305. maxDay() {
  306. if (this.year === this.endYear && this.month === this.endMonth) {
  307. return this.endDay
  308. } else {
  309. return this.daysInMonth(this.year, this.month)
  310. }
  311. },
  312. minHour() {
  313. if (this.type === 'datetime') {
  314. if (this.currentDateIsStart) {
  315. return this.startHour
  316. } else {
  317. return 0
  318. }
  319. }
  320. if (this.type === 'time') {
  321. return this.startHour
  322. }
  323. },
  324. maxHour() {
  325. if (this.type === 'datetime') {
  326. if (this.currentDateIsEnd) {
  327. return this.endHour
  328. } else {
  329. return 23
  330. }
  331. }
  332. if (this.type === 'time') {
  333. return this.endHour
  334. }
  335. },
  336. minMinute() {
  337. if (this.type === 'datetime') {
  338. if (this.currentDateIsStart && this.hour === this.startHour) {
  339. return this.startMinute
  340. } else {
  341. return 0
  342. }
  343. }
  344. if (this.type === 'time') {
  345. if (this.hour === this.startHour) {
  346. return this.startMinute
  347. } else {
  348. return 0
  349. }
  350. }
  351. },
  352. maxMinute() {
  353. if (this.type === 'datetime') {
  354. if (this.currentDateIsEnd && this.hour === this.endHour) {
  355. return this.endMinute
  356. } else {
  357. return 59
  358. }
  359. }
  360. if (this.type === 'time') {
  361. if (this.hour === this.endHour) {
  362. return this.endMinute
  363. } else {
  364. return 59
  365. }
  366. }
  367. },
  368. minSecond() {
  369. if (this.type === 'datetime') {
  370. if (this.currentDateIsStart && this.hour === this.startHour && this.minute === this.startMinute) {
  371. return this.startSecond
  372. } else {
  373. return 0
  374. }
  375. }
  376. if (this.type === 'time') {
  377. if (this.hour === this.startHour && this.minute === this.startMinute) {
  378. return this.startSecond
  379. } else {
  380. return 0
  381. }
  382. }
  383. },
  384. maxSecond() {
  385. if (this.type === 'datetime') {
  386. if (this.currentDateIsEnd && this.hour === this.endHour && this.minute === this.endMinute) {
  387. return this.endSecond
  388. } else {
  389. return 59
  390. }
  391. }
  392. if (this.type === 'time') {
  393. if (this.hour === this.endHour && this.minute === this.endMinute) {
  394. return this.endSecond
  395. } else {
  396. return 59
  397. }
  398. }
  399. },
  400. /**
  401. * for i18n
  402. */
  403. selectTimeText() {
  404. return t("uni-datetime-picker.selectTime")
  405. },
  406. okText() {
  407. return t("uni-datetime-picker.ok")
  408. },
  409. clearText() {
  410. return t("uni-datetime-picker.clear")
  411. },
  412. cancelText() {
  413. return t("uni-datetime-picker.cancel")
  414. }
  415. },
  416. mounted() {
  417. // #ifdef APP-NVUE
  418. const res = uni.getSystemInfoSync();
  419. this.fixNvueBug = {
  420. top: res.windowHeight / 2,
  421. left: res.windowWidth / 2
  422. }
  423. // #endif
  424. },
  425. methods: {
  426. /**
  427. * @param {Object} item
  428. * 小于 10 在前面加个 0
  429. */
  430. lessThanTen(item) {
  431. return item < 10 ? '0' + item : item
  432. },
  433. /**
  434. * 解析时分秒字符串,例如:00:00:00
  435. * @param {String} timeString
  436. */
  437. parseTimeType(timeString) {
  438. if (timeString) {
  439. let timeArr = timeString.split(':')
  440. this.hour = Number(timeArr[0])
  441. this.minute = Number(timeArr[1])
  442. this.second = Number(timeArr[2])
  443. }
  444. },
  445. /**
  446. * 解析选择器初始值,类型可以是字符串、时间戳,例如:2000-10-02、'08:30:00'、 1610695109000
  447. * @param {String | Number} datetime
  448. */
  449. initPickerValue(datetime) {
  450. let defaultValue = null
  451. if (datetime) {
  452. defaultValue = this.compareValueWithStartAndEnd(datetime, this.start, this.end)
  453. } else {
  454. defaultValue = Date.now()
  455. defaultValue = this.compareValueWithStartAndEnd(defaultValue, this.start, this.end)
  456. }
  457. this.parseValue(defaultValue)
  458. },
  459. /**
  460. * 初始值规则:
  461. * - 用户设置初始值 value
  462. * - 设置了起始时间 start、终止时间 end,并 start < value < end,初始值为 value, 否则初始值为 start
  463. * - 只设置了起始时间 start,并 start < value,初始值为 value,否则初始值为 start
  464. * - 只设置了终止时间 end,并 value < end,初始值为 value,否则初始值为 end
  465. * - 无起始终止时间,则初始值为 value
  466. * - 无初始值 value,则初始值为当前本地时间 Date.now()
  467. * @param {Object} value
  468. * @param {Object} dateBase
  469. */
  470. compareValueWithStartAndEnd(value, start, end) {
  471. let winner = null
  472. value = this.superTimeStamp(value)
  473. start = this.superTimeStamp(start)
  474. end = this.superTimeStamp(end)
  475. if (start && end) {
  476. if (value < start) {
  477. winner = new Date(start)
  478. } else if (value > end) {
  479. winner = new Date(end)
  480. } else {
  481. winner = new Date(value)
  482. }
  483. } else if (start && !end) {
  484. winner = start <= value ? new Date(value) : new Date(start)
  485. } else if (!start && end) {
  486. winner = value <= end ? new Date(value) : new Date(end)
  487. } else {
  488. winner = new Date(value)
  489. }
  490. return winner
  491. },
  492. /**
  493. * 转换为可比较的时间戳,接受日期、时分秒、时间戳
  494. * @param {Object} value
  495. */
  496. superTimeStamp(value) {
  497. let dateBase = ''
  498. if (this.type === 'time' && value && typeof value === 'string') {
  499. const now = new Date()
  500. const year = now.getFullYear()
  501. const month = now.getMonth() + 1
  502. const day = now.getDate()
  503. dateBase = year + '/' + month + '/' + day + ' '
  504. }
  505. if (Number(value) && typeof value !== NaN) {
  506. value = parseInt(value)
  507. dateBase = 0
  508. }
  509. return this.createTimeStamp(dateBase + value)
  510. },
  511. /**
  512. * 解析默认值 value,字符串、时间戳
  513. * @param {Object} defaultTime
  514. */
  515. parseValue(value) {
  516. if (!value) {
  517. return
  518. }
  519. if (this.type === 'time' && typeof value === "string") {
  520. this.parseTimeType(value)
  521. } else {
  522. let defaultDate = null
  523. defaultDate = new Date(value)
  524. if (this.type !== 'time') {
  525. this.year = defaultDate.getFullYear()
  526. this.month = defaultDate.getMonth() + 1
  527. this.day = defaultDate.getDate()
  528. }
  529. if (this.type !== 'date') {
  530. this.hour = defaultDate.getHours()
  531. this.minute = defaultDate.getMinutes()
  532. this.second = defaultDate.getSeconds()
  533. }
  534. }
  535. if (this.hideSecond) {
  536. this.second = 0
  537. }
  538. },
  539. /**
  540. * 解析可选择时间范围 start、end,年月日字符串、时间戳
  541. * @param {Object} defaultTime
  542. */
  543. parseDatetimeRange(point, pointType) {
  544. // 时间为空,则重置为初始值
  545. if (!point) {
  546. if (pointType === 'start') {
  547. this.startYear = 1920
  548. this.startMonth = 1
  549. this.startDay = 1
  550. this.startHour = 0
  551. this.startMinute = 0
  552. this.startSecond = 0
  553. }
  554. if (pointType === 'end') {
  555. this.endYear = 2120
  556. this.endMonth = 12
  557. this.endDay = 31
  558. this.endHour = 23
  559. this.endMinute = 59
  560. this.endSecond = 59
  561. }
  562. return
  563. }
  564. if (this.type === 'time') {
  565. const pointArr = point.split(':')
  566. this[pointType + 'Hour'] = Number(pointArr[0])
  567. this[pointType + 'Minute'] = Number(pointArr[1])
  568. this[pointType + 'Second'] = Number(pointArr[2])
  569. } else {
  570. if (!point) {
  571. pointType === 'start' ? this.startYear = this.year - 60 : this.endYear = this.year + 60
  572. return
  573. }
  574. if (Number(point) && Number(point) !== NaN) {
  575. point = parseInt(point)
  576. }
  577. // datetime 的 end 没有时分秒, 则不限制
  578. const hasTime = /[0-9]:[0-9]/
  579. if (this.type === 'datetime' && pointType === 'end' && typeof point === 'string' && !hasTime.test(
  580. point)) {
  581. point = point + ' 23:59:59'
  582. }
  583. const pointDate = new Date(point)
  584. this[pointType + 'Year'] = pointDate.getFullYear()
  585. this[pointType + 'Month'] = pointDate.getMonth() + 1
  586. this[pointType + 'Day'] = pointDate.getDate()
  587. if (this.type === 'datetime') {
  588. this[pointType + 'Hour'] = pointDate.getHours()
  589. this[pointType + 'Minute'] = pointDate.getMinutes()
  590. this[pointType + 'Second'] = pointDate.getSeconds()
  591. }
  592. }
  593. },
  594. // 获取 年、月、日、时、分、秒 当前可选范围
  595. getCurrentRange(value) {
  596. const range = []
  597. for (let i = this['min' + this.capitalize(value)]; i <= this['max' + this.capitalize(value)]; i++) {
  598. range.push(i)
  599. }
  600. return range
  601. },
  602. // 字符串首字母大写
  603. capitalize(str) {
  604. return str.charAt(0).toUpperCase() + str.slice(1)
  605. },
  606. // 检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  607. checkValue(name, value, values) {
  608. if (values.indexOf(value) === -1) {
  609. this[name] = values[0]
  610. }
  611. },
  612. // 每个月的实际天数
  613. daysInMonth(year, month) { // Use 1 for January, 2 for February, etc.
  614. return new Date(year, month, 0).getDate();
  615. },
  616. //兼容 iOS、safari 日期格式
  617. fixIosDateFormat(value) {
  618. if (typeof value === 'string') {
  619. value = value.replace(/-/g, '/')
  620. }
  621. return value
  622. },
  623. /**
  624. * 生成时间戳
  625. * @param {Object} time
  626. */
  627. createTimeStamp(time) {
  628. if (!time) return
  629. if (typeof time === "number") {
  630. return time
  631. } else {
  632. time = time.replace(/-/g, '/')
  633. if (this.type === 'date') {
  634. time = time + ' ' + '00:00:00'
  635. }
  636. return Date.parse(time)
  637. }
  638. },
  639. /**
  640. * 生成日期或时间的字符串
  641. */
  642. createDomSting() {
  643. const yymmdd = this.year +
  644. '-' +
  645. this.lessThanTen(this.month) +
  646. '-' +
  647. this.lessThanTen(this.day)
  648. let hhmmss = this.lessThanTen(this.hour) +
  649. ':' +
  650. this.lessThanTen(this.minute)
  651. if (!this.hideSecond) {
  652. hhmmss = hhmmss + ':' + this.lessThanTen(this.second)
  653. }
  654. if (this.type === 'date') {
  655. return yymmdd
  656. } else if (this.type === 'time') {
  657. return hhmmss
  658. } else {
  659. return yymmdd + ' ' + hhmmss
  660. }
  661. },
  662. /**
  663. * 初始化返回值,并抛出 change 事件
  664. */
  665. initTime(emit = true) {
  666. this.time = this.createDomSting()
  667. if (!emit) return
  668. if (this.returnType === 'timestamp' && this.type !== 'time') {
  669. this.$emit('change', this.createTimeStamp(this.time))
  670. this.$emit('input', this.createTimeStamp(this.time))
  671. this.$emit('update:modelValue', this.createTimeStamp(this.time))
  672. } else {
  673. this.$emit('change', this.time)
  674. this.$emit('input', this.time)
  675. this.$emit('update:modelValue', this.time)
  676. }
  677. },
  678. /**
  679. * 用户选择日期或时间更新 data
  680. * @param {Object} e
  681. */
  682. bindDateChange(e) {
  683. const val = e.detail.value
  684. this.year = this.years[val[0]]
  685. this.month = this.months[val[1]]
  686. this.day = this.days[val[2]]
  687. },
  688. bindTimeChange(e) {
  689. const val = e.detail.value
  690. this.hour = this.hours[val[0]]
  691. this.minute = this.minutes[val[1]]
  692. this.second = this.seconds[val[2]]
  693. },
  694. /**
  695. * 初始化弹出层
  696. */
  697. initTimePicker() {
  698. if (this.disabled) return
  699. const value = this.fixIosDateFormat(this.value)
  700. this.initPickerValue(value)
  701. this.visible = !this.visible
  702. },
  703. /**
  704. * 触发或关闭弹框
  705. */
  706. tiggerTimePicker(e) {
  707. this.visible = !this.visible
  708. },
  709. /**
  710. * 用户点击“清空”按钮,清空当前值
  711. */
  712. clearTime() {
  713. this.time = ''
  714. this.$emit('change', this.time)
  715. this.$emit('input', this.time)
  716. this.$emit('update:modelValue', this.time)
  717. this.tiggerTimePicker()
  718. },
  719. /**
  720. * 用户点击“确定”按钮
  721. */
  722. setTime() {
  723. this.initTime()
  724. this.tiggerTimePicker()
  725. }
  726. }
  727. }
  728. </script>
  729. <style>
  730. .uni-datetime-picker {
  731. /* #ifndef APP-NVUE */
  732. /* width: 100%; */
  733. /* #endif */
  734. }
  735. .uni-datetime-picker-view {
  736. height: 130px;
  737. width: 270px;
  738. /* #ifndef APP-NVUE */
  739. cursor: pointer;
  740. /* #endif */
  741. }
  742. .uni-datetime-picker-item {
  743. height: 50px;
  744. line-height: 50px;
  745. text-align: center;
  746. font-size: 14px;
  747. }
  748. .uni-datetime-picker-btn {
  749. margin-top: 60px;
  750. /* #ifndef APP-NVUE */
  751. display: flex;
  752. cursor: pointer;
  753. /* #endif */
  754. flex-direction: row;
  755. justify-content: space-between;
  756. }
  757. .uni-datetime-picker-btn-text {
  758. font-size: 14px;
  759. color: #007AFF;
  760. }
  761. .uni-datetime-picker-btn-group {
  762. /* #ifndef APP-NVUE */
  763. display: flex;
  764. /* #endif */
  765. flex-direction: row;
  766. }
  767. .uni-datetime-picker-cancel {
  768. margin-right: 30px;
  769. }
  770. .uni-datetime-picker-mask {
  771. position: fixed;
  772. bottom: 0px;
  773. top: 0px;
  774. left: 0px;
  775. right: 0px;
  776. background-color: rgba(0, 0, 0, 0.4);
  777. transition-duration: 0.3s;
  778. z-index: 998;
  779. }
  780. .uni-datetime-picker-popup {
  781. border-radius: 8px;
  782. padding: 30px;
  783. width: 270px;
  784. /* #ifdef APP-NVUE */
  785. height: 500px;
  786. /* #endif */
  787. /* #ifdef APP-NVUE */
  788. width: 330px;
  789. /* #endif */
  790. background-color: #fff;
  791. position: fixed;
  792. top: 50%;
  793. left: 50%;
  794. transform: translate(-50%, -50%);
  795. transition-duration: 0.3s;
  796. z-index: 999;
  797. }
  798. .fix-nvue-height {
  799. /* #ifdef APP-NVUE */
  800. height: 330px;
  801. /* #endif */
  802. }
  803. .uni-datetime-picker-time {
  804. color: grey;
  805. }
  806. .uni-datetime-picker-column {
  807. height: 50px;
  808. }
  809. .uni-datetime-picker-timebox {
  810. border: 1px solid #E5E5E5;
  811. border-radius: 5px;
  812. padding: 7px 10px;
  813. /* #ifndef APP-NVUE */
  814. box-sizing: border-box;
  815. cursor: pointer;
  816. /* #endif */
  817. }
  818. .uni-datetime-picker-timebox-pointer {
  819. /* #ifndef APP-NVUE */
  820. cursor: pointer;
  821. /* #endif */
  822. }
  823. .uni-datetime-picker-disabled {
  824. opacity: 0.4;
  825. /* #ifdef H5 */
  826. cursor: not-allowed !important;
  827. /* #endif */
  828. }
  829. .uni-datetime-picker-text {
  830. font-size: 14px;
  831. }
  832. .uni-datetime-picker-sign {
  833. position: absolute;
  834. top: 53px;
  835. /* 减掉 10px 的元素高度,兼容nvue */
  836. color: #999;
  837. /* #ifdef APP-NVUE */
  838. font-size: 16px;
  839. /* #endif */
  840. }
  841. .sign-left {
  842. left: 86px;
  843. }
  844. .sign-right {
  845. right: 86px;
  846. }
  847. .sign-center {
  848. left: 135px;
  849. }
  850. .uni-datetime-picker__container-box {
  851. position: relative;
  852. display: flex;
  853. align-items: center;
  854. justify-content: center;
  855. margin-top: 40px;
  856. }
  857. .time-hide-second {
  858. width: 180px;
  859. }
  860. </style>