uniapp picker中的日期选择器和时间选择器demo效果(整理)

该代码示例展示了如何在Vue.js应用中创建日期和时间选择器。用户可以选择日期并限制可选范围,以及选择时间并在指定时间段内进行选择。同时,包含了日期和时间的变更事件处理函数。

在这里插入图片描述

<template>
	<view>
		<view class="box_date" style="margin-top: 200rpx;">
			<view>
				<text>请选择日期:</text>
			</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="changeDate">
				<view class="date">
					<input type="text" disabled="true" v-model="date">
				</view>
			</picker>
		</view>
	</view>
</template>
<script>
	function getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 10;
		} else if (type === 'end') {
			year = year + 10;
		}
		month = month > 9 ? month : '0' + month;
		day = day > 9 ? day : '0' + day;

		return `${year}-${month}-${day}`;
	}
	export default {
		data() {
			return {
				date: getDate(), //当前日期
				startDate: getDate('start'), //可选择的最小日期
				endDate: getDate('end'), //可选择的最大日期
			};
		},
		methods: {
			changeDate(e) {
				this.date = e.detail.value;
				console.log('选择的日期:', this.date)
			},
		}
	};
</script>
<style>
	.box_date {
		margin: 20upx;
		display: flex;
	}

	.date {
		border: 1px solid #ccc;
		padding: 10upx;
		width: 300upx;
	}

	.box_date text {
		line-height: 60upx;
	}
</style>

在这里插入图片描述

<template>
    <view>
      <view class="box_time">
            <view>
                <text>请选择时间:</text>
            </view>
            <picker mode="time" :value="time"  :start="minTime" :end="maxTime" @change="changeTime">
                <view class="time">
                    <input type="text" disabled="true" v-model="time">
                </view>
            </picker>
        </view>
    </view>
</template>
<script>
function getTime(){
        const time = new Date();
        let hour = time.getHours();
        let minute = time.getMinutes();
        
        hour = hour > 9 ? hour : '0' + hour;
        minute = minute > 9 ? minute : '0' + minute;
        
        return `${hour}:${minute}`;
    }
export default {
   data() {
            return {
                time: getTime(), //当前时间
                minTime: '09:00', //可选择的最小时间
                maxTime: '18:00' //可选择的最大时间
            };
        },
        methods: {
            changeTime(e) {
                this.time = e.detail.value
            }
        }
};
</script>
<style >
    .box_time{
        margin: 20upx;
        display: flex;
    }
    .time{
        border: 1px solid #ccc;
        padding: 10upx;
        width: 300upx;
    }
    .box_time text{
        line-height: 60upx;
    }
</style>

选择开始时间结束时间

<template>
	<view class="ditchIndex">
		<view class="margin-top100"></view>
		<!-- 中间内容开始 -->
		<view class="ditchMain">
			<view class="clientBox">
				<view class="clientTime">
					<view class="timeBox">
						<image src="../../static/img/icon_time.png" mode="widthFix" class="timeImg"></image>
						<view class="time1">
							<picker mode="date" :value="start_date" @change="bindDateChange">
								<view class="date">{{otherEnd}}</view>
							</picker>
						</view>
						<view class="txt"></view>
						<view class="time2">
							<picker mode="date" :value="start_date" @change="bindDateChange2">
								<view class="date">{{other}}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 中间内容结束 -->

	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				system: [],
				menu: [],
				// 计算头部距离结束
				start_date: currentDate,
				end_date: currentDate,
				other: '请输入',
				otherEnd: '请输入',
			}
		},
		onLoad() {

		},

		methods: {
			// 选择时间开始
			// 选择日期
			bindDateChange: function(e) {
				this.start_date = e.target.value
				this.otherEnd = this.start_date;

			},
			bindDateChange2: function(e) {
				this.end_date = e.target.value;
				this.other = this.end_date;
			},
			// 获取当前时间
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 选择时间结束
		}
	}
</script>

<style lang="scss" scoped>
	.ditchIndex {

		.ditchMain {
			margin-right: 30rpx;
			margin-left: 30rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding-top: 30rpx;
			padding-bottom: 30rpx;

			.clientBox {
				margin-right: 30rpx;
				margin-left: 30rpx;

				.clientTime {
					.timeBox {
						display: flex;
						align-items: center;
						height: 60rpx;
						border: 1rpx solid #ECECEC;
						opacity: 1;
						border-radius: 10rpx;

						.timeImg {
							width: 30rpx !important;
							height: 30rpx !important;
							margin-left: 20rpx;

						}
					}
				}
			}
		}

	}
</style>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值