微信小程序(日历/日期)选择插件

2023-11-19

微信小程序日历选择器插件点击日历日期可以获取到年月日

wxml

<view class="canlendarBgView">
  <view class="canlendarView">
    <view class="canlendarTopView">
      <view class="leftBgView" bindtap="handleCalendar" data-handle="prev">
        <view class="leftView">《</view>
      </view>
      <view class="centerView">{{cur_year || "--"}} 年 {{cur_month || "--"}} 月</view>
      <view class="rightBgView" bindtap="handleCalendar" data-handle="next">
        <view class="rightView">》</view>
      </view>
    </view>
    <view class="weekBgView">
      <view class="weekView" wx:for="{{weeks_ch}}" wx:key="{{index}}" data-idx="{{index}}">{{item}}</view>
    </view>
    <view class="dateBgView">
      <view wx:if="{{hasEmptyGrid}}" class="dateEmptyView" wx:for="{{empytGrids}}" wx:key="{{index}}" data-idx="{{index}}">
      </view>
      <view class="dateView" wx:for="{{days}}" wx:key="{{index}}" data-idx="{{index}}" bindtap="dateSelectAction">
        <view class="datesView {{index == todayIndex ? 'dateSelectView' : ''}}">{{item}}</view>
      </view>
    </view>
  </view>
  <view>点击日期选择</view>
</view>

wxss

/* pages/calendar/calendar.wxss */

