当前位置: 代码网 > it编程>前端脚本>Vue.js > 关于vant的时间选择器使用方式

关于vant的时间选择器使用方式

2024年05月18日 Vue.js 我要评论
vant的时间选择器<van-popup :show="showpop" position="bottom" label="有效日期" custom-st

vant的时间选择器

<van-popup
      :show="showpop"
      position="bottom"
      label="有效日期"
      custom-style="height: 50%;"
      @close="oncancel"
    >
      <view v-if="showtwotime">
        <van-datetime-picker
          type="date"
          :value="currentdate"
          @confirm="confirm1"
          @cancel="oncancel"
          :min-date="mindate"
          :formatter="formatter"
        />
      </view>
      <view v-if="!showtwotime">
        <van-datetime-picker
          type="date"
          :value="currentdate"
          @confirm="confirm2"
          @cancel="oncancel"
          :min-date="mindate"
          :formatter="formatter"
        /> </view
    ></van-popup>

这里需要开始时间和结束时间:

  • 提示:因此增加了showtwotime的判定:

解决方案

  • 提示:这里是时间转换的方法:
 confirm1(value) {
      this.plan.start_time = this.formattime(value.detail, 'y/m/d')
      this.showtwotime = false
    },
    confirm2(value) {
      this.showpop = false
      this.plan.end_time = this.formattime(value.detail, 'y/m/d')
      this.showtwotime = true
    },
    formattime(date) {
      date = new date(date)
      console.log(date)
      var year = date.getfullyear()
      var month = date.getmonth() + 1
      var day = date.getdate()
      return [year, month, day].map(this.formatnumber).join('/')
    },
    formatnumber(n) {
      n = n.tostring()
      return n[1] ? n : '0' + n
    },

解决方案

  • 提示:全部方法:
  <van-popup
      :show="showpop"
      position="bottom"
      label="有效日期"
      custom-style="height: 50%;"
      @close="oncancel"
    >
      <view v-if="showtwotime">
        <van-datetime-picker
          type="date"
          :value="currentdate"
          @confirm="confirm1"
          @cancel="oncancel"
          :min-date="mindate"
          :formatter="formatter"
        />
      </view>
      <view v-if="!showtwotime">
        <van-datetime-picker
          type="date"
          :value="currentdate"
          @confirm="confirm2"
          @cancel="oncancel"
          :min-date="mindate"
          :formatter="formatter"
        /> </view
    ></van-popup>
    //data的定义
      showpop: false,
      currentdate: new date().gettime(),
      mindate: new date().gettime(),
      showtwotime: true,
     
   //方法的定义
   changefn() {
      this.changedate = this.currentdate
    },
    confirm1(value) {
      this.plan.start_time = this.formattime(value.detail, 'y/m/d')  ///'y/m/d'为了提示自己时间格式
      this.showtwotime = false
    },
    confirm2(value) {
      this.showpop = false
      this.plan.end_time = this.formattime(value.detail, 'y/m/d')
      this.showtwotime = true
    },
    formattime(date) {
      date = new date(date)  //从时间选择器中得到的时间格式为时间搓,因此需要转换为标准制式时间单位
      console.log(date)  
      var year = date.getfullyear()
      var month = date.getmonth() + 1
      var day = date.getdate() //这里只表现到日,时,分,秒自习行添加方法!
      return [year, month, day].map(this.formatnumber).join('/') //转换为产品经理想要的展示形式
    },
    formatnumber(n) {
      n = n.tostring()
      return n[1] ? n : '0' + n //加0操作!
    },
      formatter(type, value) {  //展示的格式处理
        if (type === 'year') {
          return `${value}年`
        }
        if (type === 'month') {
          return `${value}月`
        }
        if (type === 'day') {
          return `${value}日`
        }
        return value
      },

展示效果

悬着效果

页面效果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com