datePicker.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <!-- value-format="yyyy-MM-dd" -->
  3. <el-date-picker
  4. v-model="dateValue"
  5. class="timePicker"
  6. type="daterange"
  7. :picker-options="pickerOptions"
  8. value-format="yyyyMMdd"
  9. range-separator="~"
  10. start-placeholder="开始日期"
  11. end-placeholder="结束日期"
  12. :clearable="false"
  13. align="left"
  14. @change="dateChange"
  15. >
  16. </el-date-picker>
  17. </template>
  18. <script>
  19. const nowYear = new Date().getFullYear();
  20. const nowMonth = new Date().getMonth() + 1 + "";
  21. const nowDay = new Date().getDate() + "";
  22. function setdata(time) {
  23. return `${time.length == 1 ? "0" : ""}${time}`;
  24. }
  25. export default {
  26. data() {
  27. return {
  28. pickerOptions: {
  29. shortcuts: [
  30. {
  31. text: "今年",
  32. onClick(picker) {
  33. const start = new Date(`${nowYear}-01-01`);
  34. const end = new Date();
  35. picker.$emit("pick", [start, end]);
  36. },
  37. },
  38. {
  39. text: "去年",
  40. onClick(picker) {
  41. const start = new Date(`${nowYear - 1}-01-01`);
  42. const end = new Date(`${nowYear - 1}-12-31`);
  43. picker.$emit("pick", [start, end]);
  44. },
  45. },
  46. {
  47. text: "近三年",
  48. onClick(picker) {
  49. const start = new Date(`${nowYear - 3}-${nowMonth}-${nowDay}`);
  50. const end = new Date();
  51. picker.$emit("pick", [start, end]);
  52. },
  53. },
  54. {
  55. text: "近五年",
  56. onClick(picker) {
  57. const start = new Date(`${nowYear - 5}-${nowMonth}-${nowDay}`);
  58. const end = new Date();
  59. picker.$emit("pick", [start, end]);
  60. },
  61. },
  62. ],
  63. },
  64. // dateValue: [`${nowYear}-01-01`, `${nowYear}-${nowMonth}-${nowDay}`]
  65. dateValue: [
  66. `${nowYear}0101`,
  67. `${nowYear}${setdata(nowMonth)}${setdata(nowDay)}`,
  68. ],
  69. };
  70. },
  71. mounted() {
  72. this.dateChange();
  73. },
  74. methods: {
  75. dateChange() {
  76. this.$emit("dateChange", this.dateValue);
  77. store.setCockpitDate(this.dateValue);
  78. },
  79. },
  80. };
  81. </script>
  82. <style lang="scss" scoped></style>
  83. <style lang="scss">
  84. // 日期按选择器样式修改
  85. .timePicker {
  86. position: absolute;
  87. top: 40px; //top: 10%;
  88. left: 23%;
  89. z-index: 100;
  90. font-size: 14px;
  91. padding: 0;
  92. width: 180px !important;
  93. }
  94. @import "datePicker";
  95. </style>