欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序实现日历功能

程序员文章站 2023-11-12 20:02:46
本文实例为大家分享了微信小程序实现日历功能的具体代码,供大家参考,具体内容如下 效果图: 代码:

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

效果图:

微信小程序实现日历功能

微信小程序实现日历功能

代码:

<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" style="width:{{param}}px;height:{{param-17}}px;line-height:{{param-17}}px">{{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 style="{{index ==0?'margin-left:'+ param *firstday +'px;':''}}width:{{param}}px;height:{{param-10}}px;line-height:{{param-10}}px;" 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,
 })
 },

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