保誠-保戶業務員媒合平台
HelenHuang
2021-12-06 1076fdc06d40646d1d6f125a3ce4d43cf4eac673
PAMapp/components/Ui/UiDateFormat.vue
@@ -1,40 +1,57 @@
<template>
    <span>
        {{ formattedDate }}
        {{ displayValue }}
    </span>
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator';
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
@Component
export default class UiDateFormat extends Vue {
    @Prop() date!: Date;
    @Prop() date!: Date | string;
    @Prop() onlyShowSection!: 'DAY' | 'TIME';
    compareTarget!: Date;
    get formattedDate(): string {
        const isToday = (compareDate: Date): boolean => {
            return compareDate.getFullYear() === _now.getFullYear()
                && compareDate.getMonth() === _now.getMonth()
                && compareDate.getDate() === _now.getDate()
        };
    displayValue = '';
        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()}`;
    @Watch('date', {immediate: true}) formattedDate(): void {
        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}`;
        }
        if (!this.date) return;
        return isThisYear(this.date)
            ? `${thisYearDayLabel} ${this.date.getHours()}:${minutes}`
            : `${this.date.getFullYear()}/${this.date.getMonth() + 1}/${this.date.getDate()} ${this.date.getHours()}:${minutes}`;
    }
        const toDatePromise = new Promise((resolve, reject) => {
            const date: Date = typeof(this.date) === 'string' ? new Date(this.date) : this.date as Date;
            resolve(date);
        });
        toDatePromise.then((compareTarget: any) => {
            const _now = new Date();
                    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 minutes = compareTarget.getMinutes() > 9 ?  compareTarget.getMinutes() : `0${compareTarget.getMinutes()}`;
                    const thisYearDayLabel = isToday(compareTarget) ? `今天` : `${compareTarget.getMonth() + 1}/${compareTarget.getDate()}`;
                    if (this.onlyShowSection === 'DAY') {
                        this.displayValue = isThisYear(compareTarget) ? thisYearDayLabel : `${compareTarget.getFullYear()}/${compareTarget.getMonth() + 1}/${compareTarget.getDate()}`;
                    } else if (this.onlyShowSection === 'TIME') {
                        this.displayValue = `${compareTarget.getHours()}:${minutes}`;
                    }
                    if (this.onlyShowSection) return;
                    this.displayValue = isThisYear(compareTarget)
                        ? `${thisYearDayLabel} ${compareTarget.getHours()}:${minutes}`
                        : `${compareTarget.getFullYear()}/${compareTarget.getMonth() + 1}/${compareTarget.getDate()} ${compareTarget.getHours()}:${minutes}`;
                }
        )}
}
</script>