Uniapp-小程序简单的时间选择组件-年月日时分

news2024/10/6 2:26:41

文章目录

  • 前言
  • 一、组件效果
  • 二、组件代码
    • 使用
  • 总结


前言

uniapp小程序开发系列。本文实现一个简单时间选择控件。uniapp用个心仪时间控件真的麻烦。官方给得要么年月日,要么时分。产品只要年月日时分。这该怎么玩。旧项目没有引入ui框架,我也不想去引入,不然高低整一个vant 小程序版的。

uniapp生态中,有个插件市场,找了一通,是有时间控件。但是引入也是麻烦。没必要。下文基于网友给的案例再稍微改了改。对了 这是vue2 的 。


一、组件效果

二、组件代码

时间控件的实现代码,区分了闰年平年。需要秒的话,可以自行修改。

<template>
  <div class="date-time" v-if="visible">
    <div class="mask" @click.stop="close" />
    <div class="box">
      <div class="header">
        <div class="determine" @click.stop="close">取消</div>
        <div class="determine" @click.stop="confirm" :style="{color: themes.theme1 || '#FF4E09',}">确定</div>
      </div>

      <picker-view  :indicator-style="indicatorStyle" :value="value" @change="bindChange"
        class="picker-view">
        <picker-view-column>
          <view class="item" v-for="(item,index) in years" :key="index">{{item}}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item,index) in months" :key="index">{{item}}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item,index) in days" :key="index">{{item}}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item,index) in hours" :key="index">{{item}}</view>
        </picker-view-column>
        <picker-view-column>
          <view class="item" v-for="(item,index) in minutes" :key="index">{{item}}</view>
        </picker-view-column>
      </picker-view>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        years: [],
        year: null,
        months: [],
        month: null,
        days: [],
        day: null,
        hours: [],
        hour: null,
        minutes: [],
        minute: null,
        value: [],
        indicatorStyle: `height: 50px;`,
        timeValue: ''
      }
    },
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      themes: {
        type: Object,
        default() {
          return {};
        },
      },
      interviewTime: {
        type: String,
        default() {
          return '';
        },
      }
    },
    mounted() {
      this.init();
    },
    watch: {
      visible: {
        handler(newValue, oldValue) {
          if (newValue) {
            if (this.interviewTime) {
              this.init(this.interviewTime);
            }
          }
        },
        immediate: false,
        deep: true
      }
    },
    methods: {
      init(interviewTime) {
        const date = interviewTime ? new Date(interviewTime) : new Date();
        const years = []
        const year = date.getFullYear()
        const months = []
        const month = date.getMonth() + 1
        const days = []
        const day = date.getDate()
        const hours = []
        const hour = date.getHours()
        const minutes = []
        const minute = date.getMinutes()

        let isDay = 30;
        if (month == 2) {
          if((year%4==0 && year%100!=0)||(year%400==0)){
            console.log('闰年')
            isDay = 29;
          }else {
            isDay = 28;
             console.log('平年')
          }
        } else if ([1,3,5,7,8,10,12].includes(month)) {
          isDay = 31;
        } else {
          isDay = 30;
        }

        for (let i = date.getFullYear(); i <= date.getFullYear()+2; i++) {
          years.push(i)
        }
        for (let i = 1; i <= 12; i++) {
          months.push(i)
        }
        for (let i = 1; i <= isDay; i++) {
          days.push(i)
        }
        for (let i = 0; i <= 23; i++) {
          if (i < 10) {
            hours.push('0' + i)
          } else {
            hours.push(i)
          }
        }
        for (let i = 0; i <= 59; i++) {
          if (i < 10) {
            minutes.push('0' + i)
          } else {
            minutes.push(i)
          }
        }

        this.years = years
        this.year = year
        this.months = months
        this.month = month
        this.days = days
        this.day = day
        this.hours = hours
        this.hour = hour
        this.minutes = minutes
        this.minute = minute
        this.value = [0, month-1, day-1, hour, minute]
      },
      bindChange: function (e) {
        const val = e.detail.value
        console.log('日期变化',val)
        let year = this.years[val[0]] 
        let isDay = 30, days = [];
        if (val[1]+1 == 2) {
            if((val[0]%4==0 && year%100!=0)||(year%400==0)){
            console.log('闰年')
            isDay = 29;
          }else {
            isDay = 28;
             console.log('平年')
          }
        } else if ([1,3,5,7,8,10,12].includes(val[1]+1)) {
          isDay = 31;
        } else {
          isDay = 30;
        }

        for (let i = 1; i <= isDay; i++) {
          days.push(i)
        }
        this.days = days;
        this.year = this.years[val[0]]
        this.month =  this.months[val[1]]
        this.day = this.days[val[2]]
        this.hour = this.hours[val[3]]
        this.minute = this.minutes[val[4]]
      },
      // 补0
      padZeroStr(originStr){
         if(+originStr < 10){
            return  String(originStr).padStart(2,'0')
         }
         return originStr + ''
      },
      close(){
        this.$emit('update:visible', false);
      },
      confirm() {
        let monthStr = this.padZeroStr(this.month)
        let dayStr = this.padZeroStr(this.day)
        let hourStr = this.padZeroStr(this.hour)
        let minuteStr = this.padZeroStr(this.minute)
        this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
        this.$emit('confirmPickDate', this.timeValue);
        this.$emit('update:visible', false);
      },
    },
  }
