完整教程:uniapp 日历组件 uni-datetime-picker

news/2025/10/3 18:05:37/文章来源:https://www.cnblogs.com/slgkaifa/p/19124820

完整教程:uniapp 日历组件 uni-datetime-picker

背景:

App端需要日历组件,选择年月日,或者年月日时分秒。找到了两种组件,如下:

方式一:

DCloud插件市场的uni-datetime-picker组件,设置为日期时间选择器【datetimerange】,或者日期选择器【daterange】

<uni-datetime-picker

v-model="timeData"

type="datetimerange"

rangeSeparator="至"

:start="minDate"

:end="maxDate"

@change="maskChange"

@maskClick="maskClick"

/>

方式二:

uniapp基础组件的picker组件,设置为日期选择器 mode="date"

<picker 
  mode="date" 
  :value="date" 
  :start="startDate" 
  :end="endDate" 
  @change="bindDateChange">
    <view class="uni-input">{{date}}</view>
</picker>

插件市场有插件uni-datetime-pickeruniapp内置组件picker组件。

一、插件市场uni-datetime-picker组件

效果展示:【由两部分组成:一组件,二点击组件的弹框】

日期选择器:

设置type为:daterange 或者 datetimerange

js逻辑层:

<script>
export default {data() {return {timeData: [],minDate: '',maxDate: '',},methods:{formatTime(time, format = "YY-MM-DD hh:mm:ss") {const args = {y: time.getFullYear(),M: time.getMonth() + 1,d: time.getDate(),h: time.getHours(),m: time.getMinutes(),s: time.getSeconds(),};for (const key in args) {const value = args[key];if (value < 10) args[key] = "0" + value;}const dateStr =args.y +"-" +args.M +"-" +args.d +" " +args.h +":" +args.m +":" +args.s;return dateStr;},maskChange(e) {this.timeData = e},maskClick() {},},created() {const maxDate = this.formatTime(new Date(Date.now()))const minDate1 = this.formatTime(new Date(Date.now() - 8.64e7))const minDate7 = this.formatTime(new Date(Date.now() - 8.64e7 * 7))this.minDate = minDate7this.maxDate = maxDatethis.timeData = [minDate1, maxDate]},
}

插件市场官网:点击跳转

下载插件市场的插件,可以通过HBuilder下载,或者下载zip然后导入项目文件夹uni_modules

二、uniapp内置组件

效果展示:

日期选择器:

{{date}}

js逻辑层:

<script>
export default {data() {const currentDate = this.getDate({format: true})return {title: 'picker',array: ['中国', '美国', '巴西', '日本'],index: 0,date: currentDate,time: '12:01'}},computed: {minDate() {return this.getDate('start');},maxDate() {return this.getDate('end');}},methods: {bindPickerChange: function(e) {console.log('picker发送选择改变,携带值为', e.detail.value)this.index = e.detail.value},bindDateChange: function(e) {this.date = e.detail.value},bindTimeChange: function(e) {this.time = e.detail.value},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}`;}}
}
</script>

uniapp组件官网链接:点击跳转

三、自行封装一个日历选择器

背景:

以上两种组件在H5端或者Android端组件的形式有所差异,可根据实际情况进行调整。

以下是自行封装了一个名为MyCalender的组件,组件只能选择具体的某一天。封装它的意图是想要弹出框尽量小,或者日历选择区域尽量小。

效果展示:

封装组件思路:

日期选择器只能选择一天。弹框根据实时日期渲染当月的总天数。大于当前日期的不可选并有提示信息,只能选择小于等于当前日期。

封装自定义组件:


<script>
export default {name: "MyCalender",props: {defaultTime: {type: String,default: ''},timeArr: {type: Number,default: 30,}},data() {return {showPop: false,clickData: '',inputValue: '',isOpen: false,timeCalender: 0,currentD: 0,currentDStyle: 0,}},methods: {handlePopup(state) {this.showPop = state;},toggle() {console.log('toggle');this.showPop = !this.showPop},handleOpenOption(data) {console.log('handleOpenOption', data);this.isOpen = !this.isOpen},clickCalenderI(item) {console.log('clickCalenderI', item);this.currentD = item//根据第几天得出日期const whichData = new Date(new Date().getFullYear(), new Date().getMonth(), item)this.clickData = whichDataif (this.clickData > new Date()) {this.clickData = new Date()this.currentD = this.clickData.getDate()uni.showToast({icon: 'none',title: `请重新选择日期,不可大于今日${this.currentD}号`,})} else {this.clickData = this.clickData}const whichData_String = this.clickData.getFullYear() + '-' + (this.clickData.getMonth() + 1) + '-' + this.clickData.getDate()this.inputValue = whichData_Stringthis.$emit('clickCalenderI', this.inputValue)}},created() {//timeCalenderconst currentDate = new Date();const currentY = currentDate.getFullYear();const currentM = currentDate.getMonth() + 1;const currentD = currentDate.getDate();const days = new Date(currentY, currentM, 0).getDate();console.log('days', days);this.timeCalender = days;this.currentDStyle = currentD},watch: {defaultTime: {handler(val) {if (val && val.split(' ').length) {this.inputValue = val.split(' ')[0]}},immediate: true,deep: true}}
}
</script>

使用自定义的组件:

<MyCalender :defaultTime="maxDate" @clickCalenderI="clickCalenderI"></MyCalender>

//vue2

clickCalenderI(item) {

    console.log('clickCalenderI', item);

    this.timeData = [`${item} 00:00:00`, `${item} 23:59:59`]

},

this.maxDate = this.formatTime(new Date(Date.now()))

formatTime(time, format = "YY-MM-DD hh:mm:ss") {

    const args = {

        y: time.getFullYear(),

        M: time.getMonth() + 1,

        d: time.getDate(),

        h: time.getHours(),

        m: time.getMinutes(),

        s: time.getSeconds(),

    };

    for (const key in args) {

        const value = args[key];

        if (value < 10) args[key] = "0" + value;

    }

    const dateStr =

        args.y +

        "-" +

        args.M +

        "-" +

        args.d +

        " " +

        args.h +

        ":" +

        args.m +

        ":" +

        args.s;

    return dateStr;

},

写到这儿。。。我们就实现了三种uniapp选择日期的方式,根据实际情况自行选择。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926159.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

个人二级网站怎么做汽车精品设计网站建设

让手机通过电脑上网的方式有很多种&#xff0c;最常见的就是 WIFI 了&#xff0c;而且简单直接。但是有时候台式机没有 WIFI &#xff0c;或者电脑的 WIFI 设备已经连接到其他的网络了&#xff0c;这时候手机就不能通过电脑的 WIFI 连接到网络。那么还没有有办法连接到网络呢&a…

实用指南:精读C++20设计模式:行为型设计模式:中介者模式

实用指南:精读C++20设计模式:行为型设计模式:中介者模式2025-10-03 17:55 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !importan…

c 网站开发引擎wordpress 后台翻译

一 前言MySQL 5.7.8 之后 支持 JSON (由rfc7159规定)数据类型&#xff0c;其能在字段中使用json 类型&#xff0c;做到了自动校验是否为json类型数据&#xff0c;否则插入数据会报异常&#xff1b;其次&#xff0c;储存json数据内部做到了优化储存&#xff0c;能够快速读取json…

成都医院手机网站建设宁波网站推广优化外包公司

动动发财的小手&#xff0c;点个赞吧&#xff01; 简介 作为理解、生成和处理自然语言文本的有效方法&#xff0c;自然语言处理&#xff08;NLP&#xff09;的研究近年来呈现出快速传播和广泛采用。鉴于 NLP 的快速发展&#xff0c;获得该领域的概述并对其进行维护是很困难的。…

查看cuda型号.

查看cuda型号.cmd打开命令行:执行命令:nvidia-smi这里展示CUDA的版本号是与当前 GPU 驱动(driver)程序兼容的 CUDA 运行时版本。这是驱动程序支持的最高 CUDA 版本,并不是系统上安装的 CUDA的版本!!!!!!!!!!!!!!!…

AI元人文系列:透明推理者——下一代大模型架构设计

AI元人文系列:透明推理者——下一代大模型架构设计 引言:从“智能工具”到“思维伙伴” 人工智能正站在新的十字路口。当前的大模型能够创作诗歌、解答难题、生成代码,却无法清晰回答一个简单却关键的问题:“你为什…

个人随笔

设计网页时,如果遇到float 一定要注意下面的元素要 clear,不然会有bug

专做电器的网站怎么开网站做站长

利用图扑三维可视化技术展示园区在不同时间段的变化&#xff0c;提供全景漫游体验&#xff0c;帮助用户全方位感受和理解园区环境&#xff0c;实现智能化管理与优化。

Fedora Atomic Desktops

https://docs.fedoraproject.org/en-US/emerging/ Fedora Atomic DesktopsFedora Silverblue Fedora Silverblue is an atomic desktop operating system featuring the GNOME desktop, a beautiful, high-quality des…

完整教程:【论文阅读】具身人工智能:从大型语言模型到世界模型

完整教程:【论文阅读】具身人工智能:从大型语言模型到世界模型pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "C…

Android达成RecyclerView粘性头部效果,模拟微信账单列表的月份标题平移

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

实用指南:【C语言】char * 、char [ ]、const char * 和 void *的使用以及区别

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

常德网站优化公司东莞大岭山天气预报

【我是谁】 1.学历&#xff1a;22届双非本科校企合作&#xff08;软外&#xff0c;软件工程服务外包&#xff09;&#xff0c;编程课大部分是印度的NIIT老师上课&#xff0c;印式英语一点儿听不懂。。。所以大学全都自学的&#xff0c;和非科班的也没什么区别和优势&#xff0c…

PowerShell注意点

$()和${}的区别: $()表示命令替换,将括号内的命令执行后得到的输出作为值。 例如,$(ls)将会执行ls命令后得到当前目录下的文件列表作为值。 ${}表示变量替换,将大括号内的变量的值作为值。 例如,${a}将取变量a的值…

自动化脚本的自动化执行实践 - 详解

自动化脚本的自动化执行实践 - 详解2025-10-03 17:36 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !imp…

做商业网站的服务费维护费直播型网站开发

在Kotlin中&#xff0c;注解&#xff08;Annotations&#xff09;是一种用于在程序代码中添加元数据的特殊标记。它们提供了对代码的描述性信息&#xff0c;但本身并不会影响程序的运行。注解可以应用于类、方法、属性等程序元素上&#xff0c;用于提供关于这些元素的额外信息。…

m3u8转mp4软件中文版推荐与使用指南

近年来,随着在线视频的普及,m3u8格式的流媒体文件变得越来越常见。不少用户希望将m3u8文件转换为通用的mp4格式,便于本地保存、播放或分享。那么,选择一款好用的m3u8转mp4软件中文版,就成了很多小伙伴的需求。下面…

Unity简易事件分发器

一、EventFunctionusing System; namespace EventCore {public struct EventFunction{public object _caller;public Action _action;}public struct EventFunction<T>{public object _caller;public Action<…

react怎么做pc网站外贸soho建站

本文给大家整理了腾讯视频网页下载_腾讯视频怎么下载视频方面的内容。腾讯视频独播剧质量还是可以的&#xff0c;比较给力的是腾讯视频大量买入了老剧的版权&#xff0c;不乏一些比较经典的港剧&#xff0c;还把这些老剧修复了。腾讯视频播放器是一款支持多种音视频格式的主流播…

实用指南:1、docker入门简介

实用指南:1、docker入门简介pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco"…