微信小程序TDesign封装的时间选择器,可以选中今天、昨天、上周、上月等功能

微信小程序TDesign封装的日期选择器

可以选中今天、昨天、上周、上月等功能。
确定返回日期时,会判断时间戳大小,返回createdTime,endTime
组件图片展示

![在这里插入图片描述](https://img-blog.csdnimg.cn/127042c3c0c3497eb60fbd584592df77.png

父组件使用组件

//json中引入
{
...
  "usingComponents": {
    ...
    //此处根据自己的路径
    "select-time": "../../components/select-time/index",
    '''
  }
}


//wxml中使用
<select-time status="{{selectStatus}}" bind:close="closeSelect" bind:confirm="confirmSelect" />


//js中的事件等
  closeSelect() {
    this.setData({
      selectStatus: false
    })
  },
  confirmSelect(data) {
      console.log(data.detail.createdTime,data.detail.endTime)
  },

以下为子组件代码

组件wxml

<t-date-time-picker header="{{false}}" title="选择日期" visible="{{status}}" mode="date" defaultValue="{{today}}" value="{{valueTime}}" format="YYYY-MM-DD" bindchange="onConfirm" bindpick="onColumnChange" bindcancel="hidePicker" start="2022-11-01" end="{{today}}">
  <view slot="header">
    <view class="select-time-box">
      <view class="flex items jcsb">
        <view catchtap="onVisibleChange">取消</view>
        <view>选择时间</view>
        <view catchtap="confirm" style="color: #EF7B36;">确定</view>
      </view>
      <view class="flex items" style="margin-top: 60rpx;">
        <view catchtap="selectToday" class="{{btn_state === 1?'btn-active':'btn'}}">今天</view>
        <view catchtap="selectYesterday" class="{{btn_state === 2?'btn-active':'btn'}}">昨天</view>
        <view catchtap="lastWeek" class="{{btn_state === 3?'btn-active':'btn'}}">上周</view>
        <view catchtap="lastMonth" class="{{btn_state === 4?'btn-active':'btn'}}">上月</view>
      </view>
      <view class="flex jcsb items" style="margin-top: 100rpx;">
        <view class="flex1 textc flex jcc">
          <input type="text" catchtap="select1" value="{{createdTime}}" class="{{selectState === 1?'border_bottom':'border_none'}}" disabled placeholder="点击选择开始时间" style="width:170rpx" />
        </view>
        <view style="color: #B4B4B4;">——</view>
        <view class="flex1 textc flex jcc">
          <input type="text" catchtap="select2" value="{{endTime}}" class="{{selectState === 2?'border_bottom':'border_none'}}" disabled placeholder="点击选择开始时间" style="width:170rpx" />
        </view>
      </view>
    </view>
  </view>
</t-date-time-picker>

组件wxss


.select-time-box {
  padding: 32rpx 32rpx;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
  background-color: #ffffff;
  border-radius: 32rpx 32rpx 0 0;
  font-size: 28rpx;
  color: #2E2606;
}

.btn-active {
  border: 1px solid #FEC50A;
  height: 60rpx;
  width: 100rpx;
  font-size: 24rpx;
  margin-right: 24rpx;
  text-align: center;
  background-color: #FEC50A;
  color: #2E2606;
  font-weight: 700;
  line-height: 60rpx;
}

.btn {

  border: 1px solid #D9D9D9;
  height: 60rpx;
  width: 100rpx;
  font-size: 24rpx;
  margin-right: 24rpx;
  text-align: center;
  color: #999999;
  line-height: 60rpx;
}


.border_bottom {
  border-bottom: 2px solid #FFE284;
}
.border_none {
  border-bottom: 2px solid transparent;
}

.flex {
  display: flex !important;
}

.jcsb {
  justify-content: space-between !important;
}

.jcc {
  justify-content: center !important;
}

.items {
  align-items: center !important;
}

.flex-col {
  flex-direction: column !important;
}

.flex1 {
  flex: 1 !important;
}

.fwrap {
  flex-wrap: wrap !important;
}

.textc {
  text-align: center !important;
}

.textl {
  text-align: left !important;
}

.textr {
  text-align: right !important;
}
.f36{
  font-size: 36rpx;
}
.f34{
  font-size: 34rpx;
}
.f32{
  font-size: 32rpx;
}
.f30{
  font-size: 30rpx;
}
.f28 {
  font-size: 28rpx;
}
.f26 {
  font-size: 26rpx;
}
.f24{
  font-size: 24rpx;
}
.f22 {
  font-size: 22rpx;
}

.f20 {
  font-size: 20rpx;
}

.hidden {
  overflow: hidden !important;
}
.f700{
  font-weight: 700 !important;
}
.wfull {
  width: 100% !important;
}

.hfull {
  height: 100% !important;
}

.posr {
  position: relative !important;
}
input{
  height: 24rpx;
}

组件JSON

{
  "component": true,
  "usingComponents": {
    "t-popup": "tdesign-miniprogram/popup/popup",
    "t-date-time-picker": "tdesign-miniprogram/date-time-picker/date-time-picker",
    "t-picker": "tdesign-miniprogram/picker/picker",
    "t-picker-item": "tdesign-miniprogram/picker/picker-item"
  }
}

组件js

// components/select-time/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    status: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    btn_state: 0,
    today: '',
    selectState: 1,
    createdTime: '',
    endTime: '',
    valueTime: '',
  },

  /**
   * 组件的方法列表
   */
  ready() {
    const time = this.timestampToTime(new Date())
    this.setData({
      today: `${time.Y}-${time.M}-${time.D}`,
      valueTime: `${time.Y}-${time.M}-${time.D}`,
      createdTime: `${time.Y}-${time.M}-${time.D}`,
      endTime: `${time.Y}-${time.M}-${time.D}`
    })
  },
  methods: {
    onColumnChange(e) {
      if (this.data.selectState === 1) {
        this.setData({
          createdTime: e.detail.value,
          valueTime: e.detail.value,
          btn_state: 0
        })
      } else {
        this.setData({
          endTime: e.detail.value,
          valueTime: e.detail.value,
          btn_state: 0
        })
      }
    },
    select1() {
      this.setData({
        selectState: 1
      })
    },
    select2() {
      this.setData({
        selectState: 2
      })
    },
    timestampToTime(timestamp) {
      const date = new Date(timestamp)
      const Y = date.getFullYear()
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())
      const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
      const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      return {
        Y,
        M,
        D,
        h,
        m,
        s
      }
    },
    onVisibleChange() {
      this.triggerEvent('close')
    },
    confirm() {
      const createdTime = new Date(this.data.createdTime).getTime() <= new Date(this.data.endTime).getTime() ? this.data.createdTime : this.data.endTime
      const endTime = new Date(this.data.endTime).getTime() > new Date(this.data.createdTime).getTime() ? this.data.endTime : this.data.createdTime
      this.triggerEvent('confirm', {
        createdTime,
        endTime
      })
    },
    selectToday() {
      this.setData({
        valueTime: this.data.today,
        createdTime: this.data.today,
        endTime: this.data.today,
        btn_state: 1,
      })
    },
    selectYesterday() {
      const time = this.timestampToTime((new Date().getTime() - 86400000))
      this.setData({
        valueTime: `${time.Y}-${time.M}-${time.D}`,
        createdTime: `${time.Y}-${time.M}-${time.D}`,
        endTime: `${time.Y}-${time.M}-${time.D}`,
        btn_state: 2,
      })
    },
    lastWeek() {
      const created = this.timestampToTime(this.getBeginLastWeek())
      const end = this.timestampToTime(this.getEndLastWeek())
      this.setData({
        valueTime: this.data.selectState ? `${created.Y}-${created.M}-${created.D}` : `${end.Y}-${end.M}-${end.D}`,
        createdTime: `${created.Y}-${created.M}-${created.D}`,
        endTime: `${end.Y}-${end.M}-${end.D}`,
        btn_state: 3,
      })
    },
    lastMonth() {
      const created = this.timestampToTime(this.getBeginLastMonth())
      const end = this.timestampToTime(this.getEndLastMonth())
      this.setData({
        valueTime: this.data.selectState ? `${created.Y}-${created.M}-${created.D}` : `${end.Y}-${end.M}-${end.D}`,
        createdTime: `${created.Y}-${created.M}-${created.D}`,
        endTime: `${end.Y}-${end.M}-${end.D}`,
        btn_state: 4,
      })
    },
    getBeginLastWeek() {
      var currentDate = this.getCurrentDate();
      var first = currentDate.getDate() - currentDate.getDay() - 6;
      var startDate = new Date(currentDate.setDate(first));
      return this.startTime(startDate);
    },
    getEndLastWeek() {
      var currentDate = this.getCurrentDate();
      var first = currentDate.getDate() - currentDate.getDay() - 6;
      var last = first + 6;
      var endDate = new Date(currentDate.setDate(last));
      return this.endTime(endDate);
    },
    startTime(time) {
      const nowTimeDate = new Date(time)
      return nowTimeDate.setHours(0, 0, 0, 0)
    },
    endTime(time) {
      const nowTimeDate = new Date(time)
      return nowTimeDate.setHours(23, 59, 59, 999)
    },
    getCurrentDate() {
      return new Date();
    },
    getBeginLastMonth() {
      //获取当前时间
      var currentDate = this.getCurrentDate();
      //获得当前月份0-11
      var currentMonth = currentDate.getMonth();
      //获得当前年份4位年
      var currentYear = currentDate.getFullYear();
      //获得上一个月的第一天
      var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
      return priorMonthFirstDay;
    },
    getEndLastMonth() {
      //获取当前时间
      var currentDate = this.getCurrentDate();
      //获得当前月份0-11
      var currentMonth = currentDate.getMonth();
      //获得当前年份4位年
      var currentYear = currentDate.getFullYear();

      //当为12月的时候年份需要加1
      //月份需要更新为0 也就是下一年的第一个月
      if (currentMonth == 11) {
        currentYear++;
        currentMonth = 0; //就为
      } else {
        //否则只是月份增加,以便求的下一月的第一天
        currentMonth++;
      }

      //一天的毫秒数
      var millisecond = 1000 * 60 * 60 * 24;
      //求出上月的最后一天
      var lastDay = new Date(this.getBeginMonth().getTime() - millisecond);

      return this.endTime(lastDay);
    },
    getBeginMonth() {
      var currentDate = this.getCurrentDate();
      var currentMonth = currentDate.getMonth();
      //获得当前年份4位年
      var currentYear = currentDate.getFullYear();
      //求出本月第一天
      var firstDay = new Date(currentYear, currentMonth, 1);

      return firstDay;
    },
    getPriorMonthFirstDay(year, month) {
      //年份为0代表,是本年的第一月,所以不能减
      if (month == 0) {
        month = 11; //月份为上年的最后月份
        year--; //年份减1
        return new Date(year, month, 1);
      }
      //否则,只减去月份
      month--;
      return new Date(year, month, 1);;
    }

  }
})
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序tdesign-零售电商模板是一款专为零售电商行业设计的开源模板,它提供了丰富的功能和界面设计,以帮助开发者快速构建零售电商类的小程序应用。 该模板的主要特点包括以下几个方面: 1. 完善的商品展示和管理:模板提供了完善的商品展示和管理功能,包括商品分类、商品详情、商品列表、购物车等。开发者可以根据自己的需求,灵活使用这些功能来展示和管理自己的商品。 2. 灵活的购物流程:模板提供了灵活的购物流程设计,包括商品加购、修改数量、选择规格、下单支付等功能。用户可以方便地浏览和购买商品,提升购物体验。 3. 多种营销工具:模板内置了多种营销工具,如优惠券、满减活动、积分、折扣等。开发者可以根据自己的需求,选择合适的营销工具来促进销售和提升用户转化率。 4. 友好的用户界面设计:模板采用了现代化的界面设计风格,用户界面美观、简洁,并且易于操作。这有助于提升用户体验,提高用户对小程序的持续使用。 5. 功能扩展与定制化:开发者可以根据自己的需求进行功能扩展和定制化操作。模板提供了丰富的组件和接口,开发者可以根据自己的实际需要进行灵活的定制和开发。 总的来说,微信小程序tdesign-零售电商模板是一个功能丰富、界面美观的开源模板,通过使用这个模板,开发者可以快速搭建自己的零售电商类小程序应用,并提供良好的用户体验和功能支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值