</script>
<style lang="scss" scoped>
  .date-time {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    z-index: 99999;
    .mask {
      position: absolute;
      top: 0;
      background: rgba(0, 0, 0, 0.5);
      width: 100%;
      height: 100vh;
    }
    .box {
      position: absolute;
      width: 100%;
      bottom: 0;
      background-color: #fff;
      border-radius: 20rpx 20rpx 0 0;
      overflow: hidden;
      height: 600rpx;
      .header {
        height: 88rpx;
        padding: 0 30rpx;
        border-bottom: 1rpx solid #e5e5e5;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .determine {
          color: #333333;
          font-size: 32rpx;
          font-family: PingFang SC, PingFang SC-Regular;
          font-weight: 400;
        }
      }
      .picker-view {
        width: 100%;
        height: 400rpx;
      }
      .item {
        height: 100rpx;
        line-height: 100rpx;
        align-items: center;
        justify-content: center;
        text-align: center;
        font-size: 32rpx;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
      }
    }
  }
</style>

使用

在你需要的文件引入组件。

<template>
  <view class="example-body" @click="showDatePickClick">
                       <view class="uni-input" style="text-align: right;" >{{interviewTime.substring(0,16)}}</view>
			           <my-date-picker  :visible.sync="visible" :interviewTime="interviewTime" @confirmPickDate="confirmPickDate" />
		            </view>
</template>
<script>
import myDatePicker from '../components/date-picker.vue';

export default {
    name: "XXXX-XX",
    props: {
        address: Array
    },
    components: {
        myDatePicker
    },
    data() {
       
        return {
            interviewTime:'',
            visible:false
        }
    },
    mounted(){
        this.interviewTime = this.getDate()
    },
   
    methods: {
        confirmPickDate(dateStr){
           console.log('confirmPickDate',dateStr)
           this.interviewTime = dateStr
           this.$emit("getPickDate", dateStr);
        },
        showDatePickClick(){
            console.log('showDatePickClick')
            this.visible = true
        },
        getDate(type) {
            const date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();

            month = month > 9 ? month : '0' + month;
            day = day > 9 ? day : '0' + day;
            return `${year}/${month}/${day} 00:00:00`;
        }
    }
}
</script>

<style scoped>
.example-body {
        width: 270px;
        height: 30px;
		background-color: #fff;
	}
</style>

总结

以上就是今天要讲的内容,本文讨论了用uniapp 实现小程序时间选择组件 仅仅支持 年月日时分 的简单版本。

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

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

相关文章

调查居民幸福指数的社会实践报告

引言 在快速发展的现代社会中&#xff0c;居民的幸福感成为了衡量一个社区、一个城市乃至一个国家综合实力的重要指标之一。幸福指数不仅反映了居民的生活质量和满意度&#xff0c;也是政府制定和调整政策&#xff0c;提升民众生活水平的重要依据。因此&#xff0c;本次社会实践…

【Python_Zebra斑马打印机编程学习笔记(二)】基于BarTender将btw文件转换为zpl文件

基于BarTender将btw文件转换为zpl文件 基于BarTender将btw文件转换为zpl文件前言一、BarTender1、BarTender 介绍2、BarTender 安装 二、导出 ZPL 文件1、导出 ZPL 文件步骤2、Zebra 打印机驱动安装 基于BarTender将btw文件转换为zpl文件 前言 本文介绍如何基于 BarTender 软…

C语言特殊函数

静态函数 背景知识&#xff1a;普通函数都是跨文件可见的&#xff0c;即在文件 a.c 中定义的函数可以在 b.c 中使用。 静态函数&#xff1a;只能在定义的文件内可见的函数&#xff0c;称为静态函数。 语法 staitc void f(void) // 在函数头前面增加关键字 static &#xff…

J7 - 对于ResNeXt-50算法的思考

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 | 接辅导、项目定制 J6周有一段代码如下 思考过程 首先看到这个问题的描述&#xff0c;想到的是可能使用了向量操作的广播机制然后就想想办法验证一下&…

Element使用Message消息提示

Element使用Message消息提示 一、导入Element1、npm 安装2、引入 Element3、实现代码4、效果 一、导入Element 1、npm 安装 推荐使用 npm 的方式安装 npm i element-ui -S2、引入 Element 在 main.js 中写入以下内容 import ElementUI from element-ui; import element-ui…

FFmpeg解析之avformat_find_stream_info函数

avformat_find_stream_info 的主要作用就是&#xff1a;解析媒体文件并获取相关的流信息 整体的逻辑如下图所示&#xff1a; /*** Read packets of a media file to get stream information. This* is useful for file formats with no headers such as MPEG. This* function…

现货黄金中短线投资该怎么做?

