uniapp、小程序自定义选择日、周、月、季、年的时间选择器组件

这篇具有很好参考价值的文章主要介绍了uniapp、小程序自定义选择日、周、月、季、年的时间选择器组件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

uni-app 时间季度展示,uni-app,小程序

本组件用到了uni-ui的uni-popup弹窗组件

废话不多说直接上代码

代码分享

<template>
	<view class="content">
		<uni-popup ref="popup" type="bottom">
			<view class="zdp-pop">
				<view class="top-cont">
					<view class="close-cont" @click="close(1)">
						取消
					</view>
					<view class="text-info">
						请选择时间
						<view class="">
							{{selInfo}}
						</view>
					</view>
					<view class="save-cont" @click="close(2)">
						确认
					</view>
				</view>
				<view class="tabs-cont">
					<view class="tab-list" v-for="item in tabs" :class="item.value==activeIdx?'active':''"
						@click="handleTabs(item.value)">
						{{item.label}}
					</view>
				</view>
				<view class="date-cont">
					<picker-view :value="value" @change="handleChange" class="picker-view">
						<picker-view-column v-for="obj in SelArr">
							<view class="item" v-for="(item,index) in obj.child" :key="index">{{item}}{{obj.value}}
							</view>
						</picker-view-column>
					</picker-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		whichWeek
	} from '@/utils/getWeek.js'
	import {
		getMonthStartEnd
	} from '@/utils/getMonth.js'
	import {
		getQuarterDates
	} from '@/utils/getQuarter.js'
	export default {
		props: {
			type: {
				type: String,
				default: '日',
				validator(value, props) {
					return ['日', '月', '周', '年', '季'].includes(value)
				}
			},
			time: {
				type: String, //'2024-1-1' '2024-2' '2024-2' '2024-2' '2024

			}
		},
		data() {
			return {
				tabs: [{
						label: '日',
						value: '日'
					},
					{
						label: '周',
						value: '周',
					},
					{
						label: '月',
						value: '月',
					},
					{
						label: '季',
						value: '季',
					},
					{
						label: '年',
						value: '年',
					}
				],
				activeIdx: '日',
				value: [], //选择的值,要设置默认值
				dateArr: [],
				day: 18,
				week: 1,
				month: 1, //
				quarter: 2,
				years: 24,
				SelArr: [],
				range: '',
				zdpdate: ''
			}
		},
		computed: {
			selInfo() {
				let str = ''
				let {
					activeIdx,
					years,
					day,
					month,
					week,
					quarter
				} = this
				if (activeIdx == '日') {
					str = `20${years}年 ${month}月 ${day}日`
				} else if (activeIdx == '周') {
					str = `20${years}年 第${week}周`
				} else if (activeIdx == '月') {
					str = `20${years}年 ${month}月`
				} else if (activeIdx == '季') {
					str = `20${years}年 ${quarter}季`
				} else if (activeIdx == '年') {
					str = `20${years}年`
				}
				return str
			}
		},
		created() {

		},
		methods: {
			// 
			handleProps() {
				let arr = []
				arr=this.time.split('-')
				if(this.type=='日'){
					this.month=arr[1]-0
					this.day=arr[2]-0
				}else if(this.type=='周'){
					this.week=arr[1]-0
				}else if(this.type=='季'){
					this.quarter=arr[1]-0
				}else if(this.type=='月'){
					this.month=arr[1]-0
					this.quarter = this.handleQuarter(this.month)
				}
				for (let i = 0; i < arr.length; i++) {
					if (i == 0) {
						arr[0]=arr[0].slice(2)
						arr[0]-=0
						this.years=arr[0]
					} else {
						arr[i]--
					}
				}
				this.value = arr
			},
			// 初始化
			newDate() {
				// this.activeIdx=this.type
				const date = new Date()
				this.day = date.getDate()
				this.years = date.getFullYear()
				this.week = this.getYearWeek(date)
				this.quarter = this.handleQuarter(this.month)
				this.month = date.getMonth() + 1
				this.handleTabs(this.type)
				if (this.time) {
					this.handleProps()
				}
				this.initArr()
			},
			initArr() {
				const date = new Date()
				let arr = []
				let {
					activeIdx,
				} = this
				let day = []
				let week = []
				let month = []
				let quarter = []
				let years = []
				for (let i = 2000; i <= date.getFullYear(); i++) {
					years.push(i)
				}
				for (let i = 1; i <= 12; i++) {
					month.push(i)
				}
				let weekArr = whichWeek(`20${this.years}`)
				for (let i = 1; i <= weekArr.length; i++) {
					week.push(i)
				}
				for (let i = 1; i <= 4; i++) {
					quarter.push(i)
				}

				if (activeIdx == '日') {
					let num = this.getMonthDays(this.years, this.month)
					for (let i = 1; i <= num; i++) {
						day.push(i)
					}
					arr = [{
						value: "年",
						child: years
					}, {
						value: "月",
						child: month
					}, {
						value: "日",
						child: day
					}]
					this.zdpdate = `20${this.years}-${this.month}-${this.day}`
					this.range = `20${this.years}-${this.month}-${this.day}`
				} else if (activeIdx == '月') {
					arr = [{
						value: "年",
						child: years
					}, {
						value: "月",
						child: month
					}]
					this.zdpdate = `20${this.years}-${this.month}`
					this.range = getMonthStartEnd(this.zdpdate)
				} else if (activeIdx == '年') {
					arr = [{
						value: "年",
						child: years
					}]
					this.zdpdate = `20${this.years}`
					this.range = `20${this.years}-1-31/20${this.years}-12-31`
				} else if (activeIdx == '周') {
					arr = [{
						value: "年",
						child: years
					}, {
						value: "周",
						child: week
					}]
					this.zdpdate = `20${this.years}年 第${this.week}周`
					let wk = weekArr[this.week - 1]
					this.range = `${wk.year}-${wk.month}-${wk.date}` + '/' +
						`${wk.last.year}-${wk.last.month}-${wk.last.date}`
				} else if (activeIdx == '季') {
					arr = [{
						value: "年",
						child: years
					}, {
						value: "季度",
						child: quarter
					}]
					this.zdpdate = `20${this.years}年 ${this.quarter}季度`
					let obj = getQuarterDates(`20${this.years}`, this.quarter)
					this.range = obj.startDate + '/' + obj.endDate
				}
				this.SelArr = arr
			},
			open() {
				this.newDate()
				this.$refs.popup.open('bottom')
			},
			close(num) {
				if (num == 1) {
					this.newDate()
					this.handleEmit()
				} else {
					this.handleEmit()
				}
				this.$refs.popup.close()
			},
			handleEmit() {
				let {
					zdpdate,
					range,
				} = this
				this.$emit('submit-date', {
					zdpdate,
					range,
					type:this.activeIdx
					})
			},
			
			handleTabs(val) {
				this.activeIdx = val
				let {
					activeIdx,
					years,
					month,
					day,
					quarter,
					week
				} = this
				if (activeIdx == '日') {
					this.value = [years, month - 1, day - 1]
				} else if (activeIdx == '周') {
					this.value = [years, week - 1]
				} else if (activeIdx == '月') {
					this.value = [years, month - 1]
				} else if (activeIdx == '季') {
					this.value = [years, quarter - 1, ]
				} else if (activeIdx == '年') {
					this.value = [years]
				}
				console.log(this.value);
				this.initArr()
			},
			// 选择器选择
			handleChange(e) {
				let {
					value
				} = e.detail
				let {
					activeIdx
				} = this
				this.years = value[0]
				if (activeIdx == '日') {
					this.month = value[1] + 1 //月
					this.day = value[2] + 1 //日
				} else if (activeIdx == '周') {
					this.week = value[1] + 1 //周
				} else if (activeIdx == '季') {
					this.quarter = value[1] + 1
				} else if (activeIdx == '月') {
					this.month = value[1] + 1 //月
				}
				this.initArr()
			},
			//获取当前月的天数
			getMonthDays(year, month) {
				var thisDate = new Date(year, month, 0);
				return thisDate.getDate();
			},
			getYearWeek(endDate) {
				//本年的第一天
				let beginDate = new Date(endDate.getFullYear(), 0, 1);
				//星期从0-6,0代表星期天,6代表星期六
				let endWeek = endDate.getDay();
				if (endWeek == 0) endWeek = 7;
				let beginWeek = beginDate.getDay();
				if (beginWeek == 0) beginWeek = 7;
				//计算两个日期的天数差
				let millisDiff = endDate.getTime() - beginDate.getTime();
				let dayDiff = Math.floor((millisDiff + (beginWeek - endWeek) * (24 * 60 * 60 * 1000)) / 86400000);
				return Math.ceil(dayDiff / 7) + 1;
			},
			handleQuarter(month) {
				const oneQuarter = [1, 2, 3]
				const twoQuarter = [4, 5, 6]
				const threeQuarter = [7, 8, 9]
				let num
				if (oneQuarter.includes(month)) {
					num = 1
				} else if (twoQuarter.includes(month)) {
					num = 2
				} else if (threeQuarter.includes(month)) {
					num = 3
				} else {
					num = 4
				}
				return num
			}
		}
	}
