time.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="times" v-if="isTimeShow">
  3. <view class="timezhe" @click="closeTime"></view>
  4. <view class="timecontent">
  5. <view class="timetile">
  6. <text class="timeClose" @click="closeTime">取消</text>
  7. <text class="timeSelese">选择时间</text>
  8. <text class="timeSuccess" @click="oktime">确认</text>
  9. </view>
  10. <view class="timeCon">
  11. <view class="timeCons" @click="clickTimeTitle" data-id='0' :class="[timeIds===0?'activeTime':'']">按年</view>
  12. <view class="timeCons" @click="clickTimeTitle" data-id='1' :class="[timeIds===1?'activeTime':'']">按月</view>
  13. <view class="timeCons daytime" @click="clickTimeTitle" data-id='2' :class="[timeIds===2?'activeTime':'']">按日</view>
  14. </view>
  15. <!--时间逻辑-->
  16. <view class="timeScroll">
  17. <picker-view indicator-style="height: 45px;" :style="{width:'100%',height:'100%'}" :value="value" @change="bindChange">
  18. <picker-view-column>
  19. <view v-for="(item,i) in years" :key="i" class="columns">{{item}}</view>
  20. </picker-view-column>
  21. <picker-view-column v-if="timeIds===1 || timeIds===2">
  22. <view v-for="(item,i) in months" :key="i" class="columns">{{item}}</view>
  23. </picker-view-column>
  24. <picker-view-column v-if="timeIds===2">
  25. <view v-for="(item,i) in days" :key="i" class="columns">{{item}}</view>
  26. </picker-view-column>
  27. </picker-view>
  28. </view>
  29. <!--时间逻辑-->
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default{
  35. props:{
  36. isTimeShow:{
  37. type:Boolean,
  38. default:false
  39. }
  40. },
  41. data(){
  42. return{
  43. timeIds:2,
  44. years: [],//选择器年份范围
  45. year: '',//当前年份
  46. months: [],//选择器月份范围
  47. month: '',//当前月份(置于为什么+1,自行百度看原理)
  48. days:[],//选择器的天数范围
  49. day:[],//当前天数
  50. value: [],//选择器弹出的被动选项
  51. newmonth: '',
  52. newyear: '',
  53. newday:'',
  54. }
  55. },
  56. mounted(){
  57. this.huoTime();
  58. },
  59. methods:{
  60. huoTime(){
  61. // 获取当前的年月
  62. const date = new Date();
  63. //滚动选择器的范围
  64. const years = []
  65. const months = []
  66. for (let i = 1990; i <= date.getFullYear(); i++) {
  67. years.push(i)
  68. }
  69. for (let i = 1; i <= 12; i++) {
  70. months.push(i)
  71. }
  72. /*计算这个月有多少天*/
  73. this.dayfull(date.getFullYear(),date.getMonth()+1,date.getDate())
  74. /*计算这个月有多少天*/
  75. this.year = date.getFullYear()
  76. this.month = date.getMonth()+1
  77. this.value = [9999, date.getMonth(),date.getDate()-1]
  78. this.newmonth = date.getMonth()+1
  79. this.years = years
  80. this.newyear = date.getFullYear()
  81. this.months = months
  82. /*默认将年月日传回给父组件*/
  83. this.$emit("monthTime",{
  84. year:date.getFullYear(),
  85. month:date.getMonth()+1,
  86. day:date.getDate()
  87. })
  88. /*默认将年月日传回给父组件*/
  89. },
  90. /*计算这个月有多少天*/
  91. dayfull(year,month,day){
  92. const days = []
  93. let dayAllThisMonth = 31;
  94. if (month != 12) {
  95. let currentMonthStartDate = new Date(year + "/" + (month) + "/01"); // 本月1号的日期
  96. let nextMonthStartDate = new Date(year + "/" + (month + 1) + "/01"); // 下个月1号的日期
  97. dayAllThisMonth = (nextMonthStartDate - currentMonthStartDate) / (24 * 3600 * 1000);
  98. }
  99. for (let i = 1; i <= dayAllThisMonth; i++) {
  100. days.push(i)
  101. }
  102. this.day = day
  103. this.days = days
  104. },
  105. /*计算这个月有多少天*/
  106. clickTimeTitle(e){
  107. let id = parseInt(e.currentTarget.dataset.id);
  108. switch (id){
  109. case 0:
  110. this.timeIds = 0;
  111. break;
  112. case 1:
  113. this.timeIds = 1;
  114. break;
  115. case 2:
  116. this.timeIds = 2;
  117. break;
  118. default:
  119. this.timeIds = 0;
  120. break;
  121. }
  122. },
  123. /*选择的时候*/
  124. bindChange(e){
  125. const val = e.detail.value
  126. this.newyear=this.years[val[0]]
  127. this.newmonth= this.months[val[1]]
  128. this.newday= this.days[val[2]]
  129. this.dayfull(this.years[val[0]],this.months[val[1]],this.days[val[2]])
  130. },
  131. /*选择的时候*/
  132. /*点击确定*/
  133. oktime(e){
  134. let that = this;
  135. let year = this.newyear;//选中的年
  136. let month = this.newmonth;//选中的月
  137. let day = this.newday;
  138. switch (this.timeIds){
  139. case 0:
  140. this.$emit("monthTime",{
  141. year:year,
  142. month:'',
  143. day:''
  144. })
  145. break;
  146. case 1:
  147. this.$emit("monthTime",{
  148. year:year,
  149. month:month,
  150. day:''
  151. })
  152. break;
  153. default:
  154. this.$emit("monthTime",{
  155. year:year,
  156. month:month,
  157. day:day
  158. })
  159. break;
  160. }
  161. this.$emit("isTimeHide")
  162. },
  163. /*点击确定*/
  164. closeTime(){
  165. this.$emit("isTimeHide")
  166. },
  167. }
  168. }
  169. </script>
  170. <style>
  171. .columns{
  172. display: flex;
  173. align-items: center;
  174. justify-content: center;
  175. font-size: 333rpx;
  176. font-size: 32rpx;
  177. font-weight: bold;
  178. }
  179. .timeScroll{
  180. width: 100%;
  181. height: 260rpx;
  182. margin-bottom:70rpx;
  183. }
  184. .activeTime{
  185. background: #B1CB30;
  186. color: #fff!important;
  187. border: 1px solid #B1CB30!important;
  188. }
  189. .daytime{
  190. border-left: none!important;
  191. }
  192. .timeCons:nth-child(1){
  193. border-top-left-radius: 10rpx;
  194. border-bottom-left-radius: 10rpx;
  195. border-right: none!important;
  196. }
  197. .timeCons:last-child{
  198. border-top-right-radius: 10rpx;
  199. border-bottom-right-radius: 10rpx;
  200. }
  201. .timeCons{
  202. width: 148rpx;
  203. height: 66rpx;
  204. display: flex;
  205. align-items: center;
  206. justify-content: center;
  207. color: #333;
  208. font-size: 28rpx;
  209. font-weight: bold;
  210. border:1px solid #aaa;
  211. }
  212. .timeCon{
  213. width: 100%;
  214. height: 146rpx;
  215. display: flex;
  216. align-items: center;
  217. justify-content: center;
  218. }
  219. .timeSuccess{
  220. color: #B1CB30;
  221. font-size: 32rpx;
  222. margin-left: auto;
  223. }
  224. .timeSelese{
  225. color: #333333;
  226. font-size: 36rpx;
  227. font-weight: bold;
  228. display: block;
  229. margin: 0 auto;
  230. margin-left: 201rpx;
  231. }
  232. .timeClose{
  233. font-size: 32rpx;
  234. color: #666666;
  235. }
  236. .timetile{
  237. width: 690rpx;
  238. height: 98rpx;
  239. margin: 0 auto;
  240. border-bottom:1px solid #ececec;
  241. display: flex;
  242. align-items: center;
  243. }
  244. .timecontent{
  245. width: 100%;
  246. background: #fff;
  247. z-index: 99;
  248. position: absolute;
  249. left: 0;
  250. bottom: 0;
  251. min-height: 200rpx;
  252. }
  253. .timezhe{
  254. width: 100%;
  255. height: 100%;
  256. background: #000;
  257. opacity: 0.6;
  258. position: absolute;
  259. left: 0;
  260. top: 0;
  261. z-index: 9;
  262. }
  263. .times{
  264. width: 100%;
  265. height: 100vh;
  266. position: fixed;
  267. left: 0;
  268. top: 0;
  269. z-index: 999999999999;
  270. }
  271. </style>