要明确什么是现货黄金的中短线投资&#xff0c;中短线投资是指在短期内&#xff08;一般为几天至几周&#xff09;对现货黄金进行买卖操作&#xff0c;以期获得收益的投资方式。相较于长线投资&#xff0c;中短线投资的风险相对较大&#xff0c;但同时收益也更为可观。那么&…

算法题目中图和树的存储

邻接表的方式存储图和树 这就是邻接表&#xff0c;就是将每个结点的孩子结点用链表表示出来&#xff0c;再将所有结点以数组形式连起来。 存储树和图我们需要三个数组&#xff0c;h[N], e[N], ne[N],分别表示邻接表&#xff0c;结点值&#xff0c;结点的next值&#xff0c;h[i…

Zookeeper简介及选举机制

1.概述 Zookeeper是一个开源的&#xff0c;分布式的&#xff0c;为分布式框架&#xff08;如下图中的Hadoop和Hive&#xff09;提供协调服务的Apache项目。 工作机制&#xff1a;基于观察者设计模式的分布式服务管理框架&#xff0c;负责存储和管理数据&#xff0c;接受观察者…

Set集合(Java) 及底层原理

SET<E>是一个接口&#xff0c;添加的元素是无序的&#xff1a;添加数据的顺序和获取出的数据顺序不一致&#xff1b;不重复&#xff0c;无索引。 实现类&#xff1a; 1.HashSet&#xff1a;无序不重复无索引 2.LinkedHashSet&#xff1a;有序不重复无索引 3.TreeSet&…

最佳 PDF 转 Word 转换器软件,可实现无缝转换

如今&#xff0c;PDF文件格式因其高安全性而被计算机用户所熟悉&#xff0c;这使得无法直接编辑内容。因此&#xff0c;每当用户需要复制内容时&#xff0c;都会遇到很多困难。在这里将介绍了一些可以让您将 PDF 转换为 Word 的工具。 借助高效、免费的 PDF 转 Word 转换器软件…

离散数学(一) 集合

属于关系 表示 枚举法&#xff1b; 叙述法&#xff1b; 文氏图法 基数 空集 全集 全集是相对唯一的 相等关系 有相同元素看作一个元素 包含关系 幂集 集合运算 并集 交集 补集 差集 对称差集 定理 可数集合与不可数集合 自然数集 等势 如果存在集合A到集合B的双射(又称一一…

PostgreSQL如何使用UUID

离线安装时&#xff0c;一般有四个包&#xff0c;都安装的话&#xff0c;只需要开启uuid的使用即可&#xff0c;如果工具包(即 postgresql11-contrib&#xff09;没有安装的话&#xff0c;需要单独安装一次&#xff0c;再进行开启。 开启UUID方法 下面介绍一下如何开启&#…

分布式事务Seata的使用详解

一、事务概述 事务指的就是一个操作单元&#xff0c;在这个操作单元中的所有操作最终要保持一致的行为&#xff0c;要么所有操作 都成功&#xff0c;要么所有的操作都被撤销。简单地说&#xff0c;事务提供一种“要么什么都不做&#xff0c;要么做全套”机制。 1.1.本地事物 …

Linux基础知识——命令行模式下命令的执行

文章目录 Linux基础知识——命令行模式下命令的执行开始执行Linux命令Linux基础命令的操作常用Linux命令行操作按键Linux输出错误信息查看 Linux系统在线帮助--help选项man命令info命令其他有用的文件文档百度搜索 文本编辑器&#xff1a;nanonano启动&#xff01; 正确关机方法…

自动化行业文件数据\资料防泄密软件——天锐绿盾|@德人合科技

天锐绿盾是一款自动化行业文件数据防泄密软件&#xff0c;由德人合科技提供。该软件采用动态加解密技术&#xff0c;能够有效防止公司内部数据泄密&#xff0c;同时支持各种文件格式加密&#xff0c;如CAD、OFFICE、PDF、图纸等。 PC端&#xff1a;https://isite.baidu.com/sit…

第1讲-introduction

计算机组成与结构简介 计算机的基本组成 计算机的层次结构

从零开始学逆向:理解ret2syscall

1.题目信息 链接&#xff1a;https://pan.baidu.com/s/19ymHlZZmVGsJHFmmlwww0w 提取码&#xff1a;r4el 首先checksec 看一下保护机制 2.原理 ret2syscall 即控制程序执行系统调用来获取 shell 什么是系统调用&#xff1f; 操作系统提供给用户的编程接口是提供访问操作系统…

JavaScript原型继承与面向对象编程思想

原型继承与面向对象编程思想 在JavaScript中&#xff0c;原型(prototype)、构造函数(constructor)和实例对象(instance)是面向对象编程中的重要概念&#xff0c;并且它们之间存在着紧密的关系。 原型(prototype)&#xff1a;原型是JavaScript中对象之间关联的一种机制。每个Ja…

ElasticSearch之bool多条件查询

写在前面 在实际的业务场景中&#xff0c;不可能只是简单的单值查询 &#xff0c;更多的是n个条件的综合查询&#xff0c;就像下面的搜索&#xff1a; 针对这种场景我们就需要依赖于bool查询了&#xff0c;本文就一起来看下这部分的内容。 1&#xff1a;bool查询介绍 bool查…