当前位置: 首页 > 编程笔记 >

微信小程序日历/日期选择插件使用方法详解

司马萧迟
2023-03-14
本文向大家介绍微信小程序日历/日期选择插件使用方法详解,包括了微信小程序日历/日期选择插件使用方法详解的使用技巧和注意事项,需要的朋友参考一下

微信小程序日历选择器插件点击日历日期可以获取到年月日,具体内容如下

wxml

<view class="canlendarBgView">
 <view class="canlendarView">
  <view class="canlendarTopView">
   <view class="leftBgView" bindtap="handleCalendar" data-handle="prev">
    <view class="leftView">《</view>
   </view>
   <view class="centerView">{{cur_year || "--"}} 年 {{cur_month || "--"}} 月</view>
   <view class="rightBgView" bindtap="handleCalendar" data-handle="next">
    <view class="rightView">》</view>
   </view>
  </view>
  <view class="weekBgView">
   <view class="weekView" wx:for="{{weeks_ch}}" wx:key="{{index}}" data-idx="{{index}}">{{item}}</view>
  </view>
  <view class="dateBgView">
   <view wx:if="{{hasEmptyGrid}}" class="dateEmptyView" wx:for="{{empytGrids}}" wx:key="{{index}}" data-idx="{{index}}">
   </view>
   <view class="dateView" wx:for="{{days}}" wx:key="{{index}}" data-idx="{{index}}" bindtap="dateSelectAction">
    <view class="datesView {{index == todayIndex ? 'dateSelectView' : ''}}">{{item}}</view>
   </view>
  </view>
 </view>
 <view>点击日期选择</view>
</view>

js

//index.js
//获取应用实例
Page({
 data: {
  hasEmptyGrid: false,
  cur_year: '',
  cur_month: '',
 },
 onLoad(options) {
  this.setNowDate();
 },
 
 dateSelectAction: function (e) {
  var cur_day = e.currentTarget.dataset.idx;
  this.setData({
   todayIndex: cur_day
  })
  console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day + 1}日`);
 },
 
 setNowDate: function () {
  const date = new Date();
  const cur_year = date.getFullYear();
  const cur_month = date.getMonth() + 1;
  const todayIndex = date.getDate() - 1;
  console.log(`日期:${todayIndex}`)
  const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
  this.calculateEmptyGrids(cur_year, cur_month);
  this.calculateDays(cur_year, cur_month);
  this.setData({
   cur_year: cur_year,
   cur_month: cur_month,
   weeks_ch,
   todayIndex,
  })
 },
 
 getThisMonthDays(year, month) {
  return new Date(year, month, 0).getDate();
 },
 getFirstDayOfWeek(year, month) {
  return new Date(Date.UTC(year, month - 1, 1)).getDay();
 },
 calculateEmptyGrids(year, month) {
  const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
  let empytGrids = [];
  if (firstDayOfWeek > 0) {
   for (let i = 0; i < firstDayOfWeek; i++) {
    empytGrids.push(i);
   }
   this.setData({
    hasEmptyGrid: true,
    empytGrids
   });
  } else {
   this.setData({
    hasEmptyGrid: false,
    empytGrids: []
   });
  }
 },
 calculateDays(year, month) {
  let days = [];
 
  const thisMonthDays = this.getThisMonthDays(year, month);
 
  for (let i = 1; i <= thisMonthDays; i++) {
   days.push(i);
  }
 
  this.setData({
   days
  });
 },
 handleCalendar(e) {
  const handle = e.currentTarget.dataset.handle;
  const cur_year = this.data.cur_year;
  const cur_month = this.data.cur_month;
  if (handle === 'prev') {
   let newMonth = cur_month - 1;
   let newYear = cur_year;
   if (newMonth < 1) {
    newYear = cur_year - 1;
    newMonth = 12;
   }
 
   this.calculateDays(newYear, newMonth);
   this.calculateEmptyGrids(newYear, newMonth);
 
   this.setData({
    cur_year: newYear,
    cur_month: newMonth
   })
 
  } else {
   let newMonth = cur_month + 1;
   let newYear = cur_year;
   if (newMonth > 12) {
    newYear = cur_year + 1;
    newMonth = 1;
   }
 
   this.calculateDays(newYear, newMonth);
   this.calculateEmptyGrids(newYear, newMonth);
 
   this.setData({
    cur_year: newYear,
    cur_month: newMonth
   })
  }
 }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍微信小程序日历组件使用方法详解,包括了微信小程序日历组件使用方法详解的使用技巧和注意事项,需要的朋友参考一下 这个日历采用小程序组件化开发,有兴趣的同学可以引用本组件(怎么引用不多赘述,自行去微信小程序开发api了解) wxml js wxss 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍微信小程序时间选择插件使用详解,包括了微信小程序时间选择插件使用详解的使用技巧和注意事项,需要的朋友参考一下 微信小程序时间选择插件-弹出选择,供大家参考,具体内容如下 wxml js: css: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍微信小程序可滑动月日历组件使用详解,包括了微信小程序可滑动月日历组件使用详解的使用技巧和注意事项,需要的朋友参考一下 微信小程序可滑动月日历组件 此日历可进行左右滑动,展示签到打卡信息,和大家分享一下。 如果样式变形,请检查是否有共用样式起冲突 展示一下效果图 在components组件文件夹下新建calendarMonth文件夹 直接上代码吧: index.wxml index.w

  • 本文向大家介绍微信小程序日历效果,包括了微信小程序日历效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了微信小程序日历效果的具体代码,供大家参考,具体内容如下 源码下载地址 项目需要一个日历功能,花了一天时间实现的微信小程序日历,js大小只有几k,引入了bootstrap字体图标(文件icon.wxss,该字体已经bootstrap字体图标需要的字体)看起来有80k,出去icon.

  • 本文向大家介绍微信小程序当前时间时段选择器插件使用方法详解,包括了微信小程序当前时间时段选择器插件使用方法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了微信小程序当前时间时段选择器的实现代码,供大家参考,具体内容如下 DEMO效果图 插件思路 准备工作 获取当前时间,同时获取当前的年、月、日、周几; 创建处理日期数字的函数; 创建格式化日期的函数; 创建获取某月天数的函数; 创

  • 本文向大家介绍微信小程序  滚动选择器(时间日期)详解及实例代码,包括了微信小程序  滚动选择器(时间日期)详解及实例代码的使用技巧和注意事项,需要的朋友参考一下 微信小程序  滚动选择器(时间日期)详解 微信小程序自己封装了很多控件,用起来确实很方便,如果这是Android里面,还需要自己去定义,不废话,效果图: 一起来看看怎么实现的呢?看完你应该就该说,尼玛,这就行啦…. 这个效果呢,要用到p