time-picker.vue 23 KB

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