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


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 相关文章推荐
主页面中的两个iframe实现鼠标拖动改变其大小
Apr 16 Javascript
JQuery的Ajax请求实现局部刷新的简单实例
Feb 11 Javascript
Node.js文件操作详解
Aug 16 Javascript
如何在node的express中使用socket.io
Dec 15 Javascript
Bootstrap Modal遮罩弹出层代码分享
Nov 21 Javascript
JavaScript原生编写《飞机大战坦克》游戏完整实例
Jan 04 Javascript
js return返回多个值,通过对象的属性访问方法
Feb 21 Javascript
浅析Vue实例以及生命周期
Aug 14 Javascript
浅谈React之状态(State)
Sep 19 Javascript
js实现移动端轮播图
Dec 21 Javascript
vue 组件间的通信之子组件向父组件传值的方式
Jul 29 Javascript
解决Vue-cli无法编译es6的问题
Oct 30 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使用CURL实现下载文件功能示例
2019/06/03 PHP
自己开发Dojo的建议框架
2008/09/24 Javascript
关于flash遮盖div浮动层的解决方法
2010/07/17 Javascript
JavaScript使用过程中需要注意的地方和一些基本语法
2010/08/26 Javascript
验证码按回车不变解决方法
2013/03/29 Javascript
jQuery trigger()方法用法介绍
2015/01/13 Javascript
深入理解JavaScript中为什么string可以拥有方法
2016/05/24 Javascript
Javascript 5种方法实现过滤删除前后所有空格
2016/06/22 Javascript
详解angular element()方法使用
2017/04/08 Javascript
AngularJS封装$http.post()实例详解
2017/05/06 Javascript
JS设计模式之惰性模式(二)
2017/09/29 Javascript
Vue 使用formData方式向后台发送数据的实现
2019/04/14 Javascript
axios实现简单文件上传功能
2019/09/25 Javascript
JavaScript中的null和undefined用法解析
2019/09/30 Javascript
nodejs中使用archive压缩文件的实现代码
2019/11/26 NodeJs
antd table按表格里的日期去排序操作
2020/11/17 Javascript
[46:57]EG vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
深入讲解Python中面向对象编程的相关知识
2015/05/25 Python
Python实现将目录中TXT合并成一个大TXT文件的方法
2015/07/15 Python
用Django写天气预报查询网站
2018/10/21 Python
OpenCV搞定腾讯滑块验证码的实现代码
2019/05/18 Python
python实现的Iou与Giou代码
2020/01/18 Python
使用CSS3制作一个简单的Chrome模拟器
2015/07/15 HTML / CSS
将时尚融入珠宝:Adornmonde
2019/10/17 全球购物
Contém1g官网:巴西彩妆品牌
2020/01/17 全球购物
写好求职应聘自荐信的三部曲
2013/09/21 职场文书
应用化学专业本科生求职信
2013/09/29 职场文书
会计电算化专业个人的自我评价
2013/11/24 职场文书
班组长安全生产职责
2013/12/16 职场文书
应届毕业生个人求职信范文
2014/01/29 职场文书
生日主持词
2014/03/20 职场文书
给老婆的检讨书
2015/01/27 职场文书
会议主持词开场白
2015/05/28 职场文书
泰坦尼克号观后感
2015/06/04 职场文书
《百分数的认识》教学反思
2016/02/19 职场文书
2016年学校“3.12”植树节活动总结
2016/03/16 职场文书