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


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 相关文章推荐
js 模拟气泡屏保效果代码
Jul 10 Javascript
尝试在让script的type属性等于text/html
Jan 15 Javascript
js取float型小数点后两位数的方法
Jan 18 Javascript
JavaScript中的splice()方法使用详解
Jun 09 Javascript
基于jQuery Ajax实现上传文件
Mar 24 Javascript
JS动态改变浏览器标题的方法
Apr 06 Javascript
最丑的时钟效果!js canvas时钟制作方法
Aug 15 Javascript
jQuery实现菜单栏导航效果
Aug 15 jQuery
js实现单张图片平移切换效果
Oct 11 Javascript
Vue在页面数据渲染完成之后的调用方法
Sep 11 Javascript
vue 使用鼠标滚动加载数据的例子
Oct 31 Javascript
对vue生命周期的深入理解
Dec 03 Vue.js
解决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设计模式中工厂模式详细介绍
2013/05/15 PHP
使用淘宝IP库获取用户ip地理位置
2013/10/27 PHP
PHP实现二叉树的深度优先与广度优先遍历方法
2015/09/28 PHP
详解ThinkPHP3.2.3验证码显示、刷新、校验
2016/12/29 PHP
老生常谈php 正则中的i,m,s,x,e分别表示什么
2017/03/02 PHP
JavaScript 继承详解 第一篇
2009/08/30 Javascript
JQuery中两个ul标签的li互相移动实现方法
2015/05/18 Javascript
js将滚动条滚动到指定位置的简单实现方法
2016/06/25 Javascript
JavaScript正则表达式小结(test|match|search|replace|split|exec)
2016/12/08 Javascript
Jquery Easyui进度条组件Progress使用详解(8)
2020/03/26 Javascript
php结合js实现多条件组合查询
2019/05/28 Javascript
React 全自动数据表格组件——BodeGrid的实现思路
2019/06/12 Javascript
vue项目接口域名动态获取操作
2020/08/13 Javascript
Python中常用信号signal类型实例
2018/01/25 Python
Python爬虫包BeautifulSoup学习实例(五)
2018/06/17 Python
Python设计模式之桥接模式原理与用法实例分析
2019/01/10 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
关于PyTorch 自动求导机制详解
2019/08/18 Python
在Django下测试与调试REST API的方法详解
2019/08/29 Python
Python3 合并二叉树的实现
2019/09/30 Python
关于Python中定制类的比较运算实例
2019/12/19 Python
学习Python列表的基础知识汇总
2020/03/10 Python
jupyter notebook 调用环境中的Keras或者pytorch教程
2020/04/14 Python
Python selenium模块实现定位过程解析
2020/07/09 Python
python实现文件+参数发送request的实例代码
2021/01/05 Python
Nisbets法国:英国最大的厨房和餐饮设备供应商
2019/03/18 全球购物
Java工程师面试集锦之Spring框架
2013/06/16 面试题
建筑安全责任书范本
2014/07/24 职场文书
2014年财政所工作总结
2014/11/22 职场文书
毕业实习指导教师评语
2014/12/31 职场文书
幼师辞职信范文大全
2015/05/12 职场文书
公务员爱岗敬业心得体会
2016/01/25 职场文书
《颐和园》教学反思
2016/02/19 职场文书
用人单位的规章制度,怎样制定才是有效的?
2019/07/09 职场文书
《成长的天空》读后感3篇
2019/12/06 职场文书