保誠-保戶業務員媒合平台
Mila
2021-12-06 f2fe7c2bacd26cde387d4d3a17ce6161dd189e26
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
<template>
    <span>
        {{ formattedDate }}
    </span>
</template>
 
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator';
 
@Component
export default class UiDateFormat extends Vue {
    @Prop() date!: Date;
    @Prop() onlyShowSection!: 'DAY' | 'TIME';
 
    get formattedDate(): string {
        const isToday = (compareDate: Date): boolean => {
            return compareDate.getFullYear() === _now.getFullYear()
                && compareDate.getMonth() === _now.getMonth()
                && compareDate.getDate() === _now.getDate()
        };
 
        const isThisYear = (compareDate: Date): boolean => {
            return compareDate.getFullYear() === _now.getFullYear();
        };
        const _now = new Date();
        const minutes = _now.getMinutes() > 9 ?  _now.getMinutes() : `0${_now.getMinutes()}`;
        const thisYearDayLabel = isToday(this.date) ? `今天` : `${this.date.getMonth() + 1}/${this.date.getDate()}`;
 
        if (this.onlyShowSection === 'DAY') {
            return isThisYear(this.date) ? thisYearDayLabel : `${this.date.getFullYear()}/${this.date.getMonth() + 1}/${this.date.getDate()}`;
        } else if (this.onlyShowSection === 'TIME') {
            return `${this.date.getHours()}:${minutes}`;
        }
 
        return isThisYear(this.date) 
            ? `${thisYearDayLabel} ${this.date.getHours()}:${minutes}` 
            : `${this.date.getFullYear()}/${this.date.getMonth() + 1}/${this.date.getDate()} ${this.date.getHours()}:${minutes}`;
    }    
}
</script>