微信小程序时间弹窗——年月日时分

需求

  • 1、默认当前时间
  • 2、选择时间弹窗限制最大值、最小值
  • 3、每次弹起更新最大值为当前时间,默认值为上次选中时间
  • 4、== minDate: new Date(2023, 10, 1).getTime(),也可以传入时间字符串new Date('2023-10-1 12:22').getTime() ==

html

bash 复制代码
  	 <view class="flex bb ptb-12">
        <view><text class="red">* </text>处理时间:</view>
        <view class="flex1 size-28" bindtap="chooseTime">
          <view class="mr-8">{{ququ2?ququ2:'请选择'}}</view>
          <van-icon name="arrow" />
        </view>
     </view>
     
  <!-- 弹窗 -->
  <van-popup show="{{ isShowPop }}" bind:close="onClosePop" position="bottom">
    <van-datetime-picker title="处理时间" formatter="{{formatter}}" value="{{ currentDate  }}" bind:confirm="confirmPop" bind:cancel="onClosePop" min-date="{{minDate}}" max-date="{{maxDate}}" />
  </van-popup>

data

js 复制代码
  	ququ2: '',
    isShowPop: false,
    currentDate: new Date().getTime(),
    minDate: new Date(2023, 10, 1).getTime(), //也可以传入时间字符串new Date('2023-10-1 12:22').getTime()
    maxDate: new Date().getTime(),
    formatter: function (type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      } else if (type === 'day') {
        return `${value}日`;
      } else if (type === 'hour') {
        return `${value}时`;
      } else if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },

方法

js 复制代码
// -----------选择时间弹窗---------
  chooseTime() {
    this.setData({
      maxDate: new Date().getTime(),
      // new Date('2023-10-1 12:22').getTime() 回显当前选中的时间,否则显示当前时间
      currentDate: this.data.ququ2 ? new Date(this.data.ququ2).getTime() : new Date().getTime(),
      isShowPop: true
    })
  },
  confirmPop(e) {
    // console.log(e, 'e', this.formatTimestamp(e.detail))
    this.setData({
      ququ2: this.formatTimestamp(e.detail),
      isShowPop: false
    })
  },
  onClosePop() {
    this.setData({
      isShowPop: false
    })
  },
  // 选中的时间戳处理成  2014-12-23 12:11 格式
  formatTimestamp(timestamp) {
    var date = new Date(timestamp);
    var year = date.getFullYear();
    var month = date.getMonth() + 1; // 月份是从0开始的,所以需要加1
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();

    // 格式化时间为字符串
    // 确保月和日是两位数
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    hour = hour < 10 ? '0' + hour : hour;
    minute = minute < 10 ? '0' + minute : minute;

    return `${year}-${month}-${day} ${hour}:${minute}`;
  },
相关推荐
icebreaker13 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker13 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者3 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround4 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround4 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
吴声子夜歌5 天前
小程序——布局示例
小程序
码云数智-大飞5 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54595 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟5 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
攀登的牵牛花5 天前
给女朋友写了个轻断食小程序:去老丈人家也是先动筷了
前端·微信小程序