首页 > 其他分享 >element-ui 周、月、季、年的日期时间选择器

element-ui 周、月、季、年的日期时间选择器

时间:2023-07-24 17:57:44浏览次数:42  
标签:var element Date let new date ui data 选择器

日常做项目中经常会遇到根据周、月、季度、年的日期时间选择器,切换不同的时间时选择器也做出相对应的变化,并且获取相对的开始时间和结束时间。

效果如下:

 

 

1.先创建子组件--季度的日期选择器 

  1 <template>
  2   <div class="time_quarter">
  3     <mark style="position:fixed;top:0;bottom:0;left:0;right:0;background:rgba(0,0,0,0);z-index:999;" v-show="showSeason"
  4       @click.stop="showSeason = false"></mark>
  5     <el-input placeholder="请选择季度" v-model="showValue" style="width:100%;" class="elWidth" @focus="showSeason = true">
  6       <i slot="prefix" class="el-input__icon el-icon-date"></i>
  7     </el-input>
  8     <el-card class="box-card" v-show="showSeason" style="width:100%">
  9       <div slot="header" class="clearfix" style="text-align:center;padding:0">
 10         <button type="button" aria-label="前一年"
 11           class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left" @click="prev"></button>
 12         <span role="button" class="el-date-picker__header-label">{{ year }}年</span>
 13         <button type="button" aria-label="后一年" @click="next" :class="{ notallow: year == limitTime }"
 14           class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right"></button>
 15       </div>
 16       <div class="text item" style="text-align:center;">
 17         <el-button type="text" size="medium" style="width:40%;color: #606266;float:left;"
 18           @click="selectSeason(0)">第一季度</el-button>
 19         <el-button type="text" size="medium" style="float:right;width:40%;color: #606266;"
 20           @click="selectSeason(1)">第二季度</el-button>
 21       </div>
 22       <div class="text item" style="text-align:center;">
 23         <el-button type="text" size="medium" style="width:40%;color: #606266;float:left;"
 24           @click="selectSeason(2)">第三季度</el-button>
 25         <el-button type="text" size="medium" style="float:right;width:40%;color: #606266;"
 26           @click="selectSeason(3)">第四季度</el-button>
 27       </div>
 28     </el-card>
 29   </div>
 30 </template>
 31 <script>
 32 export default {
 33   props: {
 34     valueArr: {
 35       default: () => {
 36         return ['01-03', '04-06', '07-09', '10-12'];
 37       },
 38       type: Array,
 39     },
 40     getValue: {
 41       default: () => { },
 42       type: Function,
 43     },
 44     // 传入显示的时间
 45     defaultValue: {
 46       default: "",
 47       type: String,
 48     },
 49     // 限制的时间
 50     limitTime: {
 51       type: String,
 52       default: "",
 53       require: true,
 54     },
 55   },
 56   data() {
 57     return {
 58       showSeason: false,
 59       season: "",
 60       year: new Date().getFullYear(),
 61       showValue: ''
 62     };
 63   },
 64   computed: {},
 65   created() {
 66     if (this.defaultValue) {
 67       let value = this.defaultValue;
 68       let arr = value.split("-");
 69       this.year = arr[0].slice(0, 4);
 70  
 71       var myseason = arr[1];
 72       this.showValue = `${this.year}年${this.whitchQuarter(myseason)}季度`;
 73     }
 74     console.log("whitchQuarter", this.whitchQuarter(10));
 75   },
 76   watch: {
 77     defaultValue: function (value, oldValue) {
 78       let arr = value.split("-");
 79       this.year = arr[0].slice(0, 4);
 80       var myseason = arr[1];
 81       this.showValue = `${this.year}年${this.whitchQuarter(myseason)}季度`;
 82     },
 83   },
 84   methods: {
 85     // 根据输入的月份判断是哪一个季节
 86     whitchQuarter(month) {
 87       let quarter = "";
 88       month = Number(month);
 89       switch (month) {
 90         case 1:
 91         case 2:
 92         case 3:
 93           quarter = "1";
 94           break;
 95         case 4:
 96         case 5:
 97         case 6:
 98           quarter = "2";
 99           break;
100         case 7:
101         case 8:
102         case 9:
103           quarter = "3";
104           break;
105         case 10:
106         case 11:
107         case 12:
108           quarter = "4";
109           break;
110         default:
111           console.error("The entered time is incorrect");
112       }
113       return quarter;
114     },
115     one() {
116       this.showSeason = false;
117     },
118     prev() {
119       this.year = this.year * 1 - 1;
120     },
121     next() {
122       // 如果有时间限制的话会进行判断
123       if (this.limitTime == "") {
124         this.year = this.year * 1 + 1;
125       } else if (this.limitTime != "" && this.year < this.limitTime) {
126         this.year = this.year * 1 + 1;
127       }
128     },
129     // 季度时间判定
130     InitialTime(val) {
131       let num = "";
132       val = Number(val);
133       switch (val) {
134         case 1:
135           num = "01";
136           break;
137         case 2:
138           num = "04";
139           break;
140         case 3:
141           num = "07";
142           break;
143         case 4:
144           num = "10";
145           break;
146         default:
147           console.error("时间格式有误!");
148       }
149       return num;
150     },
151  
152     selectSeason(i) {
153       let that = this;
154       that.season = i + 1;
155       let arr = that.valueArr[i].split("-");
156       that.getValue(that.year + arr[0] + "-" + that.year + arr[1]);
157       that.showSeason = false;
158       this.showValue = `${this.year}年${this.season}季度`;
159       var formatValue = `${this.year}-${this.InitialTime(this.season)}`;
160       this.$emit("getTime", formatValue);
161     }
162   },
163 };
164 </script>
165 <style>
166 .notallow {
167   cursor: not-allowed;
168 }
169 .time_box {
170   position: relative;
171 }
172 .box-card {
173   position: absolute;
174   top: 40px;
175   /* left: 22px; */
176   padding: 0 3px 20px;
177   z-index: 9999;
178 }
179 .time_quarter {
180   width: 100%;
181  
182 }
183 .time_quarter .el-input--small .el-input__inner {
184   width: 82%;
185 }</style>

