uni-app日期范围选择,颗粒度为年-月-日

2024-01-31 10:18

本文主要是介绍uni-app日期范围选择,颗粒度为年-月-日,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图:

 

1.在components中引入term-picker.vue

<template><view :class="{'pickerMask':visable}" @click="maskClick" @touchmove.stop.prevent="returnHandle"><view class="picker-box" :class="{'picker-show':visable}"><view class="operate-box" @touchmove.stop.prevent="returnHandle" @tap.stop="returnHandle"><view class="time-box"><view @click="touchSelect(0)" class="time-item" :style="{color:touchIndex?'#000000':themeColor}"><text>{{startText}}</text><text>{{resultDate[0]}}</text></view><text>至</text><view @click="touchSelect(1)" class="time-item" :style="{color:touchIndex?themeColor:'#000000'}"><text>{{endText}}</text><text>{{resultDate[1]}}</text></view></view><view :style="{color:themeColor}" @click="pickerConfirm">确定</view></view><picker-view :value="pickerValue" @change="pickerChange" class="picker-view" :indicator-style="indicatorStyle" @tap.stop="returnHandle"><picker-view-column><view class="picker-item" v-for="(item, index) in years" :key="index">{{item}}年</view></picker-view-column><picker-view-column><view class="picker-item" v-for="(item, index) in months" :key="index">{{ item }}月</view></picker-view-column><picker-view-column v-if="days.length > 0"><view class="picker-item" v-for="(item, index) in days" :key="index">{{ item }}日</view></picker-view-column></picker-view></view></view>
</template>
<script>export default {name: 'termPicker',props: {visable: {type: Boolean,default: false},defaultDate: {type: Array,default: () => []},minYear: {type: Number,default: 1990,},themeColor:{type: String,default: '#10BE9D'},startText: {type: String,default: '开始时间'},endText: {type: String,default: '结束时间'}},data() {const date = new Date();const years = [];const year = date.getFullYear();const months = [];const month = date.getMonth() + 1;const day = date.getDate();for (let i = this.minYear; i <= date.getFullYear(); i++) {years.push(i);}for (let i = 1; i <= 12; i++) {months.push(i);}return {indicatorStyle: 'height: 100rpx;',touchIndex: 0,year,month,day,years,months,days: [],pickerValue: [],resultDate: []};},mounted() {this.setDate()},methods: {returnHandle(){},setDate() {if (this.defaultDate.length > 0) {let date = this.defaultDate[0]this.resultDate = this.defaultDatethis.setPicker(date)} else {let month = this.month < 10 ? '0' + this.month : this.monthlet day = this.day < 10 ? '0' + this.day : this.daylet nowTime = this.year + '-' + month + '-' + daythis.resultDate = [nowTime, nowTime]this.setPicker(nowTime)}},setPicker(date) {const splitVal = date.split('-')let year = this.years.indexOf(Number(splitVal[0]))let month = Number(splitVal[1]) - 1let day = Number(splitVal[2]) - 1this.pickerChange({detail: {value: [year, month, day]}})},touchSelect(val) {let date = this.resultDate[val]this.touchIndex = valthis.setPicker(date)},getDateTime(date) {let year = this.years[date[0]]let month = this.months[date[1]]let day = this.days[date[2]]if (month < 10) {month = '0' + month}if (day < 10) {day = '0' + day}this.resultDate[this.touchIndex] =  year + '-' + month + '-' + day},pickerChange(e) {const currents = e.detail.value;if (currents[1] + 1 === 2) {this.days = [];if (((currents[0] + this.minYear) % 4 === 0 &&(currents[0] + this.minYear) % 100 !== 0) ||(currents[0] + this.minYear) % 400 === 0) {for (let i = 1; i < 30; i++) {this.days.push(i);}} else {for (let i = 1; i < 29; i++) {this.days.push(i);}}} else if ([4, 6, 9, 11].some((item) => currents[1] + 1 === item)) {this.days = [];for (let i = 1; i < 31; i++) {this.days.push(i);}} else if ([1, 3, 5, 7, 8, 10, 12].some((item) => currents[1] + 1 === item)) {this.days = [];for (let i = 1; i < 32; i++) {this.days.push(i);}}this.pickerValue = currentsthis.getDateTime(currents)},maskClick() {this.$emit('update:visable', false)},pickerConfirm() {const { resultDate } = thislet startTime = new Date(resultDate[0]).getTime()let endTime = new Date(resultDate[1]).getTime()let nowTime = new Date().getTime()if (startTime <= endTime && endTime <= nowTime) {this.$emit('confirm', resultDate)this.maskClick()} else {uni.showToast({title: '时间范围不正确!',icon: 'none'})}}}}
</script><style lang="scss" scoped>.pickerMask {position: fixed;z-index: 998;top: 0;right: 0;left: 0;bottom: 0;background: rgba(0, 0, 0, 0.6);}.picker-box {position: fixed;bottom: 0;left: 0;width: 100%;transition: all 0.3s ease;transform: translateY(100%);z-index: 998;.operate-box {display: flex;align-items: center;justify-content: space-between;padding: 18rpx 30rpx;background-color: #FFFFFF;text-align: center;font-size: 30rpx;border-bottom: 2rpx solid #e5e5e5;.time-box {width: 60%;display: flex;align-items: center;justify-content: space-between;.time-item {display: flex;flex-direction: column;}}}}.picker-show {transform: translateY(0);}.picker-view {width: 750rpx;height: 600rpx;background-color: #FFFFFF;.picker-item {height: 100rpx;display: flex;align-items: center;justify-content: center;text-align: center;}}
</style>

2.使用

<template><view class="content"><view class="header"><view class="date-item">年月范围:{{startDay}} - {{endDay}}</view></view><button class="date-btn" type="default" @click="openPicker">打开</button>
<term-picker :visable.sync="pickerShowByDay" :defaultDate="defaultDay" :maxDay="maxDay" @confirm="confirmByDay">
</term-picker></view>
</template><script>import termPicker from '/components/term-picker/term-picker.vue'; //日期选择export default {components: {termPicker},data() {return {pickerShowByDay: false, //是否日期选择控件defaultDay: [], //日期默认选择,为空则是当前日期maxDay: 60, //最大跨度日startDay: '',endDay: ''}},methods: {openPicker() {this.pickerVisable = true},confirm(date) {this.startDay = date[0]this. endDay = date[1]}}}
</script><style>.header {width: 100%;padding: 60rpx 30rpx;display: flex;flex-direction: column;}.date-item {margin-bottom: 30rpx;}.date-btn {width: 500rpx;height: 80rpx;line-height: 80rpx;}
</style>

这篇关于uni-app日期范围选择,颗粒度为年-月-日的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/663427

相关文章

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ

macOS怎么轻松更换App图标? Mac电脑图标更换指南

《macOS怎么轻松更换App图标?Mac电脑图标更换指南》想要给你的Mac电脑按照自己的喜好来更换App图标?其实非常简单,只需要两步就能搞定,下面我来详细讲解一下... 虽然 MACOS 的个性化定制选项已经「缩水」,不如早期版本那么丰富,www.chinasem.cn但我们仍然可以按照自己的喜好来更换

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

如何选择适合孤独症兄妹的学校?

在探索适合孤独症儿童教育的道路上,每一位家长都面临着前所未有的挑战与抉择。当这份责任落在拥有孤独症兄妹的家庭肩上时,选择一所能够同时满足两个孩子特殊需求的学校,更显得尤为关键。本文将探讨如何为这样的家庭做出明智的选择,并介绍星贝育园自闭症儿童寄宿制学校作为一个值得考虑的选项。 理解孤独症儿童的独特性 孤独症,这一复杂的神经发育障碍,影响着儿童的社交互动、沟通能力以及行为模式。对于拥有孤独症兄

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验