• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

小程序日历

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

示例图

 

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/[email protected]" style="transform: rotate(180deg)" bindtap='handleCalendar' data-handle="prev"/>
      <label>{{year}}年{{month}}月</label>
      <image class="direction" src="{{imgPath}}public/[email protected]" 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()
    }
})

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap