当前位置:  开发笔记 > 编程语言 > 正文

微信小程序实现日历功能

这篇文章主要为大家详细介绍了微信小程序实现日历功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序实现日历功能的具体代码,供大家参考,具体内容如下

效果图:

代码:


 
 
 
  {{year}}年{{month}}月
 
 
 
 
 {{item}}
 
 
 
  {{item}}
 
 

data: {
 year:'',
 month:'',
 day:'',
 weekArr: ['日', '一', '二', '三', '四', '五','六'],
 dateArr:[],
 firstDay:'',
 lastDay:'',
 param:null,
 clockNum:3,
 },
getDate: function () { //获取当月日期
 var mydate = new Date();
 var year = mydate.getFullYear();
 var month = mydate.getMonth();
 var months = month + 1;
 this.data.year = year;
 this.data.month = months;
 this.data.day = mydate.getDate();
 var fist = new Date(year, month, 1);
 this.data.firstDay = fist.getDay();
 var last = new Date(year, months, 0); 
 this.data.lastDay = last.getDate();

 this.setData({
  year: this.data.year,
  month: this.data.month,
  day: this.data.day,
  firstDay: this.data.firstDay,
  lastDay: this.data.lastDay
 })
 console.log("今天:" + this.data.day);
 },
 setDate: function () {
 for (var i = 1; i < this.data.lastDay + 1; i++) {
  this.data.dateArr.push(i);
 }
 this.setData({
  dateArr: this.data.dateArr,
  firstDay: this.data.firstDay
 })
 },
 prevMonth:function(){ //上一月
 var months="";
 var years="";
 if(this.data.month ==1){
  years=this.data.year-1
  this.data.month=12;
  months=this.data.month;
 }else{
  years=this.data.year;
  months = this.data.month - 1;
 }
 
 var first = new Date(years, months-1, 1);
 this.data.firstDay = first.getDay();
 var last = new Date(years, months, 0);
 this.data.lastDay = last.getDate();
 
 this.setData({
  month: months,
  year:years,
  firstDay: this.data.firstDay,
  lastDay: this.data.lastDay
 })

 this.data.dateArr = [];
 for (var i = 1; i < this.data.lastDay + 1; i++) {
  this.data.dateArr.push(i);
 }
 this.setData({
  dateArr: this.data.dateArr
 })
 },
 nextMonth:function(){ //下一月
 var months="";
 var years="";
 if(this.data.month== 12){
  this.data.month=0;
  months = this.data.month;
  years = this.data.year+1;
 }else{
  months = this.data.month+1;
  years = this.data.year;
 }
 var months = this.data.month + 1;
 var first = new Date(years, months-1,1);
 this.data.firstDay= first.getDay();
 var last = new Date(years,months,0);
 this.data.lastDay= last.getDate();
 this.setData({
  month: months,
  year:years,
  firstDay:this.data.firstDay,
  lastDay:this.data.lastDay
 })

 this.data.dateArr = [];
 for (var i = 1; i < this.data.lastDay + 1; i++) {
  this.data.dateArr.push(i);
 }
 this.setData({
  dateArr: this.data.dateArr
 })
 },
onLoad: function (options) {
 this.getDate();
 this.setDate();
 var res = wx.getSystemInfoSync();
 this.setData({
  param:res.windowHeight/12,
 })
 },

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

推荐阅读
jerry613
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有