</script>

<style lang="scss" scoped>
	* {
		box-sizing: border-box;
	}

	.zdp-pop {
		box-sizing: border-box;
		padding: 24rpx;
		width: 100%;
		height: 750rpx;
		background-color: #fff;

		.top-cont {
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 32rpx;

			.close-cont {
				color: #aaa;
				letter-spacing: 6rpx;
			}

			.text-info {
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			.save-cont {
				background: #409EFF;
				color: #fff;
				width: 100rpx;
				height: 60rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 6rpx;
			}
		}

		.tabs-cont {
			box-sizing: border-box;
			height: 80rpx;
			background-color: #F3F5F9;
			display: flex;
			align-items: center;
			border-radius: 4rpx;
			padding: 0 10rpx;

			.tab-list {
				min-width: 20%;
				display: flex;
				height: 60%;
				flex: 1;
				justify-content: center;
				border-radius: 4rpx;

				&.active {
					background-color: #fff;
				}
			}
		}

		.date-cont {
			height: calc(750rpx - 180rpx);

			.picker-view {
				width: 100%;
				height: 100%;

				.item {
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.uni-picker-view-indicator {
					height: 100rpx;
				}
			}
		}
	}
</style>

utils文件

@/utils/getMonth.js
//获取这个月的月初和月末
export function getMonthStartEnd(vars){
  var str = '';
  if(vars!=null&&vars!=''){
    var nyYear=vars.slice(0,4);
    var nyMonth=vars.slice(5,vars.length);
    var firstDay = new Date(nyYear,nyMonth-1);
    var lastDay = new Date(new Date(nyYear,nyMonth).valueOf()-60*60*1000*24);
    function datasFormat(d){
      var datetime=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
      return datetime;
    }
    str = datasFormat(firstDay) + "/" + datasFormat(lastDay)
  }
  return str
}
@/utils/getQuarter.js
export function getQuarterDates(year, quarter) {
	const startDate = new Date(year, (quarter - 1) * 3, 1);
	const endDate = new Date(year, quarter * 3, 0);

	const startDay = startDate.getDate();
	const endDay = endDate.getDate();

	const startMonth = startDate.getMonth() + 1;
	const endMonth = endDate.getMonth() + 1;

	const startYear = startDate.getFullYear();
	const endYear = endDate.getFullYear();

	const startDateString =
		`${startYear}-${startMonth < 10 ? '0' + startMonth : startMonth}-${startDay < 10 ? '0' + startDay : startDay}`;
	const endDateString =
		`${endYear}-${endMonth < 10 ? '0' + endMonth : endMonth}-${endDay < 10 ? '0' + endDay : endDay}`;

	return {
		startDate: startDateString,
		endDate: endDateString
	};
}

// // 示例用法:计算2024年第2季度的起止日期
// const quarterDates = getQuarterDates(2024,2);
// console.log(quarterDates.startDate); // 输出:2024-04-01
// console.log(quarterDates.endDate);   // 输出:2024-06-30
@/utils/getWeek.js
//时间戳转年月日  参数是秒的时间戳 函数返回一个对象 对象里有年 月 日
export function yearDay(long){
  var time = new Date(long * 1000)
  var year = time.getFullYear();
  var month = (time.getMonth()+1) < 10 ? '0' + (time.getMonth()+1) : (time.getMonth()+1);
  var date = time.getDate() < 10 ? '0' + time.getDate() : time.getDate() ;
  var yearday = {year,month,date}
  return yearday
}
//计算一年中的每一周都是从几号到几号
//第一周为1月1日到 本年的 第一个周日
//第二周为 本年的 第一个周一 往后推到周日
//以此类推 再往后推52周。。。
//如果最后一周在12月31日之前,则本年有垮了54周,反之53周
//12月31 日不论是周几,都算为本周的最后一天
//参数年份 ,函数返回一个数组,数组里的对象包含 这一周的开始日期和结束日期
export function whichWeek(year){
  var d = new Date(year, 0, 1);
  while (d.getDay() != 1) {
    d.setDate(d.getDate() + 1);
  }
  let arr = []
  let longnum = d.setDate(d.getDate())
  if(longnum > +new Date(year, 0, 1)){
    let obj = yearDay(+new Date(year, 0, 1) / 1000)
    obj.last = yearDay( longnum / 1000 - 86400)
    arr.push(obj)
  }
  let oneitem = yearDay(longnum / 1000)
  oneitem.last = yearDay( longnum / 1000 + 86400 * 6)
  arr.push(oneitem)
  var lastStr
  for(var i = 0 ; i<51 ;i++){
    let long = d.setDate(d.getDate() + 7)
    let obj = yearDay( long / 1000)
    obj.last = yearDay( long / 1000 + 86400 * 6)
    lastStr = long + 86400000 * 6
    arr.push(obj)
  }
  if(lastStr < +new Date(year + 1, 0, 1)){        
    let obj = yearDay(lastStr / 1000 + 86400)
    obj.last = yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400)
    arr.push(obj)
  }else{
    arr[arr.length-1].last = yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400)
  }
  return arr
}
// //例如 2012 年就跨了54周,也有很多是53周的
// let week2012 = whichWeek(2012)
// console.log(week2012)
// //调用完得到的是一个数组见下图

