微信小程序实现日历功能

发布时间:2020-09-27 08:35:48 作者:前端阿月
来源:脚本之家 阅读:291

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

效果图:

微信小程序实现日历功能

微信小程序实现日历功能

代码:

<view class="calendar">
 <view class="selectDate">
 <view class="goleft iconfont icon-jianzuo" bindtap="prevMonth"></view>
 <view class="date-wrap">
  {{year}}年{{month}}月
 </view>
 <view class="goright iconfont icon-jianzuo" bindtap="nextMonth"></view>
 </view>
 <view class="week">
 <view wx:for="{{weekArr}}" wx:for-index="index" wx:for-item="item" wx:key="key" >{{item}}</view>
 </view>
 <view class="date" style='width: {{ param * 7 }}px;'>
 <block wx:for="{{dateArr}}" wx:for-index="index" wx:for-item="item" wx:key="key">
  <view  class="{{index+1==day?'today':''}} {{index+1==day&&isClock?'clockOn':''}}"><view class="day">{{item}}</view></view>
 </block>
 </view>
</view><!--end calendar-->
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,
 })
 },

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

推荐阅读:
  1. 微信小程序如何实现签字功能
  2. 微信小程序如何实现搜索功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

微信小程序 日历 信小

上一篇:vue子传父关于.sync与$emit的实现

下一篇:Spring Boot 配置和使用多线程池的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》