index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div>
  3. <el-date-picker
  4. v-model="dateRange"
  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="changedValue"
  17. />
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { ref, onMounted } from 'vue'
  22. import dayjs, { Dayjs } from 'dayjs'
  23. defineOptions({
  24. name: 'DateRangePicker'
  25. })
  26. const props = defineProps({
  27. modelValue: {type: Array, required: true },
  28. timezone: { type: String, required: true },
  29. popperPlacement: { type: String, default: 'bottom-start' }
  30. })
  31. const dateRange = ref(props.modelValue)
  32. const emits = defineEmits(['update:modelValue', 'change'])
  33. const changedValue = (newVal: string[]) => {
  34. emits('update:modelValue', newVal)
  35. emits('change', newVal)
  36. }
  37. // onMounted(() => {
  38. // if(dateRange.value.length === 0 && props.defaultRecentDay > 0) {
  39. // const now_tz = dayjs(new Date()).tz(props.timezone)
  40. // const start = now_tz.subtract(props.defaultRecentDay, 'day')
  41. // const end = now_tz.subtract(1, 'day')
  42. // dateRange.value = [start.format("YYYY-MM-DD"), end.format("YYYY-MM-DD")]
  43. // emits('update:modelValue', dateRange.value)
  44. // }
  45. // })
  46. function disabledDate(datetime: Date) {
  47. const now = dayjs(new Date()).tz(props.timezone)
  48. const now_tz = now.startOf("day")
  49. return now_tz.isBefore(dayjs(datetime).tz(props.timezone))
  50. }
  51. function tzFormat(dt_tz: Dayjs) {
  52. return dt_tz.format("YYYY-MM-DD HH:mm:ss")
  53. }
  54. function recentDays(days: number):string[] {
  55. const now_tz = dayjs(new Date()).tz(props.timezone)
  56. const start = now_tz.subtract(days, 'day')
  57. const end = now_tz.subtract(1, 'day')
  58. return [tzFormat(start), tzFormat(end)]
  59. }
  60. function currentWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  61. const end = dayjs(new Date()).tz(props.timezone)
  62. const start = end.startOf(dim)
  63. return [tzFormat(start), tzFormat(end)]
  64. }
  65. function lastWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  66. const last = dayjs(new Date()).tz(props.timezone).subtract(1, dim)
  67. return [tzFormat(last.startOf(dim)), tzFormat(last.endOf(dim))]
  68. }
  69. const shortcuts = [
  70. {
  71. text: '今天',
  72. value: () => {
  73. const now_tz = dayjs(new Date()).tz(props.timezone)
  74. return [tzFormat(now_tz), tzFormat(now_tz)]
  75. },
  76. },
  77. {
  78. text: '昨天',
  79. value: () => recentDays(1)
  80. },
  81. {
  82. text: '最近7天',
  83. value: () => recentDays(7)
  84. },
  85. {
  86. text: '最近15天',
  87. value: () => recentDays(15)
  88. },
  89. {
  90. text: '最近30天',
  91. value: () => recentDays(30)
  92. },
  93. {
  94. text: '本周',
  95. value: () => currentWeekMonthYear('week')
  96. },
  97. {
  98. text: '上周',
  99. value: () => lastWeekMonthYear('week')
  100. },
  101. {
  102. text: '本月',
  103. value: () => currentWeekMonthYear('month')
  104. },
  105. {
  106. text: '上月',
  107. value: () => lastWeekMonthYear('month')
  108. },
  109. {
  110. text: '本年',
  111. value: () => currentWeekMonthYear('year')
  112. }
  113. ]
  114. </script>
  115. <style scoped>
  116. </style>