uniapp使用插件使用

https://ext.dcloud.net.cn/plugin?id=16387https://ext.dcloud.net.cn/plugin?id=16387

zdp-date-picker使用说明

<zdp-date-picker ref="zdpdate" type="月" time="2023-2" @submit-date="handleDate($event)"></zdp-date-picker>

属性参数说明:

ref用于控制picker的弹出与隐藏,组件内有open和close函数

this.$refs.zdpdate.open()
参数名 作用 类型 默认值
type 控制打开piker时tabs默认显示的位置 String
time 显示的默认日期 String 2023-1

type用于控制打开piker时显示的tabs;默认参数:日、周、月、季、年

time用于控制显示的默认日期;

type=“日” time="2024-1-23" 代表默认显示日、time代表选中日期2024年1月23日

type=“周” time=“2024-4” 代表默认显示周、time代表选中日期2024年第4周

type=“月” time=“2024-1” 代表默认显示月、time代表选中日期2024年1月

type=“季” time=“2024-1” 代表默认显示季、time代表选中日期2024年第1季度

type=“年” time=“2024” 代表默认显示年、time代表选中日期2024

事件返回参数说明:

事件名 作用
submit-date 用于接收组件返回的时间

submit-date 用于接收组件返回的时间

range 是选中日期的时间范围