.canlendarBgView {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.canlendarView {
  color: #6666CC;
  display: flex;
  flex-direction: column;
}

.canlendarTopView {
  height: 80rpx;
  font-size: 28rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.leftBgView {
  text-align: right;
  height: 80rpx;
  display: flex;
  flex-direction: row-reverse;
}

.leftView {
  width: 80rpx;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.centerView {
  width: 50%;
  height: 80rpx;
  text-align: center;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.rightBgView {
  height: 80rpx;
  display: flex;
  flex-direction: row;
}

.rightView {
  width: 80rpx;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.weekBgView {
  height: 50rpx;
  line-height: 50rpx;
  opacity: 0.5;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.weekView {
  flex-grow: 1;
  text-align: center;
  font-size: 28rpx;
}

.dateBgView {
  height: 500rpx;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.dateEmptyView {
  width: 107.1428571429rpx;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dateView {
  width: 107.1428571429rpx;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.datesView {
  width: 60rpx;
  height: 60rpx;
  color: #6666CC;
  font-size: 26rpx;
  font-weight: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dateSelectView {
  border-radius: 50%;
  position: relative;
  left: 0;
  top: 0;
  color: #fff;
  background-color: #6666CC;
}

js

//index.js
//获取应用实例
Page({
  data: {
    hasEmptyGrid: false,
    cur_year: '',
    cur_month: '',
  },
  onLoad(options) {
    this.setNowDate();
  },

  dateSelectAction: function (e) {
    var cur_day = e.currentTarget.dataset.idx;
    this.setData({
      todayIndex: cur_day
    })
    console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day + 1}日`);
  },

  setNowDate: function () {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const todayIndex = date.getDate() - 1;
    console.log(`日期:${todayIndex}`)
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.setData({
      cur_year: cur_year,
      cur_month: cur_month,
      weeks_ch,
      todayIndex,
    })
  },

  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];

    const thisMonthDays = this.getThisMonthDays(year, month);

    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
    }

    this.setData({
      days
    });
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  }
})

 

点击下载:点击打开链接

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

微信小程序(日历/日期)选择插件 的相关文章

  • 《图解物联网》--阅读笔记

    第1 章物联网的基础知识1 1 1 物联网入门 2 1 1 1 物联网 2 1 1 2 物联网的相关动向 2 1 2 物联网所实现的世界 3 1 2 1 泛在网络 社会 3 1 2 2 物 的互联网连接 4 1 2 3 机器对机器通信所实现

随机推荐

  • Java String类型和BigDecimal类型之间的转化及BigDecimal类型的介绍

    String和BigDecimal的相互转化 String a 50 00 字符串类型 必须是数字 否则会报错 java lang NumberFormatException 异常 BigDecimal b new BigDecimal a
  • 汇编语言有如下的汇编程序段,请完成code段中的代码,实现将string1段和string2段中的数据拷贝到string3段中,并且将string3段中的数据输出到屏幕。

    有如下的汇编程序段 请完成code段中的代码 实现将string1段和string2段中的数据拷贝到string3段中 并且将string3段中的数据输出到屏幕 题目 有如下的汇编程序段 请完成code段中的代码 实现将string1段和s
  • Bose700降噪体验

    戴了多年耳塞 还是决定买一块主动降噪的看看 第一款主动降噪耳机当然选择降噪最强的bose700 直接官方旗舰店买不废话 虽然是主动降噪 不过众所周知是主要降低频部分1KHz以下 所以呢 给小白的用话小白会说 人声 喇叭声 风扇声都是听得到的
  • 学习动态规划-子矩阵

    1 全为1的最大正方形 在一个由 0 和 1 组成的二维矩阵内 找到只包含 1 的最大正方形 并返回其面积 来源 221 最大正方形 解题思路 dp i j 表示以matrix i j 为右下角的全1的正方形的最大边长 很明显 当matri
  • C++ 之 String类详解

    String 小引 string类常用接口 常见构造 容量操作 访问操作 修改操作 string类非成员函数 模拟实现 小引 C语言中 字符串是以 0结尾的一些字符的集合 为了操作方便 C标准库中提供了一些str系列的库函数 但是这些库函数
  • MAC Android Studio 克隆新项目出现问题及解决方法

    目录 前言 重装Android Studio 卸载Android Studio 安装Android Studio 打开新项目 前言 MAC OS 10 15 1 使用Android Studio打开GIT克隆下来的新项目 报错1 Could
  • VS2022 E1696 无法打开源文件报错修改

    1 先检查安装时的配件都安装正确了没有 在工具栏位置打开 获取工具和功能 此时会跳转到我们一开始安装VS时要安装配件的界面 在该界面内 检查是否是 使用C 的桌面开发 的安装选项 如果不是的话就选中该应用并选择下载路径进行修改下载 2 如果
  • C++-std::unique_lock介绍和简单使用

    unique lock std unique lock比std lock guard更灵活 这种灵活性主要体现在以下几点 lock guard在构造时或者构造前 std adopt lock 就已经获取互斥锁 并且在作用域内保持获取锁的状态
  • Linux系统的安装(在VM虚拟机上安装CentOS 7)

    工具准备 物理计算机一台 配置要求 操作系统 win10 64位 大家基本上都是 硬盘可用容量 20G以上 内存容量 4G以上 虚拟机安装包 VMware workstation full 12 5 下载链接 点我下载 提取码 9gha C
  • 为什么程序员招聘都要5年经验起?因为他们懂Java8底层优化!

    一 前情回顾 上篇文章给大家聊了一下volatile的原理 具体参见 入坑两个月自研非外包创业公司 居然让我搞懂了volatile 这篇文章给大家聊一下java并发包下的CAS相关的原子操作 以及Java 8如何改进和优化CAS操作的性能
  • 在Qt中如何实现窗口交互

    首先介绍done函数 它的作用是 关闭当前窗口 同时返回一个状态信息 Qt助手解释 关闭对话框并将其结果代码设置为r 如果这个对话框显示了exec done 导致本地事件循环结束 exec 返回r void QDialog done int
  • checkbox样式改写

    div class checkbox font s div
  • js 微观任务、宏观任务、循环机制

    javascript是单线程语言 就是因为单线程的特性 就不得不提js中的同步和异步 同步和异步 所谓单线程 无非就是同步队列和异步队列 js代码是自上向下执行的 在主线程中立即执行的就是同步任务 比如简单的逻辑操作及函数 而异步任务不会立
  • 计算机网络--绪论

    一 计网的体系结构 1 概念和功能 2 组成和分类 3 标准化工作及相关组织 二 性能指标 1 速率 2 带宽 3 吞吐量 4 时延 5 时延带宽积 6 往返时间RTT利用率 7 利用率 三 分层结构 1 分层 四 OSI参考模型 1 OS
  • 随机数产生方法

    5 产生一定范围随机数的通用表示公式 要取得 a b 的随机整数 使用 rand b a a 要取得 a b 的随机整数 使用 rand b a 1 a 要取得 a b 的随机整数 使用 rand b a a 1 通用公式 a rand n
  • 【Vue】Vue基础自用笔记&Day02_①Vue过滤器②按键修饰符③自定义指令

    Vue基础 Day02 1 Vue过滤器 2 按键修饰符 3 自定义Vue指令 1 Vue过滤器 Vue js 允许你自定义过滤器 可被用于一些常见的文本格式化 过滤器可以用在两个地方 双花括号 插值和 v bind 表达式 后者从 2 1
  • 袁红岗的编程感悟

    我自己知道 近几年也一直在用 但就是说不出来 直到最近几天才能够表达 叫作Think in Code 也就是用代码思考 同时也把代码当成自己思想表达的方式 正如哲学家用文字设计 诠释思想 程序员 说话 用的是代码 这就是一个程序员的境 界
  • 使用python实现简单全连接神经网络

    最近在学习神经网络的相关知识 特在此做一个笔记 python语言的功能很强大 可以使用很少的代码实现很多功能 因此大家如果想研究深度学习的话 一定要懂得python语言 这篇笔记记录我的第一次使用python编写神经网络代码的过程 其中代码
  • Centos7 ELK7.6.2集群搭建

    Centos7 ELK7 6 2集群搭建 ELK7 6 2网盘安装包下载 一 单节点准备 配置ip 配置主机名和主机名映射 关闭防火墙 事件同步 更换yum源 阿里云yum源 安装常用软件 系统优化 创建用来启动es的普通用户 jdk安装
  • 微信小程序(日历/日期)选择插件

    微信小程序日历选择器插件点击日历日期可以获取到年月日 wxml