Skip to content

Commit 368c175

Browse files
committed
feat: implement fromNowDate function for time difference calculation
1 parent 1a792dd commit 368c175

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

ui/src/layout/layout-header/avatar/AboutDialog.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
<span class="label">{{ $t('layout.about.expiredTime') }}</span>
1919
<span
2020
>{{ licenseInfo?.expired || '-' }}
21-
<!-- <span class="color-danger"-->
22-
<!-- v-if="licenseInfo?.expired && fromNowDate(licenseInfo?.expired)"-->
23-
<!-- >({{ fromNowDate(licenseInfo?.expired) }})</span>-->
21+
<span class="color-danger"
22+
v-if="licenseInfo?.expired && fromNowDate(licenseInfo?.expired)"
23+
>({{ fromNowDate(licenseInfo?.expired) }})</span>
2424
</span
2525
>
2626
</div>
@@ -66,7 +66,7 @@
6666
<script setup lang="ts">
6767
import {ref, computed, watch} from 'vue'
6868
import licenseApi from '@/api/system/license'
69-
//import {fromNowDate} from '@/utils/time'
69+
import {fromNowDate} from '@/utils/time'
7070
import {Role} from '@/utils/permission/type'
7171
import useStore from '@/stores'
7272
import { t } from '@/locales'

ui/src/utils/time.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import moment from 'moment'
22
import 'moment/dist/locale/zh-cn'
3+
34
moment.locale('zh-cn')
4-
import { t } from '@/locales'
5+
import {t} from '@/locales'
56

67
// 当天日期 YYYY-MM-DD
78
export const nowDate = moment().format('YYYY-MM-DD')
@@ -41,3 +42,42 @@ export const dateFormat = (timestamp: any) => {
4142

4243
return `${y}-${m}-${d}`
4344
}
45+
46+
47+
export function fromNowDate(time: any) {
48+
// 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
49+
const curTime = new Date()
50+
const futureTime = new Date(time)
51+
const timeDiff = futureTime.getTime() - curTime.getTime()
52+
53+
// 单位换算
54+
const min = 60 * 1000
55+
const hour = min * 60
56+
const day = hour * 24
57+
const week = day * 7
58+
59+
// 计算发布时间距离当前时间的周、天、时、分
60+
const exceedWeek = Math.floor(timeDiff / week)
61+
const exceedDay = Math.floor(timeDiff / day)
62+
const exceedHour = Math.floor(timeDiff / hour)
63+
const exceedMin = Math.floor(timeDiff / min)
64+
65+
// 最后判断时间差到底是属于哪个区间,然后return
66+
if (exceedWeek > 0) {
67+
return ''
68+
} else {
69+
if (exceedDay < 7 && exceedDay > 0) {
70+
return exceedDay + t('layout.time.daysLater')
71+
} else {
72+
if (exceedHour < 24 && exceedHour > 0) {
73+
return exceedHour + t('layout.time.hoursLater')
74+
} else {
75+
if (exceedMin < 0) {
76+
return t('layout.time.expired')
77+
} else {
78+
return t('layout.time.expiringSoon')
79+
}
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)