index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div>
  3. <el-date-picker
  4. v-model="dateRangeValue"
  5. type="daterange"
  6. unlink-panels
  7. range-separator="To"
  8. start-placeholder="开始日期"
  9. end-placeholder="结束日期"
  10. :shortcuts="shortcuts"
  11. size="default"
  12. value-format="YYYY-MM-DD"
  13. :disabled-date="disabledDate"
  14. :clearable="false"
  15. :popper-options="{placement: props.popperPlacement}"
  16. @change="$emit('change', dateRangeValue)"
  17. />
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { ref } from 'vue'
  22. import dayjs, { Dayjs } from 'dayjs'
  23. const props = defineProps({
  24. timezone: { type: String, required: true },
  25. popperPlacement: { type: String, default: 'bottom-start' }
  26. })
  27. const dateRangeValue = ref(['2023-10-01', '2023-10-10'])
  28. function disabledDate(datetime: Date) {
  29. const now = dayjs(new Date()).tz(props.timezone)
  30. const now_tz = now.startOf("day")
  31. return now_tz.isBefore(dayjs(datetime).tz(props.timezone))
  32. }
  33. function tzFormat(dt_tz: Dayjs) {
  34. return dt_tz.format("YYYY-MM-DD HH:mm:ss")
  35. }
  36. function recentDays(days: number):string[] {
  37. const now_tz = dayjs(new Date()).tz(props.timezone)
  38. const start = now_tz.subtract(days, 'day')
  39. const end = now_tz.subtract(1, 'day')
  40. return [tzFormat(start), tzFormat(end)]
  41. }
  42. function currentWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  43. const end = dayjs(new Date()).tz(props.timezone)
  44. const start = end.startOf(dim)
  45. return [tzFormat(start), tzFormat(end)]
  46. }
  47. function lastWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  48. const last = dayjs(new Date()).tz(props.timezone).subtract(1, dim)
  49. return [tzFormat(last.startOf(dim)), tzFormat(last.endOf(dim))]
  50. }
  51. const shortcuts = [
  52. {
  53. text: '今天',
  54. value: () => {
  55. const now_tz = dayjs(new Date()).tz(props.timezone)
  56. return [tzFormat(now_tz), tzFormat(now_tz)]
  57. },
  58. },
  59. {
  60. text: '昨天',
  61. value: () => recentDays(1)
  62. },
  63. {
  64. text: '最近7天',
  65. value: () => recentDays(7)
  66. },
  67. {
  68. text: '最近15天',
  69. value: () => recentDays(15)
  70. },
  71. {
  72. text: '最近30天',
  73. value: () => recentDays(30)
  74. },
  75. {
  76. text: '本周',
  77. value: () => currentWeekMonthYear('week')
  78. },
  79. {
  80. text: '上周',
  81. value: () => lastWeekMonthYear('week')
  82. },
  83. {
  84. text: '本月',
  85. value: () => currentWeekMonthYear('month')
  86. },
  87. {
  88. text: '上月',
  89. value: () => lastWeekMonthYear('month')
  90. },
  91. {
  92. text: '本年',
  93. value: () => currentWeekMonthYear('year')
  94. }
  95. ]
  96. </script>
  97. <style scoped>
  98. </style>