2.父组件

 

 

 

 

 以下是日期选择器切换时的时间换算

  1 //获取周的开始时间和结束时间
  2         getMonday(type, dates) {
  3             var now = new Date();
  4             var nowTime = now.getTime();
  5             var day = now.getDay();
  6             var longTime = 24 * 60 * 60 * 1000;
  7             var n = longTime * 7 * (dates || 0);
  8             if (type == "s") {
  9                 var dd = nowTime - (day - 1) * longTime + n;
 10             };
 11             if (type == "e") {
 12                 var dd = nowTime + (7 - day) * longTime + n;
 13             };
 14             dd = new Date(dd);
 15             var y = dd.getFullYear();
 16             var m = dd.getMonth() + 1;
 17             var d = dd.getDate();
 18             m = m < 10 ? "0" + m : m;
 19             d = d < 10 ? "0" + d : d;
 20             var day = y + "-" + m + "-" + d;
 21             return day;
 22         },
 23         //周
 24         changeTime(data) {
 25             let date = new Date(data);
 26             let y = date.getFullYear();
 27             let m = date.getMonth();
 28             let d = date.getDate();
 29             let week = date.getDay();
 30             let start = new Date(y, m, d - week + 1);
 31             let end = new Date(y, m, d - week + 7);
 32             this.queryParams.startTime = this.getCurrentTime(start, 0) + " 00:00:00";
 33             this.queryParams.endTime = this.getCurrentTime(end, 0) + " 23:59:59";
 34         },
 35         //月
 36         changeTimeM(data) {
 37             this.queryParams.startTime = this.getCurrentTime(data, 0) + " 00:00:00";
 38             this.queryParams.endTime = this.getCurrentTime(this.getCuurrentTimeM(data), 0) + " 23:59:59";
 39             console.log(this.queryParams)
 40         },
 41         //年
 42         changeTimeY(data) {
 43             this.queryParams.startTime = this.getCurrentTime(data, 0) + " 00:00:00";
 44             this.queryParams.endTime = this.getCurrentTime(this.getCuurrentTimeY(data), 0) + " 23:59:59";
 45             console.log(this.queryParams)
 46         },
 47         getCurrentTime(data, num) {
 48             let date = new Date(data)
 49             let Y = date.getFullYear()
 50             let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
 51             let D = date.getDate() + num < 10 ? ('0' + (date.getDate() + num)) : (date.getDate() + num);
 52             date = Y + '-' + M + '-' + D;
 53             return date
 54         },
 55         getCuurrentTimeM(data) {
 56             let date = new Date(data)
 57             let Y = date.getFullYear();
 58             let M = date.getMonth();
 59             return new Date(Y, M + 1, 0).toLocaleDateString();
 60         },
 61         getCuurrentTimeY(data) {
 62             let date = new Date(data)
 63             let Y = date.getFullYear();
 64             return new Date(Y, 11, 31).toLocaleDateString();
 65         },
 66         //获得某月的天数
 67         getMonthDays: function (nowYear, myMonth) {
 68             var monthStartDate = new Date(nowYear, myMonth, 1);
 69             var monthEndDate = new Date(nowYear, myMonth + 1, 1);
 70             var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
 71             return days;
 72         },
 73         //获得本季度的结束日期
 74         getQuarterEndDate(data) {
 75             let date = new Date(data);
 76             let nowYear = date.getFullYear();
 77             let M = date.getMonth();
 78             var quarterEndMonth = M + 2;
 79             var quarterStartDate = new Date(nowYear, quarterEndMonth, this.getMonthDays(nowYear, quarterEndMonth));
 80             return this.getAllDate(quarterStartDate, "yyyy-MM-dd");
 81         },
 82         //获得本季度的开始月份
 83         getQuarterStartMonth: function (nowMonth) {
 84             var quarterStartMonth = 0;
 85             if (nowMonth < 3) {
 86                 quarterStartMonth = 0;
 87             }
 88             if (2 < nowMonth && nowMonth < 6) {
 89                 quarterStartMonth = 3;
 90             }
 91             if (5 < nowMonth && nowMonth < 9) {
 92                 quarterStartMonth = 6;
 93             }
 94             if (nowMonth > 8) {
 95                 quarterStartMonth = 9;
 96             }
 97             return quarterStartMonth;
 98         },
 99         //获得日期
100         getAllDate: function (date, fmt) {
101             if (null == fmt || undefined == fmt || "" == fmt)
102                 fmt = "yyyy/MM/dd";
103             var date = new Date(date);
104             var o = {
105                 "M+": date.getMonth() + 1, //月份
106                 "d+": date.getDate(), //日
107             };
108             if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
109             for (var k in o)
110                 if (new RegExp("(" + k + ")").test(fmt))
111                     fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
112             return fmt;
113         },
114         getmgTime(data) {
115             let start = data + "-01";
116             this.queryParams.startTime = start + " 00:00:00";
117             let end = this.getQuarterEndDate(start);
118             this.queryParams.endTime = end + " 23:59:59";
119         },
120         watch: {
121             //监听内容
122             timeType() {
123                 if (this.timeType == 1) {//周
124                     this.value2 = new Date(new Date().valueOf());
125                     this.queryParams.startTime = this.getMonday("s", 0) + " 00:00:00";
126                     this.queryParams.endTime = this.getMonday("e", 0) + " 23:59:59";
127                 } else if (this.timeType == 2) {//月
128                     this.value2 = new Date().getFullYear() + '-' + (new Date().getMonth() + 1);
129                     this.changeTimeM(this.value2);
130                 } else if (this.timeType == 3) {//季度
131                     let date = new Date();
132                     let y = date.getFullYear();
133                     let M = date.getMonth();
134                     let quarterStartDate = new Date(y, this.getQuarterStartMonth(M), 1);
135                     let starts = this.getAllDate(quarterStartDate, "yyyy-MM-dd");
136                     let end = this.getQuarterEndDate(starts);
137                     this.queryParams.startTime = starts + " 00:00:00";
138                     this.queryParams.endTime = end + " 23:59:59";
139                 } else if (this.timeType == 4) {//年
140                     let date = new Date();
141                     let y = date.getFullYear();
142                     this.value2 = y.toString();
143                     this.changeTimeY(this.value2);
144                 }
145             }
146         },

 

标签:var,element,Date,let,new,date,ui,data,选择器
From: https://www.cnblogs.com/yuwenjing0727/p/17577898.html

相关文章

  • element table列表项展示省市区
    使用的省市区插件npminstallelement-china-area-data-S引用import{provinceAndCityData,regionData,provinceAndCityDataPlus,regionDataPlus,CodeToText,TextToCode}from'element-china-area-data'绑定formatter的方法<el-table-columnalign="center&qu......
  • 初级设计师必看的UI设计组件库
    UI组件库是一个可以理解为重复使用界面设计元素的集合,是一个文件库。UI组件库在整个系统中起着行为层面的作用,是团队内部设计师与开发者之间的横向合作,是保证产品产出一致的标准基础。即时设计资源社区提供丰富的组件库,包括不同行业、不同顶级互联网厂商的UI组件库!可以免费一键使......
  • 【后端面经-Java】String与StringBuffer与StringBuilder的比较
    目录1.String2.StringBuffer3.StringBuilder4.性能提升5.总结和比较面试模拟参考资料1.String不可变查看String源码如下:publicfinalclassStringimplementsjava.io.Serializable,Comparable<String>,CharSequence{/**Thevalueisusedforcharacterstora......
  • vue组件封装 - 选择器远程搜索下拉列表
    <!--*component:人员选择-远程搜索下拉列表*time:2023/7/19*author:zx*使用方式*importPersonSelectfrom"@/components/Dialog/personSelect.vue";*components:{PersonSelect}*<person-selectv-model="test"/>--><......
  • ValueNotifier<T> ValueListenableBuilder<T> Stack() positioned.fill()
     1、在Column下面增加可以滚动的Row2、在widget外部控件其内部的变量ValueNotifier<T> ValueListenableBuilder<T>(valueListenable:...,builder:()=>)  import'package:flutter/material.dart';classSettingsPageextendsStatelessWidget{finalint_cou......
  • 3步带你搞定华为云编译构建CodeArts Build “新手村任务”
    华为云编译构建(CodeArtsBuild)基于云端大规模并发加速,为客户提供高速、低成本、配置简单的混合语言构建能力,帮助客户缩短构建时间,提升构建效率。本文将给各位开发者带来华为云CodeArtsPipeline的手把手初级教学,让没有接触过的开发者能够轻松上手体验。【操作步骤】体验准备:注......
  • 如何从 Fedora 37 工作站升级到 Fedora 38(GUI 和 CLI)
    导读使用GUI和CLI方法从Fedora37工作站版升级到Fedora38的完整步骤。Fedora38已发布,没有任何延误。此版本带来了令人惊叹的GNOME44工作站版桌面、KDEPlasma5.27(最后一个Plasma5系列)等。你可以在此页面上阅读我写的Fedora38功能指南。如果你尝......
  • easyui combobox 多选及全选 功能(XTHS_实测可行)
    代码如下:$("#DepartmentCode").comboex({data:[{"value":"","text":"全部"},{"value":"001","text":"部门1"},{......
  • AI识别检验报告 -PaddleNLP UIE-X 在医疗领域的实战
    目录UIE-X在医疗领域的实战1.项目背景2.案例简介3.环境准备数据转换5.模型微调6.模型评估7.Taskflow一键部署UIE-X在医疗领域的实战PaddleNLP全新发布UIE-X......
  • 什么是 SAP UI5 的 Shell Layout
    ShellLayout是SAPUI5中一个重要的UI控件,它为应用程序提供了一个统一的、一致的用户界面。ShellLayout是一个基本的框架,它为应用程序提供了一个顶层的容器。这个容器包含了一些基本的UI元素,如标题栏、导航栏、工具栏、内容区域等。通过使用ShellLayout,开发者可以确保他们的应......