小程序日历

示例图

 

wxml文件

<!--pages/template/calendar/index.wxml-->
<nav-bar navbar-data='{{navbarData}}'></nav-bar>
<view style='padding-top: {{height}}px;padding-bottom: 48px;'>
  <view class="calendar-wrap">
    <view class="date">
      <image class="direction" src="{{imgPath}}public/next@2x.png" style="transform: rotate(180deg)" bindtap='handleCalendar' data-handle="prev"/>
      <label>{{year}}年{{month}}月</label>
      <image class="direction" src="{{imgPath}}public/next@2x.png" bindtap='handleCalendar' data-handle="next" />
    </view>
    <view class="content">
      <view class="header">
        <block wx:for="{{weeks}}" wx:key="index">
          <text class="weeks-item-text">{{item}}</text>
        </block>
      </view>
      <view class="body-days">
        <block wx:for="{{days}}" wx:key="index">
          <view class="days-item {{item.current ? '' : 'no-current'}}" >
            <view class="days-item-text" wx:if="{{item.date}}">{{item.date}}</view>
          </view>
        </block>
      </view>
    </view>
  </view>
</view>

wxss文件

/* pages/template/calendar/index.wxss */
.calendar-wrap {
  width: 690rpx;
  margin: 30rpx auto 0;
  background: #FFFFFF;
  box-shadow: 0 0 16rpx 0 rgba(0,0,0,0.08);
  border-radius: 16rpx;
}
.date {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  line-height: 92.4rpx;
  margin: 0 41rpx;
  border-bottom: 2rpx solid #F5F6F6;
  font-family: 'PingFangSC-Medium';
  font-size: 44rpx;
  color: #434C53;
}

.direction {
  width: 40rpx;
  margin: 10rpx;
  height: 40rpx;
}

.content {
  padding: 20rpx 20rpx 0;
}

.header {
  display: flex;
  flex-direction: row;
  margin-bottom: 20rpx;
}

.weeks-item-text {
  width: 80rpx;
  line-height: 48rpx;
  font-size: 30rpx;
  text-align: center;
}

.header .weeks-item-text:not(:nth-child(7n)) {
  margin-right: 15rpx;
}

.body-days {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  text-align: center;
  padding-bottom: 10rpx;
}

.days-item {
  width: 80rpx;
  height: 48rpx;
  display: flex;
  justify-content: center;
  align-content: center;
  margin-bottom: 15rpx;
  color: #434C53;
}

.days-item.no-current {
  color: #DCDCDC;
}

.days-item:not(:nth-child(7n)) {
  margin-right: 15rpx;
}

.days-item-text {
  font-size: 30rpx;
  text-align: center;
}

js文件

// pages/template/calendar/index.js
const util = wx.base.util
const app = getApp()
Page({
    data: {
        // 组件所需的参数
        navbarData: {
            showCapsule: 0, //是否显示左上角图标 1 表示显示 0 表示不显示
            showHome: 0, // 是否显示左上角的home键 1 显示 0 不显示
            title: '', //导航栏 中间的标题
        },
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        imgPath: app.globalData.imgPath,
        date: [],
        weeks: ['日', '一', '二', '三', '四', '五', '六'],
        days: [],
        year: 0,
        month: 0
    },
    onLoad: function (options) {
        this.dateData()
    },
    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1 //这个数是,tabBar从左到右的下标,从0开始
            })
        }
        this.data.navbarData.title = wx.getStorageSync('account-corpNameShort') || '福利'
        this.setData({
            navbarData: this.data.navbarData
        })
    },
    dateData: function () {
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        this.updateDays(year, month)

    },
    handleCalendar: function (e) {
        const handle = e.currentTarget.dataset.handle;
        const year = this.data.year;
        const month = this.data.month;
        if (handle === 'prev') {
            let newMonth = month - 1;
            let newYear = year;
            if (newMonth < 1) {
                newYear = year - 1;
                newMonth = 12;
            }
            this.updateDays(newYear, newMonth);
            this.setData({
                year: newYear,
                month: newMonth
            })
        } else {
            let newMonth = month + 1;
            let newYear = year;
            if (newMonth > 12) {
                newYear = year + 1;
                newMonth = 1;
            }
            this.updateDays(newYear, newMonth);
            this.setData({
                year: newYear,
                month: newMonth
            })
        }
    },
    updateDays: function (year, month) {
        var days = []
        var dateDay, dateWeek, lastWeek
        dateDay = this.getThisMonthDays(year, month)
        dateWeek = this.getFirstDayOfWeek(year, month)
        lastWeek = this.getLastDayOfWeek(year, month)
        //向数组中添加天
        for (let index = 1; index <= dateDay; index++) {
            days.push({
                date: index,
                isSign: false,
                current: true // 是不是当月的日期
            })
        }
        // 获取本月第一天的上一天是几号
        let time = new Date(year + '-' + month+ '-' + 1).getTime() - 24 * 60 * 60 * 1000;
        let yesterday = new Date(time).getDate()
        //向数组中添加,一号之前应该空出的空格
        for (let index = 1; index <= dateWeek; index++) {
            days.unshift({
                date: yesterday--,
                isSign: false,
                current: false
            })
        }
        //向数组中添加,最后一天之后应该空出的空格
        for (let index = 1; index <= 6 - lastWeek; index++) {
            days.push({
                date: index,
                isSign: false,
                current: false
            })
        }
        this.setData({
            days: days,
            year: year,
            month: month
        })
    },
    // 获取当月共多少天
    getThisMonthDays: function (year, month) {
        return new Date(year, month, 0).getDate()
    },
    // 获取当月第一天星期几
    getFirstDayOfWeek: function (year, month) {
        console.log(year, month)
        let _time = new Date(year + '-' + (month + 1)+ '-' + 1).getTime() - 24 * 60 * 60 * 1000;
        let lastDay = new Date(_time).getDate()
        console.log(lastDay)
        console.log(new Date(Date.UTC(year, month - 1, 29)).getDay())
        return new Date(Date.UTC(year, month - 1, 1)).getDay()
    },
    // 获取当月第一天星期几
    getLastDayOfWeek: function (year, month) {
        let _time = new Date(year + '-' + (month + 1)+ '-' + 1).getTime() - 24 * 60 * 60 * 1000;
        let lastDay = new Date(_time).getDate()
        return new Date(Date.UTC(year, month - 1, lastDay)).getDay()
    }
})

 

转载于:https://www.cnblogs.com/memphis-f/p/11063893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值