wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<template>
  <div
    style="width: 100%; height: 100%; display: flex; align-items: center"
    :style="{ 'justify-content': element.style.textAlign }"
  >
    <p>{{ state.nowDate }}</p>
  </div>
</template>
 
<script setup lang="ts">
import { onMounted, onUnmounted, reactive, toRefs } from 'vue'
 
const props = defineProps({
  element: {
    type: Object
  }
})
 
const { element } = toRefs(props)
const state = reactive({
  nowDate: '', // 当前日期
  nowWeek: '',
  timer: null
})
 
const currentTime = () => {
  state.timer = setInterval(formatDate, 500)
}
const formatDate = () => {
  const weekArr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  let timeFormat = element.value.formatInfo.timeFormat
  const showWeek = element.value.formatInfo.showWeek
  const showDate = element.value.formatInfo.showDate
  const dateFormat = element.value.formatInfo.dateFormat || 'yyyy-MM-dd'
  if (showDate && dateFormat) {
    timeFormat = dateFormat + ' ' + timeFormat
  }
 
  const date = new Date()
 
  state.nowDate = date.format(timeFormat)
 
  if (showWeek) {
    state.nowWeek = weekArr[date.getDay()]
    state.nowDate = state.nowDate + ' ' + state.nowWeek
  }
}
 
onMounted(() => {
  currentTime()
})
 
onUnmounted(() => {
  if (state.timer) {
    clearInterval(state.timer) // 在Vue实例销毁前,清除时间定时器
  }
})
</script>
 
<style lang="less" scoped></style>