微信小程序——获取当天的前一个月至后一个月
看标题也不知道你有没有明白我想表达的意思,先上个动态图吧~
需要分析:
1.获取当前日期的前一个月,后一个月和当月。比如说现在是7月5号,我需要得到6月5号至8月5号的日期,同时还要返回当前的星期。
2.滑动到某个月份的区间的时候,左侧也相应的变到当前月份。比如我现在滑动到6月10号了,那么左侧就要显示成6月了。
3.页面打开默认是显示今天。
实现思路:
1.获取本月的数据(这个例子中就是从7.1至7.31)
2.获取上个月某号至月底的数据(这个例子中就是从6.5至6.30)
3.获取下个月1号至某号的数据(这个例子中就是从8.1至8.5)
4.获取这个月1号,今天,和下一个月1号的scrollLeft的值,这是为了在滑动的时候判断它当前的日期区间是在哪个月份,从而改变左侧的月份值。(这个例子中就是从7.1,7.5,8.1这三个scrollLeft的值)
5.默认显示今天可能通过改变scroll-view的scroll-left的值实现
主要用到的函数:
1.setDate(day):设置一个月的某一天
2.setMonth(month[,day]):设置月份,day是可选参数,可填可不填。填了返回某月的某一天,不填返回某月的第1天。
3.getMonth(),getDate(),getDay():获取月(从0开始),日期,星期(返回0时表示星期日)
具体代码:
JS:
- // pages/teacher/interview/index.js
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- },
- //获取星期
- getWeek(date) {
- let weekArray = ['日', '一', '二', '三', '四', '五', '六'];
- return weekArray[date.getDay()];
- },
- //滑动至某个区间时显示当前的月份
- dayScroll(e) {
- const scrollLeftArray = this.data.scrollLeftArray;
- console.log(e.detail.scrollLeft)
- let dayScrollLeft = e.detail.scrollLeft;
- if (dayScrollLeft < scrollLeftArray[0] - 100) {
- this.setData({
- showCurrentMonth: this.data.month
- })
- } else if (this.data.day < 7) {
- if (e.detail.scrollLeft > scrollLeftArray[2] - (7 - this.data.day)*100) {
- this.setData({
- showCurrentMonth: this.data.month + 2
- })
- }
- } else if (this.data.day >= 7){
- if (e.detail.scrollLeft > scrollLeftArray[2]) {
- this.setData({
- showCurrentMonth: this.data.month + 2
- })
- }
- } else {
- this.setData({
- showCurrentMonth: this.data.month + 1
- })
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- const _this = this;
- let now = new Date(),
- month = now.getMonth(),
- weekday = _this.getWeek(now),
- day = now.getDate(),
- prevMonth = month==0 ? 11 : month-1,
- nextMonth = month==11 ? 0 : month+1,
- lastDay = new Date((new Date().setMonth(month + 1, 1) - 1000 * 60 * 60 * 24)).getDate(), //获取当月最后一天日期;
- prevLastDay = new Date((new Date().setMonth(month, 1) - 1000 * 60 * 60 * 24)).getDate(); //获取上一个月最后一天日期;
- let currentMonthDateArray = [], //当前月份的日期和星期的数据集合
- prevMonthDateArray = [], //上月日期和星期的数据集合
- nextMonthDateArray = []; //下月日期和星期的数据集合
- for (let i = 1; i <= lastDay; i++) {
- currentMonthDateArray.push({
- day: i,
- weekDay: _this.getWeek(new Date(new Date().setDate(i)))
- })
- }
- for (let i = day; i <= prevLastDay; i++) {
- prevMonthDateArray.push({
- day: i,
- weekDay: _this.getWeek(new Date(new Date().setMonth(month - 1, i)))
- })
- }
- for (let i = 1; i <= day; i++) {
- nextMonthDateArray.push({
- day: i,
- weekDay: _this.getWeek(new Date(new Date().setMonth(month + 1, i)))
- })
- }
- _this.setData({
- day: day,
- month: month,
- weekday: weekday,
- showCurrentMonth: month + 1,
- prevMonthDateArray: prevMonthDateArray,
- currentMonthDateArray: currentMonthDateArray,
- nextMonthDateArray: nextMonthDateArray
- })
- //获取左边距是为了滑动时改变月份
- const query = wx.createSelectorQuery();
- let scrollLeftArray = [];
- query.selectAll(`#day${month + 1}1, #day${month + 1}${day}, #day${month + 2}1`).boundingClientRect(function(rects) {
- rects.forEach(function(rect) {
- scrollLeftArray.push(rect.left)
- })
- _this.setData({
- scrollLeftArray: scrollLeftArray,
- scrollLeftInit: scrollLeftArray[1] - 100
- })
- console.log(scrollLeftArray)
- }).exec()
- },
- })
wxml:
- <view class='row day-wrap item-center'>
- <view class='month fs-28 fc-66 shrink'>
- <text class='fs-48'>{{showCurrentMonth}}</text>月
- </view>
- <view class='day-list grow over-hidden'>
- <scroll-view scroll-x="{{true}}" class='day-list-scroll row' bindscroll="dayScroll" scroll-left="{{scrollLeftInit}}px">
- <view class="day-item {{prevMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{prevMonthDateArray}}" wx:for-item="prevMonth" id="day{{month}}{{prevMonth.day}}">
- <view class='fs-24'>{{prevMonth.weekDay}}</view>
- <view class='fs-32 mt-3'>{{prevMonth.day}}</view>
- </view>
- <view class="day-item {{currentMonth.day == day ? 'today' : ''}} {{currentMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{currentMonthDateArray}}" wx:for-item="currentMonth" id="day{{month+1}}{{currentMonth.day}}">
- <view class='fs-24'>{{currentMonth.weekDay}}</view>
- <view class='fs-32 mt-3'>{{currentMonth.day}}</view>
- </view>
- <view class="day-item {{nextMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{nextMonthDateArray}}" wx:for-item="nextMonth" id="day{{month+2}}{{nextMonth.day}}">
- <view class='fs-24'>{{nextMonth.weekDay}}</view>
- <view class='fs-32 mt-3'>{{nextMonth.day}}</view>
- </view>
- </scroll-view>
- </view>
- </view>
wxss:
- .day-wrap{
- background-color: #fff;
- padding-top: 10px;
- padding-bottom: 10px;
- }
- .month{
- padding: 0 15px;
- }
- .day-list-scroll{
- white-space:nowrap;
- }
- .day-item{
- padding: 2px 8px;
- margin-right:15px;
- text-align: center;
- display: inline-block;
- vertical-align: middle
- }
- .day-item.weekday{
- background-color: #f2f2f2;
- }
- .day-item.today{
- background-color: #ffdee8;
- color: #ff5e2f
- }
Happy Ending~
微信小程序——获取当天的前一个月至后一个月的更多相关文章
- 微信小程序-获取经纬度
微信小程序-获取经纬度 最近公司新功能 要求在外的市场人员 发送位置信息回来. 用的还是微信小程序开发.... 微信小程序 提供一个接口 getLocation 这个接口反回来的位置 相对实际位置 相 ...
- 微信小程序-获取当前城市位置及再次授权地理位置
微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...
- 微信小程序获取Access_token和页面URL生成小程序码或二维码
1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...
- [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息
因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...
- C# 微信小程序获取openid sessionkey
项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...
- JavaScript和微信小程序获取IP地址的方法
最近公司新加了一个需求,根据用户登录的IP地址判断是否重复登录,重复登录就进行逼退,那么怎么获取到浏览器的IP地址呢?最后发现搜狐提供了一个JS接口,可以通过它获取到客户端的IP. 接口地址如下: h ...
- 微信小程序获取输入框(input)内容
微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...
- .Net之微信小程序获取用户UnionID
前言: 在实际项目开发中我们经常会遇到账号统一的问题,如何在不同端或者是不同的登录方式下保证同一个会员或者用户账号唯一(便于用户信息的管理).这段时间就有一个这样的需求,之前有个客户做了一个微信小程序 ...
- 微信小程序获取手机号码看这篇文章就够了
前言 微信小程序获取手机号码,从官方文档到其他博主的文档 零零散散的 (我就是这样看过来 没有一篇满意的 也许是我搜索姿势不对) 依旧是前人栽树 后人乘凉 系列.保证看完 就可以实现获取手机号码功能 ...
随机推荐
- graph处理工具
仅作为记录笔记,完善中...................... 1 PyGSP https://pygsp.readthedocs.io/en/stable/index.html ht ...
- [转帖]关于4A(统一安全管理平台)系统的理解
雪山上的蒲公英 https://www.cnblogs.com/zjfjava/p/10674577.html 关于4A(统一安全管理平台)系统的理解 1. 4A系统的需求分析 近年来企业用户的业 ...
- poj1458公共子序列 C语言
/*Common SubsequenceTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 56416 Accepted: 23516D ...
- 国内P2P网贷行业再次大清理,仅剩646家
最近有网贷行业头部网站流出消息,国内网贷行业再次迎来大洗牌 清扫之后网贷的平台数量仅剩646家,数量陡降 根据小编了解.自2007年国外网络借贷平台模式引入中国以来,由于国家一时没有做出相应规定个条例 ...
- python_dict json读写文件
命令汇总: json.dumps(obj) 将python数据转化为json Indent实现缩进,ensure_ascii 是否用ascii解析 json.loads(s) 将json数据转换为py ...
- DevExpress中GridColumnCollection实现父子表数据绑定
绑定数据: 父表: DataTable _parent = _dvFlt.ToTable().Copy(); 子表: DataTable _child = _dvLog.ToTable().Copy( ...
- hystrix简介
hystrix,框架,提供了高可用相关的各种各样的功能,然后确保说在hystrix的保护下,整个系统可以长期处于高可用的状态,100%. 高可用系统架构: 资源隔离.限流.熔断.降级.运维监控 资源隔 ...
- Windows 7 下安装 docker
Windows 7 下需要安装docker toolbox即可(里面打包了docker.oracle virtualbox.Git) 1. 下载 1. 下载路径https://github.com/d ...
- 【转载】C#中List集合使用IndexOf判断元素第一次出现的索引位置
在C#的List集合操作中,有时候需要判断元素对象在List集合中第一次出现的索引位置信息,此时需要使用到List集合的IndexOf方法来判断,如果元素存在List集合中,则IndexOf方法返回所 ...
- Python运算符大全
一. Python的算术运算 Python的算术运算符与C语言类似,略有不同.包括加(+).减(-).乘(*).除(/).取余(%).按位或(|).按位与(&).按位求补(~).左移位(< ...