您现在的位置是:网站首页> 编程资料编程资料
vue的h5日历组件实现详解_vue.js_
2023-01-17
887人已围观
简介 vue的h5日历组件实现详解_vue.js_
本文实例为大家分享了vue的h5日历组件实现代码,供大家参考,具体内容如下
日历样式自定义


日历组件
{{dateTop}} {{tag}}{{item.id}}
calendar.js 是生成月份盘,月数多少天的逻辑
import moment from "moment"; export default { // 当某月的天数 getDaysInOneMonth(date) { const year = date.getFullYear(); const month = date.getMonth() + 1; const d = new Date(year, month, 0); return d.getDate(); }, // 向前空几个 getMonthweek(date) { const year = date.getFullYear(); const month = date.getMonth() + 1; const dateFirstOne = new Date(year + '/' + month + '/1'); return this.sundayStart ? dateFirstOne.getDay() == 0 ? 7 : dateFirstOne.getDay() : dateFirstOne.getDay() == 0 ? 6 : dateFirstOne.getDay() - 1; }, /** * 获取当前日期上个月或者下个月 */ getOtherMonth(date, str = 'nextMonth') { const timeArray = this.dateFormat(date).split('/'); const year = timeArray[0]; const month = timeArray[1]; const day = timeArray[2]; let year2 = year; let month2; if (str === 'nextMonth') { month2 = parseInt(month) + 1; if (month2 == 13) { year2 = parseInt(year2) + 1; month2 = 1; } } else { month2 = parseInt(month) - 1; if (month2 == 0) { year2 = parseInt(year2) - 1; month2 = 12; } } let day2 = day; const days2 = new Date(year2, month2, 0).getDate(); if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = '0' + month2; } if (day2 < 10) { day2 = '0' + day2; } const t2 = year2 + '/' + month2 + '/' + day2; return new Date(t2); }, // 上个月末尾的一些日期 getLeftArr(date) { const arr = []; const leftNum = this.getMonthweek(date); const num = this.getDaysInOneMonth(this.getOtherMonth(date, 'preMonth')) - leftNum + 1; const preDate = this.getOtherMonth(date, 'preMonth'); // 上个月多少开始 for (let i = 0; i < leftNum; i++) { const nowTime = preDate.getFullYear() + '/' + (preDate.getMonth() + 1) + '/' + (num + i); arr.push({ id: num + i, date: nowTime, isToday: false, isPreDay:false, otherMonth: 'preMonth', }); } return arr; }, // 下个月末尾的一些日期 getRightArr(date) { const arr = []; const nextDate = this.getOtherMonth(date, 'nextMonth'); const leftLength = this.getDaysInOneMonth(date) + this.getMonthweek(date); const _length = 7 - leftLength % 7; for (let i = 0; i < _length; i++) { const nowTime = nextDate.getFullYear() + '/' + (nextDate.getMonth() + 1) + '/' + (i + 1); arr.push({ id: i + 1, date: nowTime, isToday: false, isPreDay:false, otherMonth: 'nextMonth', }); } return arr; }, // format日期 dateFormat(date) { date = typeof date === 'string' ? new Date(date.replace(/-/g, '/')) : date; return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate(); }, // 获取某月的列表不包括上月和下月 getMonthListNoOther(date) { const arr = []; const num = this.getDaysInOneMonth(date); const year = date.getFullYear(); const month = date.getMonth() + 1; const toDay = this.dateFormat(new Date()); console.log(toDay,'今日日期的格式化'); var curDate = new Date(); var preDate = Date.parse(new Date(curDate.getTime() - 24 * 60 * 60 * 1000)); //前一天 const preDay = this.dateFormat(moment(preDate).format('YYYY-MM-DD')); console.log(preDay,'前一日日期的格式化'); for (let i = 0; i < num; i++) { const nowTime = year + '/' + month + '/' + (i + 1); arr.push({ id: i + 1, date: nowTime, isToday: toDay === nowTime, isPreDay: false, otherMonth: 'nowMonth', }); } // console.log(arr,'月份日期') return arr; }, // 获取某月的列表 用于渲染 getMonthList(date) { return [ ...this.getLeftArr(date), ...this.getMonthListNoOther(date), ...this.getRightArr(date) ]; }, // 默认是周一开始 sundayStart: false, };组件的导出
// index.js import CalendarDemo from './calendar.vue'; export default CalendarDemo;
组件的使用
日周提示: 本文由神整理自网络,如有侵权请联系本站删除!
本站声明:
1、本站所有资源均来源于互联网,不保证100%完整、不提供任何技术支持;
2、本站所发布的文章以及附件仅限用于学习和研究目的;不得将用于商业或者非法用途;否则由此产生的法律后果,本站概不负责!
相关内容
- React中使用react-file-viewer问题_React_
- Vue报错Syntax Error:TypeError: this.getOptions is not a function的解决方法_vue.js_
- JavaScript队列数据结构详解_javascript技巧_
- 微信小程序实现tab点击切换_javascript技巧_
- 降低vue-router版本的2种解决方法实例_vue.js_
- 一文让你快速了解JavaScript栈_javascript技巧_
- 从EFCore上下文的使用到深入剖析DI的生命周期最后实现自动属性注入_实用技巧_
- 在Asp.Net Core中使用ModelConvention实现全局过滤器隔离_实用技巧_
- Automation服务器不能创建对象的多种解决办法_实用技巧_
- .NET Core使用HttpClient进行表单提交时遇到的问题_实用技巧_
点击排行
本栏推荐
![]()
