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

这篇具有很好参考价值的文章主要介绍了微信小程序TDesign封装的时间选择器,可以选中今天、昨天、上周、上月等功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

微信小程序TDesign封装的时间选择器,可以选中今天、昨天、上周、上月等功能,微信小程序,微信小程序,tdesign,小程序文章来源地址https://www.toymoban.com/news/detail-612861.html

父组件使用组件

//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);;
    }

  }
})

到了这里,关于微信小程序TDesign封装的时间选择器,可以选中今天、昨天、上周、上月等功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序--下拉选择框组件封装,可CV直接使用

            接到的项目需求,查看ui设计图后,由于微信小程序官方设计的下拉选择框不符合需求,而且常用的第三方库也没有封装类似的,所以选择自己自定义组件。在此记录一下,方便日后复用。         ui设计图如下:                   微信官方提供的选择框         对比发现

    2024年02月05日
    浏览(39)
  • 微信小程序原生写法——24小时时间选择器组件

    使用picker-view来封装成的一个时间选择器 开始时间是当前时间的一个小时之后,秒默认是0秒 可能还有一些情况未处理,后续发现再更新 js文件 第一版:略繁琐 第二版js文件:根据当前时间的时间戳A与24小时之后的时间戳B两者来进行处理获取对应的列表 json文件 wxml文件 wxs

    2024年02月04日
    浏览(38)
  • 微信小程序封装自定义van-dropdown-item 下拉选择框

    1.vant weapp虽然给我们提供了van-dropdown-item且美观的组件但是没有插槽无法自定义内容,限制了各位大神的操作,接下来我们先来了解他的使用在去封装自己的自定义 这是官方自己的效果! 下面来看看我们自己封装的组件 2.还能自定义搜索框等等是不是很奈斯! 接下来我们在

    2024年02月11日
    浏览(35)
  • 微信小程序 - 日期时间选择器(年月日时分秒)

    您只需要跟着步骤一路复制粘贴,最后看一下使用示例即可。 由于微信官方的 picker 组件不支持同时选择年月日时分, 所以 在此官方组件上再次封装 ,可靠性毋庸置疑。 您将获得一个可选择 年月日时分 / 年月日时分秒 日期选择器组件, 您可以通过一个属性来自由切换要不

    2024年02月10日
    浏览(33)
  • 微信小程序使用TDesign(TS版本)

    1.使用微信小程序的Terminal执行: 2.修改project.config.json  3.修改app.json,移除\\\"style\\\": \\\"v2\\\" 4.编译npm:微信开发者工具的Tools-构建npm 5.在页面下的页面json中导入要使用的组件,在wxml中使用

    2024年02月11日
    浏览(46)
  • 微信小程序中使用 TDesign 自定义 TabBar

    根据 微信官方文档 描述,每个 tab 页下的自定义 tabBar 组件实例是不同的; 如需实现 tab 选中态,要在当前页面下,通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态 在使用到 TabBar 的页面中加入以下代码

    2024年02月08日
    浏览(34)
  • 微信小程序在TS模板下引入TDesign组件

    TDesign 是腾讯官方出品的一款微信小程序组件库。本文介绍如何在新建ts空白模板下引入TDesign库 新建一个空白项目,这里可以选择TS-基础模板 新建项目目录结构如图所示: 注意这里其实小程序的文件都存放在miniprogram文件夹下,因此我们后续安装npm包时需要进入miniprogram文件

    2024年02月03日
    浏览(33)
  • tdesign的文件上传(微信小程序+idea的springboot)

    目录 1. springboot后端 1.1 FileController.java  1.2 listener文件的ErpApplicationListener.java 1.3  【重点!】FileServiceImpl层  1.4 IFileService 1.5 StringUtil通用类  1.6 主程序加一个监听器  1.7 oss是什么和怎么创建(application.yml文件) 2. 微信小程序端 2.1 TDesign的upload组件 1. app.json全局引用一下

    2024年02月03日
    浏览(32)
  • 微信小程序入门学习02-TDesign中的自定义组件

    我们上一篇讲解了TDesign模板的基本用法,如何开始阅读模板。本篇我们讲解一下自定义组件的用法。 官方模板在顶部除了显示图片外,还显示了一段文字介绍。文字是嵌套在容器组件里,先按照他的写法复制代码到我们自己创建的index.wxml文件里 因为我们的布局是从上到下,

    2024年02月10日
    浏览(49)
  • 微信小程序(typescript) npm添加Tdesign UI组件库

    最近,发现一个新的微信小程序UI组件库-TDesign。腾讯自家出品,颜值杠杆。网址如下: https://tdesign.tencent.com/miniprogram/getting-started 使用NPM Node.js 安装包及源码下载地址为:https://nodejs.org/zh-cn/download/ Node.js 菜鸟教程网址:https://www.runoob.com/nodejs/nodejs-install-setup.html 检查npm是否安

    2024年01月16日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包