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

需求

  • 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}`;
  },
相关推荐
Jyywww12132 分钟前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero2 小时前
微信小程序41~50
微信小程序·小程序
走,带你去玩10 小时前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉10 小时前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序
一笑code11 小时前
vue/微信小程序/h5 实现react的boundary
微信小程序·vue·react
菌菇汤12 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
CC同学呀19 小时前
从0到100:单位订餐统计小程序开发日记2025
小程序
666HZ6661 天前
微信小程序中scss、ts、wxml
微信小程序·小程序·scss
二十十十十十1 天前
微信点餐小程序—美食物
微信·小程序
向明天乄1 天前
在小程序中实现实时聊天:WebSocket最佳实践
websocket·网络协议·小程序