index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. @change="$emit('change', dateRangeValue)"
  16. />
  17. </div>
  18. </template>
  19. <script setup lang="ts">
  20. import { ref } from 'vue'
  21. import dayjs, { Dayjs } from 'dayjs'
  22. import { start } from 'nprogress';
  23. const props = defineProps<{timezone: string}>()
  24. const dateRangeValue = ref([])
  25. function disabledDate(datetime: Date) {
  26. const now = dayjs(new Date()).tz(props.timezone)
  27. const now_tz = now.startOf("day")
  28. return now_tz.isBefore(dayjs(datetime).tz(props.timezone))
  29. }
  30. function tzFormat(dt_tz: Dayjs) {
  31. return dt_tz.format("YYYY-MM-DD HH:mm:ss")
  32. }
  33. function recentDays(days: number):string[] {
  34. const now_tz = dayjs(new Date()).tz(props.timezone)
  35. const start = now_tz.subtract(days, 'day')
  36. const end = now_tz.subtract(1, 'day')
  37. return [tzFormat(start), tzFormat(end)]
  38. }
  39. function currentWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  40. const end = dayjs(new Date()).tz(props.timezone)
  41. const start = end.startOf(dim)
  42. return [tzFormat(start), tzFormat(end)]
  43. }
  44. function lastWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  45. const last = dayjs(new Date()).tz(props.timezone).subtract(1, dim)
  46. return [tzFormat(last.startOf(dim)), tzFormat(last.endOf(dim))]
  47. }
  48. const shortcuts = [
  49. {
  50. text: '今天',
  51. value: () => {
  52. const now_tz = dayjs(new Date()).tz(props.timezone)
  53. return [tzFormat(now_tz), tzFormat(now_tz)]
  54. },
  55. },
  56. {
  57. text: '昨天',
  58. value: () => recentDays(1)
  59. },
  60. {
  61. text: '最近7天',
  62. value: () => recentDays(7)
  63. },
  64. {
  65. text: '最近15天',
  66. value: () => recentDays(15)
  67. },
  68. {
  69. text: '最近30天',
  70. value: () => recentDays(30)
  71. },
  72. {
  73. text: '本周',
  74. value: () => currentWeekMonthYear('week')
  75. },
  76. {
  77. text: '上周',
  78. value: () => lastWeekMonthYear('week')
  79. },
  80. {
  81. text: '本月',
  82. value: () => currentWeekMonthYear('month')
  83. },
  84. {
  85. text: '上月',
  86. value: () => lastWeekMonthYear('month')
  87. },
  88. {
  89. text: '本年',
  90. value: () => currentWeekMonthYear('year')
  91. }
  92. ]
  93. </script>
  94. <style scoped>
  95. </style>