Uni-App中的u-datetime-picker时间选择器Demo

目录

前言

对于网页端的推荐阅读:【ElementUI】详细分析DatePicker 日期选择器

事情起因是两个时间选择器同步了,本身是从后端慢慢步入全栈,对此将这个知识点从实战进行提炼

通过Demo进行总结

Demo

用于选择日期和时间的组件,在 Uni-App 框架中很常用,可以配置成多种模式,如日期选择、时间选择、日期时间选择等

以下是实战提炼的Demo:

html 复制代码
<template>
	<view>
		<u-datetime-picker 
			@confirm="dateTimePickerConfirm" 
			@cancel="dateTimePicker = false"
			:show="dateTimePicker" 
			v-model="timeValue"
			ref="datetimePicker"
			mode="datetime"
			closeOnClickOverlay
			:defaultIndex="defaultTimeIndex">
		</u-datetime-picker>

		<u-form-item @click="dateTimePicker = true" label="选择时间:" labelWidth="200rpx">
			<u--input
				v-model="formattedTime"
				prefixIcon="calendar"
				readonly>
			</u--input>
		</u-form-item>
	</view>
</template>

<script>
export default {
	data() {
		return {
			defaultTimeIndex: [], // 默认选中的时间索引,可以为空
			timeValue: Number(new Date()), // 当前时间的时间戳
			dateTimePicker: false, // 控制选择器的显示
			formattedTime: '' // 用于显示格式化后的时间
		}
	},
	methods: {
		formatDate(date) {
			const pad = (num) => (num < 10 ? '0' + num : num);

			const year = date.getFullYear();
			const month = pad(date.getMonth() + 1);
			const day = pad(date.getDate());
			const hours = pad(date.getHours());
			const minutes = pad(date.getMinutes());
			const seconds = pad(date.getSeconds());

			return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
		},
		dateTimePickerConfirm(e) {
			this.dateTimePicker = false;
			const date = new Date(e.value);
			this.formattedTime = this.formatDate(date);
		}
	}
}
</script>

对应的解读如下:

  • show :控制选择器的显示状态。
  • v-model :绑定当前选中的时间值
  • mode :设置为 datetime,表示选择日期和时间
  • closeOnClickOverlay: 设置为 true,表示点击遮罩时关闭选择器。
  • @confirm :事件在确认选择时间时触发
  • @cancel :事件在取消选择时触发

对应的界面如下所示:

本文针对的是uni-app中的时间选择器:u-datetime-picker 官网解读

官网同样还有其他花里胡哨正常的选择:

相关推荐
予你@。1 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
游戏开发爱好者81 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
予你@。1 天前
uni-app progress 组件使用详解
uni-app
iOS阿玮1 天前
春节提审高峰来袭!App Store 审核时长显著延长。
uni-app·app·apple
2501_916007472 天前
ios上架 App 流程,证书生成、从描述文件创建、打包、安装验证到上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
iPhone 文件管理,如何进行应用沙盒文件查看
android·ios·小程序·https·uni-app·iphone·webview
2501_915921433 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记3 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue
2501_915921433 天前
iOS 抓包怎么绕过 SSL Pinning 证书限制,抓取app上的包
android·网络协议·ios·小程序·uni-app·iphone·ssl
予你@。4 天前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app