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

 更新时间:2024年03月22日 09:30:55   作者:Emotion#  
这篇文章主要介绍了关于vant的时间选择器使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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
      },

展示效果

悬着效果

页面效果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • element-ui表单提交自动清空隐藏表单值实现

    element-ui表单提交自动清空隐藏表单值实现

    这篇文章主要为大家介绍了element-ui表单提交自动清空隐藏表单值实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 使用kbone解决Vue项目同时支持小程序问题

    使用kbone解决Vue项目同时支持小程序问题

    这篇文章主要介绍了使用kbone解决Vue项目同时支持小程序问题,本文通过一个todo的例子跟大家详细介绍,需要的朋友可以参考下
    2019-11-11
  • vue3中使用vuedraggable实现拖拽el-tree数据分组功能

    vue3中使用vuedraggable实现拖拽el-tree数据分组功能

    这篇文章主要介绍了vue3中使用vuedraggable实现拖拽el-tree数据分组功能,可以实现单个拖拽、双击添加、按住ctrl键实现多个添加,或者按住shift键实现范围添加,添加到框中的数据,还能拖拽排序,需要的朋友可以参考下
    2024-02-02
  • 基于vue和bootstrap实现简单留言板功能

    基于vue和bootstrap实现简单留言板功能

    这篇文章主要为大家详细介绍了基于vue和bootstrap实现简单留言板功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • vue 输入电话号码自动按3-4-4分割功能的实现代码

    vue 输入电话号码自动按3-4-4分割功能的实现代码

    这篇文章主要介绍了vue 输入电话号码自动按3-4-4分割功能的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • vue在路由中验证token是否存在的简单实现

    vue在路由中验证token是否存在的简单实现

    今天小编就为大家分享一篇vue在路由中验证token是否存在的简单实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • react+ ts vite搭建及二次封装请求的过程解析

    react+ ts vite搭建及二次封装请求的过程解析

    这篇文章主要介绍了react+ ts vite搭建及二次封装请求,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • vue实现鼠标移过出现下拉二级菜单功能

    vue实现鼠标移过出现下拉二级菜单功能

    这篇文章主要介绍了vue实现鼠标移过出现下拉二级菜单功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • Vue3 全局切换字体大小的实现

    Vue3 全局切换字体大小的实现

    本文主要介绍了Vue3 全局切换字体大小的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-03-03
  • $router.push()中通过path跳转和通过name跳转区别解析

    $router.push()中通过path跳转和通过name跳转区别解析

    今天在路由跳转传参时发现params传参接收到的总是为空,才发现通过path和name传参是有区别的,这篇文章主要介绍了$router.push()中通过path跳转和通过name跳转有什么区别,需要的朋友可以参考下
    2023-11-11

最新评论