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


Posted in Javascript onDecember 28, 2018

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript使用activex控件的代码
Jan 27 Javascript
js中parseInt函数浅谈
Jul 31 Javascript
深入理解JavaScript系列(43):设计模式之状态模式详解
Mar 04 Javascript
jQuery使用after()方法在元素后面添加多项内容的方法
Mar 26 Javascript
JavaScript中join()方法的使用简介
Jun 09 Javascript
jQuery UI设置固定日期选择特效代码分享
Aug 27 Javascript
JS实现自定义简单网页软键盘效果代码
Nov 05 Javascript
微信小程序 石头剪刀布实例代码
Jan 04 Javascript
AngularJS实现的回到顶部指令功能实例
May 17 Javascript
详解Vue 方法与事件处理器
Jun 20 Javascript
JavaScript Date对象应用实例分享
Oct 30 Javascript
VUE.CLI4.0配置多页面入口的实现
Nov 25 Javascript
解决echarts的多个折现数据出现坐标和值对不上的问题
Dec 28 #Javascript
微信小程序时间控件picker view使用详解
Dec 28 #Javascript
微信小程序时间选择插件使用详解
Dec 28 #Javascript
微信小程序当前时间时段选择器插件使用方法详解
Dec 28 #Javascript
Angular6新特性之Angular Material
Dec 28 #Javascript
关于微信公众号开发无法支付的问题解决
Dec 28 #Javascript
小程序实现抽奖动画
Apr 16 #Javascript
You might like
php获取字段名示例分享
2014/03/03 PHP
php实现ip白名单黑名单功能
2015/03/12 PHP
jQuery的初始化与对象构建之浅析
2011/04/12 Javascript
JavaScript将数组转换成CSV格式的方法
2015/03/19 Javascript
jQuery使用$.ajax进行异步刷新的方法(附demo下载)
2015/12/04 Javascript
bootstrap-wysiwyg结合ajax实现图片上传实时刷新功能
2016/05/27 Javascript
Vue.js实现多条件筛选、搜索、排序及分页的表格功能
2020/11/24 Javascript
web.js.字符串与正则表达式操作
2017/05/13 Javascript
AngularJS实现的JSONP跨域访问数据传输功能详解
2017/07/20 Javascript
vue2.5.2使用http请求获取静态json数据的实例代码
2018/02/27 Javascript
mpvue全局引入sass文件的方法步骤
2019/03/06 Javascript
使用taro开发微信小程序遇到的坑总结
2019/04/08 Javascript
Vue.js下拉菜单组件使用方法详解
2019/10/19 Javascript
[03:04]DOTA2超级联赛专访ZSMJ “莫名其妙”的逆袭
2013/05/23 DOTA
[04:59]DOTA2-DPC中国联赛 正赛 Ehome vs iG 选手采访
2021/03/11 DOTA
python简单读取大文件的方法
2016/07/01 Python
Python模拟登陆实现代码
2017/06/14 Python
python+selenium实现登录账户后自动点击的示例
2017/12/22 Python
Python交互环境下实现输入代码
2018/06/22 Python
Python切片操作去除字符串首尾的空格
2019/04/22 Python
Python3实现定时任务的四种方式
2019/06/03 Python
Python循环结构的应用场景详解
2019/07/11 Python
python编写俄罗斯方块
2020/03/13 Python
HTML5进阶段内联标签汇总(小篇)
2016/07/13 HTML / CSS
美国最大的在线水培用品商店:GrowersHouse.com
2018/08/14 全球购物
英国网上购买肉类网站:Great British Meat
2018/10/17 全球购物
澳大利亚领先的亚麻品牌:Bed Threads
2019/12/16 全球购物
PyQt QMainWindow的使用示例
2021/03/24 Python
最新会计专业求职信范文
2014/01/28 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
文艺晚会策划方案
2014/06/11 职场文书
2014年底工作总结
2014/12/15 职场文书
sql通过日期判断年龄函数的示例代码
2021/07/16 SQL Server
windows11怎么查看自己安装的版本号? win11版本号的查看方法
2021/11/21 数码科技
【海涛七七解说】DCG第二周:DK VS 天禄
2022/04/01 DOTA
了解MySQL查询语句执行过程(5大组件)
2022/08/14 MySQL