zdpdate是具体的固定日期

type 是选中日期类型 日、周、月、季、年文章来源地址https://www.toymoban.com/news/detail-839763.html

handleDate(e){
    let {range,zdpdate,type}=e
    this.range=range
    this.zdpdate=zdpdate
    this.type=type
}

到了这里,关于uniapp、小程序自定义选择日、周、月、季、年的时间选择器组件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • uni-app编译成钉钉小程序,uViewUI组件样式失效,自定义组件样式失效

    公司项目需要把原本用 uinapp 开发的微信小程序想要在钉钉上也能使用,然后就打算用uniapp编译成钉钉小程序,看看是否有坑,不试不知道,一试吓一跳,官方没有提供钉钉的编译方式,按照网上文章的配置编译成钉钉小程序后,初步发现, uView-ui的icon没啦 ,然后样式和布局

    2024年02月11日
    浏览(42)
  • 使用原生小程序组件Picker自定义日期时间选择器

    1、 Picker简单介绍 可以看到Picker类型有5种, 具体可以查看微信开放文档 picker。 Picker(选择器)是一种常见的用户界面控件,用于从多个选项中选择一个或多个选项。在小程序中,Picker 是一种可用于选择日期、时间、地点等信息的组件。 小程序中的 Picker 组件提供了几种不

    2024年02月09日
    浏览(34)
  • uni-app-使用tkiTree组件实现树形结构选择

    前言 在实际开发中我们经常遇见树结构-比如楼层区域-组织架构-部门岗位-系统类型等情况 往往需要把这个树结构当成条件来查询数据,在PC端可以使用Tree,table,Treeselect等组件展示 在uni-app的内置组件中似乎没有提供这样组件来展示,但是是有第三方包tkiTree组件来解决这个

    2024年02月14日
    浏览(42)
  • uni-app自定义组件及拓展(uni-ui)组件的使用

    UniApp 是一个基于 Vue.js 的跨平台应用框架,可以用来开发同时运行在多个平台(如微信小程序、支付宝小程序、App等)的应用程序。在 UniApp 中,组件的使用与 Vue.js 中的组件使用基本类似,但需要考虑跨平台兼容性。 1. 创建组件文件 在 UniApp 项目中创建一个新的组件,通常

    2024年04月29日
    浏览(32)
  • 前端vue uni-app自定义精美海报生成组件

    在当前技术飞速发展的时代,软件开发的复杂度也在不断提高。传统的开发方式往往将一个系统做成整块应用,一个小的改动或者一个小功能的增加都可能引起整体逻辑的修改,从而造成牵一发而动全身的情况。为了解决这个问题,组件化开发逐渐成为了一种趋势。通过组件

    2024年02月14日
    浏览(44)
  • uni-app开发小程序使用uni.chooseMedia选择图片,安卓手机无法选择图片

    一、在小程序中,选择图片并上传,是一个很常见的功能; 二、最近在开发中,使用 uni.chooseMedia 来做选择图片功能时,苹果手机是正常的,安卓手机打不开!!!代码如下: 就这点屁代码,苹果手机可以正常打开选择图片的功能,安卓手机没法打开!!! !!!! 我百思

    2024年02月16日
    浏览(32)
  • 十、uni-app小程序时间戳

    源码 源码 引用的 MxDatePicker 源码

    2024年02月16日
    浏览(25)
  • uni-app自定义组件components导入失败或页面不显示文本等

            一般来说分三步走,如下图  如果引入不成功则考虑以下几个问题: 是 components 而非 component 导入后的命名方式采用驼峰命名法 检查需要引入的文件路径和文件名是否正确 如果还有问题,则采用第二种方式直接在 components 中引入、注册一体,引入直接使用 chan

    2024年02月16日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包