vant van-datetime-picker 日期组件设置选择范围当前时间到一年以内

本文介绍了如何在Vue.js项目中使用Vant的日期组件van-datetime-picker,设置选择范围为当前时间到一年以后。通过定义最小时间变量为当前时间,并在计算属性中计算最大时间为当前时间加一年。同时,详细说明了在methods和mounted阶段的相关配置方法,最终实现了预期的效果。
部署运行你感兴趣的模型镜像

直接贴代码

1.调用日期组件

  <van-field v-model="timeValue" @click="showPopFn()" placeholder="请选择到期日" readonly/>
    <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
      <van-datetime-picker 
        v-model="currentDate" 
        type="date"
        :min-date="minDate" 
        :max-date="this.maxDate"
        @change="changeFn()" 
        @confirm="confirmFn()" 
        @cancel="cancelFn()" 
        />
    </van-popup>

2.定义变量,最小时间就是当前时间

data() {
    return {
      value: '',
      timeValue: '',
      currentDate: new Date(),
      changeDate: new Date(),
      minDate: new Date(),
      show: false, // 用来显示弹出层
    };

3. 通过计算属性算出最大时间并返回

computed: {
    
    maxDate(){
       
      let curDate = (new Date()).getTime();
      let one = 365 * 24 * 3600 * 1000;
      let oneYear = curDate + one;
      return new Date(oneYear);
    }
 },

4.method里面写对应的方法

methods: {
   
     
      changeFn() { // 值变化是触发
        this.changeDate = this.currentDate // Tue Sep 08 2020 00:00:00 GMT+0800 (中国标准时间)
      },
      confirmFn() { // 确定按钮
        this.timeValue = this.timeFormat(this.currentDate);
        this.show = false;
      },
      cancelFn(){
        this.show = false;
      },
      
       timeFormat(time) { // 时间格式化 2019-09-08
        let year = time.getFullYear();
        let month = time.getMonth() + 1;
        let day = time.getDate();
        return year + '-' + month + '-' + day
      },
    },
  

 5.mounted

mounted(){
    this.timeFormat(new Date());
}

效果如下图

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ougexingfuba

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值