long-date.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <template>
  2. <view>
  3. <view class="long-data">
  4. <view class="flex plr-36 ptb-25 bb">
  5. <view>选择出发时间</view>
  6. <view @click="confirm" class="green">确定</view>
  7. </view>
  8. <!-- <view class="long-data-fr long-data-changeTimeIcon" @tap="openStatus">></view> -->
  9. <!-- <view class="long-data-changeTime long-data-fr" @tap="tapIsShow">{{year}}年{{month}}月{{day}}日 {{week}} {{hour}}:{{minute}}{{ type == 'day' ? '(' + desc + ')' : ''}}
  10. <view v-if="showCheck" class="long-data-check">{{checkStr}}</view>
  11. </view> -->
  12. <!-- <view class="long-data-check-triangle"></view> -->
  13. </view>
  14. <view class="long-data-picker" :style="'display:' + [isShow ? 'block' : 'none']">
  15. <picker-view :indicator-style="itemHeight" :value="dateValues" @change="bindDateChange">
  16. <picker-view-column>
  17. <view class="long-datetime-item" v-for="(item,index) in dateObj.dates" :key="index">{{item}}</view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="long-datetime-item" v-for="(item,index) in dateObj.hours" :key="index">{{item}}时</view>
  21. </picker-view-column>
  22. <picker-view-column>
  23. <view class="long-datetime-item" v-for="(item,index) in dateObj.minutes" :key="index">{{item}}分
  24. </view>
  25. </picker-view-column>
  26. </picker-view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. name: "long-date",
  33. props: {
  34. openStatus: { //是否展开状态
  35. type: Boolean,
  36. default () {
  37. return false //默认不展开
  38. }
  39. },
  40. type: { //模式
  41. type: String,
  42. default () {
  43. // return 'between' //根据开始时间和结束模式获取
  44. return 'day' //以天数获取开始时间模式
  45. }
  46. },
  47. getDayNum: { //获取天数(day模式可用)
  48. type: Number,
  49. default () {
  50. return 7
  51. }
  52. },
  53. chooesMaxDay: { //是否设置最大选择天数 (day模式可用)
  54. type: Number,
  55. default () {
  56. return 0 // 0 默认不设置 >0代表天数
  57. }
  58. },
  59. startTime: { //开始时间(between模式可用)
  60. type: String,
  61. default () {
  62. return ''
  63. }
  64. },
  65. endTime: { //结束时间(between模式可用)
  66. type: String,
  67. default () {
  68. return ''
  69. }
  70. },
  71. },
  72. data() {
  73. return {
  74. index: 0,//是否改变日期
  75. index1: 0,//是否改变小时
  76. index2: 0,//是否改变分钟
  77. chooesDate: '',
  78. year: '',
  79. month: '',
  80. day: '',
  81. hour: '',
  82. minute: '',
  83. // second,
  84. dateObj: '',
  85. checkStr: '出发时间不能小于当前时间',
  86. showCheck: false,
  87. isShow: this.openStatus,
  88. selectRes: '',
  89. itemHeight: `height: ${uni.upx2px(88)}px;`,
  90. dateValues: [0, 0, 0],
  91. };
  92. },
  93. computed: {
  94. //获取几天后的描述
  95. desc: function() {
  96. let chooes_time = new Date(this.chooesDate.toLocaleDateString())
  97. // var chooes_time = Date.parse(new Date('2019-10-30 10:00:00')) / 1000
  98. let now_time = new Date(new Date().toLocaleDateString())
  99. //获取多少秒
  100. let haveSecond = (chooes_time - now_time) / 86400000;
  101. if (haveSecond == 0) {
  102. return '今天'
  103. } else if (haveSecond == 1) {
  104. return '明天'
  105. } else if (haveSecond == 2) {
  106. return '后天'
  107. } else if (haveSecond == 3) {
  108. return '3天后'
  109. } else if (haveSecond == 4) {
  110. return '4天后'
  111. } else if (haveSecond == 5) {
  112. return '5天后'
  113. } else if (haveSecond == 6) {
  114. return '6天后'
  115. } else if (haveSecond == 7) {
  116. return '一周后'
  117. }
  118. },
  119. //获取星期几
  120. week: function() {
  121. let week = this.chooesDate.getDay()
  122. switch (week) {
  123. case 1:
  124. return '周一'
  125. break;
  126. case 2:
  127. return '周二'
  128. break;
  129. case 3:
  130. return '周三'
  131. break;
  132. case 4:
  133. return '周四'
  134. break;
  135. case 5:
  136. return '周五'
  137. break;
  138. case 6:
  139. return '周六'
  140. break;
  141. case 0:
  142. return '周日'
  143. break;
  144. }
  145. },
  146. },
  147. mounted() {
  148. this.initDate();
  149. },
  150. methods: {
  151. //初始化时间
  152. initDate() {
  153. switch (this.type) {
  154. case 'between':
  155. //获取开始时间
  156. var startDate = new Date(this.startTime + ' 00:00:00')
  157. //获取结束时间
  158. var endDate = new Date(this.endTime + ' 00:00:00');
  159. //设置默认选择时间
  160. this.chooesDate = new Date(this.startTime + ' 00:00:00')
  161. //获取多少天日期
  162. var num = (endDate - startDate) / 86400000
  163. break;
  164. case 'day':
  165. //获取当前时间
  166. var startDate = new Date();
  167. //设置默认选择时间
  168. this.chooesDate = new Date();
  169. //获取多少天日期
  170. var num = this.getDayNum
  171. break;
  172. }
  173. //获取年份
  174. let year = this.chooesDate.getFullYear()
  175. //获取月份
  176. let month = this.chooesDate.getMonth() + 1
  177. //获取当前日(1-31)
  178. let day = this.chooesDate.getDate()
  179. //获取当前的小时 并格式化1-10数字
  180. let h = this.chooesDate.getHours()
  181. // let hour = h < 10 ? '0' + h : h
  182. let hour = h
  183. console.log(hour);
  184. //获取当前的分钟数 并格式化1-10数字
  185. let m = this.chooesDate.getMinutes()
  186. let minute = m < 10 ? '0' + m : m
  187. let minTwo = minute.toString().substr(-1, 1)
  188. //设置日期数组
  189. let dates = []
  190. //设置小时数数组
  191. let hours = []
  192. //设置分钟数数组
  193. let minutes = []
  194. let str_minute;
  195. let str_hour;
  196. if (this.index != 0) {
  197. // 滚动日期时
  198. str_hour = 0
  199. str_minute = 0
  200. } else {
  201. if (Number(minute.toString().substr(-2, 1)) + 1 > 5) {
  202. if (this.index1 != 0) {
  203. str_hour = 0
  204. str_minute = 0
  205. } else {
  206. if (hour + 1 > 24) {
  207. str_hour = 0
  208. this.index = 1
  209. str_minute = Number(minute.toString().substr(-2, 1)) - 1
  210. } else {
  211. str_hour = hour + 1
  212. str_minute = Number(minute.toString().substr(-2, 1)) - 1
  213. }
  214. }
  215. } else {
  216. if (this.index1 != 0) {
  217. str_hour = 0
  218. str_minute = 0
  219. } else {
  220. if (hour + 1 > 24) {
  221. str_hour = 0
  222. this.index = 1
  223. str_minute = Number(minute.toString().substr(-2, 1)) - 1
  224. } else {
  225. str_hour = hour
  226. str_minute = Number(minute.toString().substr(-2, 1)) + 1
  227. }
  228. }
  229. }
  230. }
  231. for (let i = str_minute; i < 6; i++) {
  232. let str = i;
  233. str = str + '0';
  234. minutes.push(str);
  235. }
  236. console.log(str_hour);
  237. for (let i = str_hour; i < 24; i++) {
  238. let str = i;
  239. if (i < 10) {
  240. str = '0' + str;
  241. } else {
  242. str = '' + str;
  243. }
  244. hours.push(str);
  245. }
  246. for (let i = 0; i <= num; i++) { //获取包括的日期
  247. if (i != 0) {
  248. startDate.setDate(startDate.getDate() + 1)
  249. }
  250. let str_year = startDate.getFullYear()
  251. let str_month = startDate.getMonth() * 1 + 1
  252. let str_day;
  253. if (this.index == 0) {
  254. str_day = startDate.getDate()
  255. } else {
  256. //str_day = startDate.getDate() + 1
  257. str_day = startDate.getDate()
  258. }
  259. if (str_month < 10) {
  260. str_month = '0' + str_month;
  261. }
  262. if (str_day < 10) {
  263. str_day = '0' + str_day;
  264. }
  265. dates.push(str_year + '-' + str_month + '-' + str_day)
  266. }
  267. let dateObj = {
  268. dates,
  269. hours,
  270. minutes,
  271. }
  272. this.dateObj = dateObj
  273. if (this.index == 0 && this.index1 == 0 && this.index2 == 0) {
  274. this.checkStr = ''
  275. this.selectRes = dates[0] + ' ' + hours[0] + ':' + minutes[0]
  276. }
  277. },
  278. //切换日期选择显示状态
  279. tapIsShow() {
  280. this.isShow = !this.isShow
  281. },
  282. //滚动切换时间
  283. bindDateChange(e) { //有效日期的滚动日期时间方法
  284. // console.log(e.detail.value[0]);
  285. this.index = Number(e.detail.value[0])
  286. this.index1 = Number(e.detail.value[1])
  287. this.index2 = Number(e.detail.value[2])
  288. this.initDate()
  289. console.log('index:' + this.index);
  290. console.log('index1:' + this.index1);
  291. console.log('index2:' + this.index2);
  292. let valueArr = e.detail.value;
  293. let dateStr = this.dateObj.dates[valueArr[0]];
  294. let hourStr = this.dateObj.hours[valueArr[1]];
  295. let minuteStr = this.dateObj.minutes[valueArr[2]];
  296. let chooes_time;
  297. // console.log(hourStr);
  298. chooes_time = new Date(dateStr.split('-')[0], (Number(dateStr.split('-')[1]) - 1).toString(), dateStr
  299. .split('-')[2], hourStr, minuteStr, '00')
  300. this.chooesDate = chooes_time
  301. this.year = chooes_time.getFullYear()
  302. this.month = chooes_time.getMonth() + 1
  303. this.day = chooes_time.getDate()
  304. let h = chooes_time.getHours()
  305. let m = chooes_time.getMinutes()
  306. this.hour = h < 10 ? '0' + h : h
  307. this.minute = m < 10 ? '0' + m : m
  308. let max_day = this.chooesMaxDay;
  309. if (max_day > 0 && this.type == 'day') {
  310. let haveSecond = chooes_time - new Date();
  311. console.log(haveSecond);
  312. if (haveSecond < 0) {
  313. this.checkStr = '出发时间不能小于当前时间'
  314. this.showCheck = true
  315. } else {
  316. if ((haveSecond / 86400000) > max_day) {
  317. this.checkStr = '出发时间不能大于' + max_day + '天'
  318. this.showCheck = true
  319. } else {
  320. this.checkStr = ''
  321. this.showCheck = false
  322. }
  323. }
  324. }
  325. this.selectRes = this.year + '-' + this.month + '-' + this.day + ' ' + this.hour + ':' + this.minute;
  326. // this.$emit("select", {
  327. // time: this.selectRes,
  328. // tip:this.checkStr
  329. // });
  330. },
  331. // 点击确定
  332. confirm() { //有效日期的滚动日期时间方法
  333. console.log(111111);
  334. this.$emit("select", {
  335. time: this.selectRes,
  336. tip: this.checkStr
  337. });
  338. }
  339. },
  340. }
  341. </script>
  342. <style>
  343. .long-data {
  344. background: #FFFFFF;
  345. }
  346. .long-data-check {
  347. height: 40rpx;
  348. width: 100%;
  349. background: #e54d42;
  350. position: absolute;
  351. left: 0;
  352. top: 18rpx;
  353. color: #fff;
  354. line-height: 45rpx;
  355. border-radius: 0rpx;
  356. padding: 0px 20rpx;
  357. font-size: 20rpx;
  358. text-align: center;
  359. border-radius: 20rpx;
  360. }
  361. .long-data-check-triangle {
  362. width: 0;
  363. height: 0;
  364. border-top: 12rpx solid transparent;
  365. border-left: 10rpx solid #e54d42;
  366. border-bottom: 12rpx solid transparent;
  367. position: absolute;
  368. right: 223rpx;
  369. top: 26rpx;
  370. }
  371. .long-data-fl {
  372. float: left;
  373. }
  374. .long-data-fr {
  375. float: right;
  376. }
  377. .long-data-changeTime {
  378. color: #888;
  379. font-size: 25rpx;
  380. position: relative;
  381. text-align: right;
  382. padding: 0rpx 20rpx;
  383. }
  384. .long-data-changeTimeIcon {
  385. color: #888;
  386. }
  387. .long-data-picker {
  388. width: 100%;
  389. height: 250rpx;
  390. overflow: hidden;
  391. background: #fff;
  392. transition: height 0.3s;
  393. }
  394. .long-datetime-item {
  395. text-align: center;
  396. width: 100%;
  397. height: 88upx;
  398. line-height: 88upx;
  399. text-overflow: ellipsis;
  400. white-space: nowrap;
  401. font-size: 30upx;
  402. }
  403. .long-data-picker picker-view {
  404. height: 100%;
  405. }
  406. </style>