微信小程序使用picker-view自定义时间选择器

timePick.vue

cpp 复制代码
<template>
  <view class=" ">
    <view class="picker_view">
      <picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange" indicator-class="picker-inner">
        <picker-view-column>
          <view class="item" v-for="(item, index) in years" :key="index"
            :style="{ fontWeight: item === year ? 'bold' : 'normal' }">{{ item
            }}年</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item, index) in months" :key="index"
            :style="{ fontWeight: item === month ? 'bold' : 'normal' }">{{ item }}月</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item, index) in days" :key="index"
            :style="{ fontWeight: item === day ? 'bold' : 'normal' }">{{ item }}日</view>
        </picker-view-column>
      </picker-view>
    </view>
  </view>
</template>

<script>
// {{year}}-{{month}}-{{day}} {{hour}}:{{minute}}
export default {
  name: 'testCom',
  // 开始年份
  props: {
    beginYear: {
      type: [String, Number],
      default() {
        return '2019'
      }
    },
    //结束年份
    endYear: {
      type: [String, Number],
      default() {
        return '2050'
      }
    },

  },
  data() {
    return {
      userSelectDate: '',
      years: [],
      year: 0,
      months: [],
      month: 0,
      days: [],
      day: 0,
      hour: 0,
      hours: [],
      minutes: [],
      minute: 0,
      nowYear: 0,
      value: [0, 0, 0],
      visible: true,
      indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth / (750 / 80))}px; background: rgba(0,0,0,0.03);font-size:16px;font-weight:700 !important;border-radius:4px;`,
      selectArr: [],
      showPicker: true,
      selectRes: ''
    };
  },
  created() {
    // console.log(this.nowYear, this.month, this.day, 'nowYear')
    this.initPicker()
  },

  mounted() {
    this.$nextTick(() => {
      this.value = [this.nowYear, this.month - 1, this.day - 1]
    })
  },

  methods: {
    // 初始化picker
    initPicker() {
      const date = new Date()
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
      this.day = date.getDate()
      const dayCount = new Date(this.year, this.month, 0).getDate()
      const hour = date.getHours()
      const minutes = []
      const minute = date.getMinutes()
      // // 当前年计算  
      this.nowYear = date.getFullYear() - this.beginYear

      for (let i = this.beginYear; i <= this.endYear; i++) {
        this.years.push(i)
      }
      for (let i = 1; i <= 12; i++) {
        this.months.push(i)
      }
      for (let i = 1; i <= dayCount; i++) {
        this.days.push(i)
      }
      for (let i = 1; i <= 24; i++) {
        this.hours.push(i)
      }
      for (let i = 1; i <= 60; i++) {
        this.minutes.push(i)
      }
    },

    // 显示picker
    change() {
      this.showPicker = true;
      // 防止第一次点击无返回值
      this.selectRes = `${this.year + '-' + this.month + '-' + this.day + ' ' + this.hour + ':' + this.minute}`;
    },
    bindChange: function (e) {
      const val = e.detail.value

      // 页面显示数值
      this.year = this.years[val[0]]
      console.log(this.year, 'this.year')
      this.month = this.months[val[1]]
      // 获取当前月份的天数
      let dayCount = new Date(this.year, this.month, 0).getDate()
      let dayArray = []
      for (let i = 1; i <= dayCount; i++) {
        dayArray.push(i)
      }
      this.days = Object.assign([], dayArray)
      this.day = this.days[val[2]]
      // 返回时间
      this.selectRes = `${this.year + '-' + this.month + '-' + this.day}`;
      this.$emit('change', this.selectRes)
    }
  }
}
</script>

<style lang="scss">
.picker-inner {
  font-weight: 700 !important;
  font-size: 16px !important;
}

.box {
  position: relative;
  z-index: 888;
}

.mask {
  position: fixed;
  z-index: 999;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s ease;

  &.show {
    visibility: visible;
    opacity: 1;
  }
}

.picker_view {
  width: 100%;
  height: 200px;
  overflow: hidden;
  background-color: rgba(255, 255, 255, 1);
  z-index: 666;

  picker-view {
    height: 100%;
  }

  .item {
    text-align: center;
    width: 100%;
    line-height: 88upx;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 30upx;
  }
}
</style>
相关推荐
腾马科技4 小时前
新版saas餐饮外卖小程序源码/微信/支付宝/抖音/扫码点餐/DIY装修/美团代付/全开源
微信小程序·小程序·点餐小程序
郑叔敲代码8 小时前
帝国cms 微信小程序的登录逻辑
微信小程序·小程序·notepad++
棋子一名12 小时前
跑马灯组件 Vue2/Vue3/uni-app/微信小程序
微信小程序·小程序·uni-app·vue·js
无衣 秦风13 小时前
vue3+hubuilderX开发微信小程序使用elliptic生成ECDH密钥对遇到的问题
微信小程序·小程序
游戏开发爱好者813 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088913 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
流***陌14 小时前
陪诊就医小程序中健康档案的精细化管理设计方案
小程序
明天你好26714 小时前
如何做一个花店小程序,搭建一个小程序多少钱
微信小程序·小程序·模拟退火算法
2501_9151063215 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
江城开朗的豌豆15 小时前
小程序登录不迷路:一篇文章搞定用户身份验证
前端·javascript·微信小程序