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

相关文章

Java日期类详解(最新推荐)

《Java日期类详解(最新推荐)》早期版本主要使用java.util.Date、java.util.Calendar等类,Java8及以后引入了新的日期和时间API(JSR310),包含在ja... 目录旧的日期时间API新的日期时间 API(Java 8+)获取时间戳时间计算与其他日期时间类型的转换Dur

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

exfat和ntfs哪个好? U盘格式化选择NTFS与exFAT的详细区别对比

《exfat和ntfs哪个好?U盘格式化选择NTFS与exFAT的详细区别对比》exFAT和NTFS是两种常见的文件系统,它们各自具有独特的优势和适用场景,以下是关于exFAT和NTFS的详细对比... 无论你是刚入手了内置 SSD 还是便携式移动硬盘或 U 盘,都需要先将它格式化成电脑或设备能够识别的「文

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详