微信小程序实现日历功能

(编辑:jimmy 日期: 2024/11/19 浏览:2)

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

效果图:

微信小程序实现日历功能

微信小程序实现日历功能

代码:

<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" class="{{index+1==day"><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,
 })
 },

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

一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?