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

需求

  • 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}`;
  },
相关推荐
spmcor1 天前
微信小程序 setStorageSync 踩坑实录:别让"顺手一存"变成"隐形炸弹"
微信小程序
用户4324281061142 天前
小程序埋点设计规范:如何设计可扩展的数据采集体系
微信小程序
m0_526119402 天前
iconfont我修改好颜色,但是在小程序项目是黑色的
小程序
2601_956743682 天前
2026 上海小程序开发甄选:源码、云函数、跨端兼容技术评判
小程序·开发经验·上海
IT_张三2 天前
CSDN-项目分享-暑期备考小程序
小程序
IsJunJianXin3 天前
pdd小程序 cdp 保存响应体
linux·服务器·小程序·pdd小程序·拼多多响应体解密·小程序cdp·拼多多rpc取响应体
Geek_Vison3 天前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
weikecms3 天前
聚合返利CPS小程序快速搭建教程
人工智能·微信·小程序
Haibakeji3 天前
长沙餐饮门店点餐配送小程序定制开发
大数据·小程序
2501_915918413 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview