当前位置:   article > 正文

vue中日,周,月,年时间选择器(基于elementui)_vue日历组件 切换月周日都可选择

vue日历组件 切换月周日都可选择

 

通过选择上面的选项展示选择不同的日期,周,月份,年份

 因为项目中点击切换时需要传递不同的日期,

例如:日,即选择日期的00:00分-23:59

周:即选择当月的第三周,截取第三周的周一和第三周的周日为开始时间和截止时间传值

月,即选择月的第一天---选择月得最后一天传值

  1. <template>
  2. <div class="chart-date-picker">
  3. <el-radio-group v-model="dateType" size="mini" @change="handleChange">
  4. <el-radio-button label="date"></el-radio-button>
  5. <el-radio-button label="week"></el-radio-button>
  6. <el-radio-button label="month"></el-radio-button>
  7. <el-radio-button label="year"></el-radio-button>
  8. </el-radio-group>
  9. <el-date-picker :clearable="false" v-model="date" size="mini" :picker-options="{ 'firstDayOfWeek': 1 }" :type="dateType" :format="dateType==='week'?'yyyy 第 WW 周':null" :placeholder="dateType==='date'?'选择日':dateType==='week'?'选择周':dateType==='month'?'选择月':dateType==='year'?'选择年':''" style="width: 140px;vertical-align: middle" @change="handleChange"></el-date-picker>
  10. </div>
  11. </template>
  12. <script>
  13. import { dateFormat } from "@/filters/index";
  14. export default {
  15. name: "ultralabxChartDatePicker",
  16. props: {
  17. value: {
  18. type: Date,
  19. },
  20. type: {
  21. type: String,
  22. default: "week",
  23. },
  24. },
  25. data() {
  26. return {
  27. dateType: "",
  28. date: null,
  29. };
  30. },
  31. methods: {
  32. handleChange() {
  33. let obj = this.timeDifference(this.date, this.dateType);
  34. this.$emit("handleChangeTime", obj);
  35. },
  36. timeDifference() {
  37. let timeObj = {};
  38. let time = new Date(dateFormat(this.date, "yyyy-MM-dd")); // 可入参指定时间
  39. let year = time.getFullYear();
  40. let month = time.getMonth();
  41. let date = time.getDate(); // 获取日期
  42. let day = time.getDay(); // 获取周几,0-6,0表示周日
  43. let _day = day == 0 ? 7 : day;
  44. switch (this.dateType) {
  45. case "date":
  46. timeObj = {
  47. startTime: `${dateFormat(this.date, "yyyy-MM-dd")} 00:00:00`,
  48. endTime: `${dateFormat(this.date, "yyyy-MM-dd")} 23:59:59`,
  49. };
  50. break;
  51. case "week":
  52. // 获取周一日期
  53. var startDate = new Date(year, month, date - _day + 1);
  54. // 获取周日日期
  55. var endDate = new Date(
  56. new Date(startDate).getTime() + 6 * 24 * 3600 * 1000
  57. );
  58. timeObj = {
  59. startTime: `${dateFormat(startDate, "yyyy-MM-dd")} 00:00:00`,
  60. endTime: `${dateFormat(endDate, "yyyy-MM-dd")} 23:59:59`,
  61. };
  62. break;
  63. case "month":
  64. var startDate = new Date(year, month, 1);
  65. var endDate = new Date(year, month + 1, 0);
  66. timeObj = {
  67. startTime: `${dateFormat(startDate, "yyyy-MM-dd")} 00:00:00`,
  68. endTime: `${dateFormat(endDate, "yyyy-MM-dd")} 23:59:59`,
  69. };
  70. break;
  71. case "year":
  72. var startDate = new Date(year, 0, 1);
  73. var endDate = new Date(year, 12, 0);
  74. timeObj = {
  75. startTime: `${dateFormat(startDate, "yyyy-MM-dd")} 00:00:00`,
  76. endTime: `${dateFormat(endDate, "yyyy-MM-dd")} 23:59:59`,
  77. };
  78. break;
  79. }
  80. return timeObj;
  81. },
  82. },
  83. watch: {
  84. value: {
  85. immediate: true,
  86. handler(val) {
  87. this.date = val;
  88. },
  89. },
  90. type: {
  91. immediate: true,
  92. handler(val) {
  93. if (val === this.dateType) {
  94. return;
  95. }
  96. this.dateType = val;
  97. },
  98. },
  99. date: {
  100. immediate: true,
  101. handler(val) {
  102. this.$emit("input", val);
  103. },
  104. },
  105. dateType: {
  106. immediate: true,
  107. handler(val) {
  108. if (val === this.type) {
  109. return;
  110. }
  111. this.$emit("update:type", val);
  112. },
  113. },
  114. },
  115. };
  116. </script>
  117. <style lang="scss">
  118. .chart-date-picker {
  119. .el-radio-button--mini .el-radio-button__inner {
  120. padding: 5px 15px;
  121. }
  122. .el-radio-button:first-child .el-radio-button__inner {
  123. border-radius: 15px 0 0 15px;
  124. }
  125. .el-radio-button:last-child .el-radio-button__inner {
  126. border-radius: 0;
  127. }
  128. .el-input--mini .el-input__inner {
  129. height: 24px;
  130. line-height: 24px;
  131. }
  132. .el-input--mini .el-input__icon {
  133. line-height: 24px;
  134. }
  135. .el-input__inner {
  136. border-left: none;
  137. border-radius: 0 15px 15px 0;
  138. }
  139. }
  140. </style>
  141. <style lang="scss" scoped>
  142. .chart-date-picker {
  143. }
  144. </style>

此处为上面时间格式化的方法,按需使用

  1. /**
  2. * 日期格式化
  3. */
  4. export function dateFormat(date, format = 'yyyy-MM-dd hh:mm:ss') {
  5. if (date !== 'Invalid Date') {
  6. var o = {
  7. 'M+': date.getMonth() + 1, // month
  8. 'd+': date.getDate(), // day
  9. 'h+': date.getHours(), // hour
  10. 'm+': date.getMinutes(), // minute
  11. 's+': date.getSeconds(), // second
  12. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  13. 'S': date.getMilliseconds() // millisecond
  14. }
  15. if (/(y+)/.test(format)) {
  16. format = format.replace(RegExp.$1,
  17. (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  18. }
  19. for (var k in o) {
  20. if (new RegExp('(' + k + ')').test(format)) {
  21. format = format.replace(RegExp.$1,
  22. RegExp.$1.length === 1 ? o[k]
  23. : ('00' + o[k]).substr(('' + o[k]).length))
  24. }
  25. }
  26. return format
  27. }
  28. return ''
  29. }

父组件中使用方法

  1. <chart-date-picker v-model="value" :type.sync="groupType" @handleChangeTime="handleChangeTime(type=0,$event)"></chart-date-picker>
  2. data() {
  3. retrun {
  4. value: new Date(),
  5. groupType: "week",
  6. }
  7. }
  8. methods:{
  9. handleChangeTime(type, timeObj){
  10. console.log(timeObj)
  11. // TODO
  12. }
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/97566
推荐阅读