uView DatetimePicker 选择器

此选择器用于时间日期

#平台差异说明

App(vue) App(nvue) H5 小程序

#基本使用

  • 通过show绑定一个布尔值变量,用于控制组件的弹出与收起。

  • 通过mode配置选择何种日期格式。

    <template> <view> <u-datetime-picker :show="show" v-model="value1" mode="datetime" ></u-datetime-picker> <u-button @click="show = true">打开</u-button> </view> </template> <script> export default { data() { return { show: false, value1: Number(new Date()), } } } </script>

copy

#年 月 日

此模式通过mode设置为date

复制代码
<template>
    <view>
        <u-datetime-picker
                :show="show"
                v-model="value1"
                mode="date"
        ></u-datetime-picker>
        <u-button @click="show = true">打开</u-button>
    </view>
</template>

<script>
	export default {
        data() {
            return {
                show: false,
                value1: Number(new Date()),
            }
        }
	}
</script>

copy

#格式化

如有需要,可以通过formatter参数编写自定义格式化规则。

注意:

微信小程序不支持通过props传递函数参数,所以组件内部暴露了一个setFormatter方法用于设置格式化方法,注意在页面的onReady生命周期获取ref再操作。

复制代码
<template>
    <view>
        <u-datetime-picker
			ref="datetimePicker"
			:show="show"
            v-model="value1"
			mode="datetime"
			:formatter="formatter"
        ></u-datetime-picker>
        <u-button @click="show = true">打开</u-button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                show: false,
                value1: Number(new Date()),
            }
        },
		onReady() {
			// 微信小程序需要用此写法
			this.$refs.datetimePicker.setFormatter(this.formatter)
		},
        methods: {
            formatter(type, value) {
                if (type === 'year') {
                    return `${value}年`
                }
                if (type === 'month') {
                    return `${value}月`
                }
                if (type === 'day') {
                    return `${value}日`
                }
                return value
            },
        },
    }
</script>

copy

#限制最大最小值

参数minDatemaxDate可以设置最大值和最小值(传入时间戳)。

复制代码
<template>
    <view>
        <u-datetime-picker
                :show="show"
                v-model="value1"
                :minDate="1587524800000"
                :maxDate="1786778555000"
                mode="datetime"
        ></u-datetime-picker>
        <u-button @click="show = true">打开</u-button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                show: false,
                value1: Number(new Date()),
            }
        }
    }
</script>
相关推荐
ZC跨境爬虫4 天前
模块化烹饪小程序开发日记 Day2:全局配置与 tabBar 实现
java·前端·javascript·微信小程序·html·notepad++
idolao8 天前
npp.8.5.Installer文本编辑器安装步骤详解(附Notepad++配置与插件安装教程)
notepad++
px不是xp9 天前
【灶台导航】 RAG系统的容错设计:从向量搜索到关键词降级,一个都不能少
javascript·微信小程序·notepad++·rag
空中海10 天前
微信小程序 - 03 工程实践层与综合 Demo
微信小程序·小程序·notepad++
舟遥遥娓飘飘12 天前
面向零基础初学者,从环境搭建到发布上线,手把手教你开发第一个微信小程序(第3章-认识项目结构)
微信小程序·小程序·notepad++
优睿远行12 天前
微信小程序自定义组件开发实战:从封装到发布的全流程指南
微信小程序·小程序·notepad++
喜欢南方姑娘14 天前
微信小程序热更新-用户打开小程序时检测版本自动更新
微信小程序·小程序·notepad++
yzx99101314 天前
从零开始写一个微信小程序:完整代码实战指南(入门篇)
微信小程序·小程序·notepad++
fengyehongWorld16 天前
Notepad++ NppExec插件的使用
notepad++
fengyehongWorld18 天前
Notepad++ 常用插件
notepad++