【uni-app】通过uni-app基础组件picker实现选择日期、时间的功能示例(完整代码+图文)

该文章展示了如何在Vue.js应用中使用picker组件来创建日期和时间选择功能。用户可以选择日期,然后更新显示的值,同样的过程也适用于时间选择。代码示例包括了基础的日期选择和改进的日期及时间选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、获取日期(基础)

<template>
	<view class="container">
		<picker mode="date" @change="onDateChange" :value="date">
			<view class="date-picker">{{date}}</view>
		</picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				date: '请选择日期'
			}
		},
		methods: {
			onDateChange: function(e) {
				this.date = e.detail.value;
			}
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
	}

	.date-picker {
		line-height: 80rpx;
		border: 1px solid #ccc;
		padding: 10rpx;
		text-align: center;
		background-color: #fff;
	}
</style>

二、获取日期和时间(改进)

 

<template>
	<view>
			<!--日期选择-->
			<view class="SelectDate">
				<view class="DateLabel">
					预定日期
				</view>
				<view class="DateText">
					<picker mode="date" @change="onDateChange" :value="DateValue">
						<view class="date-picker">{{DateValue}}</view>
					</picker>
				</view>
			</view>

			<view class="SelectTime">
				<view class="TimeLabel">
					预定时间
				</view>
				<view class="TimeText">
					<picker mode="time" @change="onTimeChange" :value="TimeValue">
						<view class="Time-picker">{{TimeValue}}</view>
					</picker>
				</view>
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				DateValue: "点击选择日期",
				TimeValue: "点击选择时间",
			}
		},
		methods: {
			
			onDateChange: function(e) {
				this.DateValue = e.detail.value;
			},

			onTimeChange: function(e) {
				this.TimeValue = e.detail.value;
			},
			}
	}
</script>

<style scoped>

	/* ## 日期 ## */

	.SelectDate {
		height: 40px;
		wdith: 100%;
		display: flex;
		flex-direction: grow;
		/* background-color: red; */
	}

	.DateLabel {
		width: 0;
		flex-grow: 3;
		background-color: #eaeaea;
		line-height: 40px;
		text-align: left;
		padding-left: 40px;
		border: 1px solid #f3f3f3;
	}

	.DateText {
		width: 0;
		flex-grow: 7;
	}

	.date-picker {
		background-color: aquamarine;
		height: 40px;
		line-height: 25px;
		width: 100%;

		border: 1px solid #f3f3f3;
		padding: 10rpx;
		text-align: center;
		background-color: #fff;
		color: #8f8f8f;
	}

	/* ## 时间 ## */

	.SelectTime {
		height: 40px;
		wdith: 100%;
		margin-top: 10px;
		display: flex;
		flex-direction: grow;
		/* background-color: red; */
	}

	.TimeLabel {
		width: 0;
		flex-grow: 3;
		background-color: #eaeaea;
		line-height: 40px;
		text-align: left;
		padding-left: 40px;
		border: 1px solid #f3f3f3;
	}

	.TimeText {
		width: 0;
		flex-grow: 7;
	}

	.Time-picker {
		background-color: aquamarine;
		height: 40px;
		line-height: 25px;
		width: 100%;
		border: 1px solid #f3f3f3;
		padding: 10rpx;
		text-align: center;
		background-color: #fff;
		color: #8f8f8f;
	}
</style>

{ "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages /* 主页面 */ { "path": "pages/index/index", "style": { // 导航栏标题文字内容 "navigationBarTitleText": "今天吃什么", // 是否开启下拉刷新 "enablePullDownRefresh": true } }, { "path": "pages/Inventory/Inventory", "style": { // 导航栏标题文字内容 "navigationBarTitleText": "今天吃什么", // 是否开启下拉刷新 "enablePullDownRefresh": true } }, { "path": "pages/share/share", "style": { // 导航栏标题文字内容 "navigationBarTitleText": "今天吃什么", // 是否开启下拉刷新 "enablePullDownRefresh": true } }, { "path": "pages/me/me", "style": { // 导航栏标题文字内容 "navigationBarTitleText": "今天吃什么", // 是否开启下拉刷新 "enablePullDownRefresh": true } }, ], "globalStyle": { // 导航栏标题颜色及状态栏前景颜色,仅支持 black/white "navigationBarTextStyle": "black", // 导航栏标题文字内容 "navigationBarTitleText": "今天吃什么?", // 导航栏背景颜色(同状态栏背景色) "navigationBarBackgroundColor": "#e5e5e5", "backgroundColor": "#e5e5e5" }, "uniIdRouter": { }, "tabBar": { // 字体颜色 "color": "#333333", // 选中字体颜色 "selectedColor": "#333333", "borderStyle": "black", "backgroundColor": "#fff", "fontSize": "16px", "iconWidth": "21px", "list": [{ "pagePath": "pages/index/index", "text": "菜谱", // 图标路径 "iconPath": "/static/menu.png", // 选中图标路径 "selectedIconPath": "/static/menu 0.png" }, { "pagePath": "pages/Inventory/Inventory", "text": "库存", // 图标路径 "iconPath": "/static/Inventory.png", // 选中图标路径 "selectedIconPath": "/static/Inventory 0.png" }, { "pagePath": "pages/share/share", "text": "广场", // 图标路径 "iconPath": "/static/share.png", // 选中图标路径 "selectedIconPath": "/static/share 0.png" }, { "pagePath": "pages/me/me", "text": "我的", // 图标路径 "iconPath": "/static/me.png", // 选中图标路径 "selectedIconPath": "/static/me 0.png" } ] } } 这是uniapp的pages.json文件,根据以上代码发挥想象生出完整的小程序代码,搭建一个完整的小程序,菜谱页面需要实现功能是自定义菜单菜谱,库存页面需要实现功能是手动录入食材信息包括名称,保质期,数量,广场页面需要实现功能是用户菜品成果分享,我的页面需要实现登录账号,用户提交使用反馈功能;在此基础上,在发布新内容功能上增加文案叙述功能,将成功发布的内容更新在广场页面里面;新建菜单改名为添加菜谱,增加AI生成功能,可以输入菜名、做菜想法、使用食材等,也可以粘贴文字菜谱;库存页面增加录入食材信息功能(包括食材名称,生产日期,保质期,数量),根据保质期为用户提供过期提醒;我的页面完善登录微信之后出现用户昵称和头像。
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敦厚的曹操

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值