Time 時間
顯示日期、時間或相對時間。使用原生 <time> 保留機器可讀的 ISO 時間,相對文字跟隨目前語言。
引入
import { HuiTime } from '@nscode/handy-ui'
import type { TimeProps, TimeType } from '@nscode/handy-ui'
示例
四種顯示方式
type 支援 date、datetime、time、relative,預設 datetime。下面固定參照時間,演示結果不會隨查看日期改變。
<script setup lang="ts">
import { HuiSpace, HuiTime } from '@nscode/handy-ui'
const value = '2026-06-15 09:20:30'
const to = '2026-06-15 12:00:00'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace><span>date</span><HuiTime :time="value" type="date" /></HuiSpace>
<HuiSpace><span>datetime</span><HuiTime :time="value" type="datetime" /></HuiSpace>
<HuiSpace><span>time</span><HuiTime :time="value" type="time" /></HuiSpace>
<HuiSpace><span>relative</span><HuiTime :time="value" type="relative" :to="to" /></HuiSpace>
</HuiSpace>
</template>
相對時間的過去和將來
依次判斷:小於一分鐘顯示“剛剛”,小於一小時顯示分鐘,同一天顯示小時,昨天或明天加 HH:mm,預設七個自然日內顯示天數;再遠則同年顯示月日和時分,跨年顯示年月日。分鐘和小時向下取整,自然日按本地時區判斷。
<script setup lang="ts">
import { HuiSpace, HuiTime } from '@nscode/handy-ui'
const to = '2026-06-15 12:00:00'
const values = [
'2026-06-15 11:59:30',
'2026-06-15 12:00:30',
'2026-06-15 11:57:00',
'2026-06-15 12:03:00',
'2026-06-15 07:00:00',
'2026-06-15 17:00:00',
'2026-06-14 14:20:00',
'2026-06-16 09:00:00',
'2026-06-12 12:00:00',
'2026-06-18 12:00:00',
'2026-05-01 12:00:00',
'2027-01-01 12:00:00'
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="value in values" :key="value">
<span>{{ value }}</span
><HuiTime :time="value" type="relative" :to="to" />
</HuiSpace>
</HuiSpace>
</template>
日期、字串和時間戳
接受 Date、毫秒時間戳和日期字串。無時區字串按本地時間解析,帶時區 ISO 字串表示確定時刻。unix 只把數字的 time、to 當成秒,字串和 Date 不受影響。
<script setup lang="ts">
import { HuiSpace, HuiTime } from '@nscode/handy-ui'
const date = new Date(2026, 5, 15, 12, 0, 0)
const milliseconds = date.getTime()
const seconds = milliseconds / 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace><span>Date</span><HuiTime :time="date" /></HuiSpace>
<HuiSpace><span>毫秒</span><HuiTime :time="milliseconds" /></HuiSpace>
<HuiSpace><span>秒</span><HuiTime :time="seconds" unix /></HuiSpace>
<HuiSpace><span>字串</span><HuiTime time="2026-06-15 12:00:00" /></HuiSpace>
<HuiSpace><span>ISO</span><HuiTime :time="date.toISOString()" /></HuiSpace>
<HuiSpace
><span>秒參照</span><HuiTime :time="seconds - 180" :to="seconds" unix type="relative"
/></HuiSpace>
</HuiSpace>
</template>
自訂格式
format 覆蓋 date、datetime、time 的預設格式,不改變相對時間。支援 YYYY、YY、MM、M、DD、D、HH、H、mm、m、ss、s,以及本地偏移 Z(±HH:mm)、ZZ(±HHmm)。輸出使用該時刻的本地時間和偏移,不保留輸入偏移;UTC 環境輸出 +00:00 / +0000。方括號中的文字原樣輸出,[Z] / [ZZ] 也是字面量;空字串會顯示空文字。
<script setup lang="ts">
import { HuiSpace, HuiTime } from '@nscode/handy-ui'
const value = '2026-06-15 09:20:30'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiTime :time="value" type="date" format="YYYY/MM/DD" />
<HuiTime :time="value" type="datetime" format="YYYY[年]M[月]D[日] H:mm" />
<HuiTime :time="value" type="time" format="HH:mm" />
<HuiTime :time="value" format="YYYY-MM-DDTHH:mm:ssZ" />
<HuiTime :time="value" format="YYYY-MM-DDTHH:mm:ssZZ" />
</HuiSpace>
</template>
更新參照時間
傳入 to 後使用固定參照,不訂閱自動刷新;改變 to 會立即重新計算。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTime } from '@nscode/handy-ui'
const to = ref(new Date(2026, 5, 15, 12).getTime())
const value = new Date(2026, 5, 15, 11, 57)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiButton @click="to += 60000">參照時間增加一分鐘</HuiButton>
<HuiTime :time="to" />
<HuiTime :time="value" :to="to" type="relative" />
</HuiSpace>
</template>
自動刷新
只有 relative、autoUpdate=true 且未傳 to 時才會自動更新;頁面上的實例共用一個 30 秒計時器。頁面回到前景時立即更新,最後一個訂閱實例卸載時清理計時器。關閉自動更新會保留目前快照,重新開啟時立即更新。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiSwitch, HuiTime } from '@nscode/handy-ui'
const autoUpdate = ref(true)
const value = Date.now() - 45000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace><HuiSwitch v-model="autoUpdate" /><span>自動刷新</span></HuiSpace>
<HuiTime :time="value" type="relative" :auto-update="autoUpdate" />
<span>初次顯示剛剛,約 30 秒後顯示 1 分鐘前。</span>
</HuiSpace>
</template>
相對天數上限和暫留標題
relativeLimit 預設 7;超過上限切換到日期,但“昨天、明天”始終優先。showTitle 控制相對時間的原生暫留標題,標題使用完整日期時間。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiSwitch, HuiTime } from '@nscode/handy-ui'
const extended = ref(true)
const showTitle = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace
><HuiSwitch v-model="extended" /><span>{{
extended ? '上限 7 天' : '上限 2 天'
}}</span></HuiSpace
>
<HuiSpace><HuiSwitch v-model="showTitle" /><span>暫留顯示完整時間</span></HuiSpace>
<HuiTime
time="2026-06-12 09:20:30"
to="2026-06-15 12:00:00"
type="relative"
:relative-limit="extended ? 7 : 2"
:show-title="showTitle"
/>
</HuiSpace>
</template>
跟隨語言
分鐘、小時、天的相對文字由 Intl.RelativeTimeFormat 按目前語言生成。“剛剛”和數字格式來自語言包,可以透過 HuiConfigProvider 局部覆蓋。
<script setup lang="ts">
import { HuiConfigProvider, HuiSpace, HuiTime, localeEn, localeZhCn } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiConfigProvider :locale="localeZhCn">
<HuiSpace
><span>中文</span><HuiTime time="2026-06-15 11:57" to="2026-06-15 12:00" type="relative"
/></HuiSpace>
</HuiConfigProvider>
<HuiConfigProvider :locale="localeEn">
<HuiSpace
><span>English</span><HuiTime time="2026-06-15 11:57" to="2026-06-15 12:00" type="relative"
/></HuiSpace>
</HuiConfigProvider>
<HuiConfigProvider :locale="{ time: { dateFormat: 'DD/MM/YYYY' } }">
<HuiTime time="2026-06-15" type="date" />
</HuiConfigProvider>
</HuiSpace>
</template>
Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| time | 顯示的時間;不傳時為空 | Date | number | string | — |
| type | 顯示方式 | 'relative' | 'date' | 'datetime' | 'time' | 'datetime' |
| format | 絕對時間自訂格式,空字串有效 | string | 對應語言包格式 |
| unix | 數字 time、to 按秒解析 | boolean | false |
| to | 相對時間的固定參照 | Date | number | string | 目前時間 |
| relativeLimit | 相對天數上限;負數按 0,非有限值按 7 | number | 7 |
| autoUpdate | 無 to 的相對時間自動刷新 | boolean | true |
| showTitle | 相對時間暫留顯示完整時間 | boolean | true |
Events
無。
Slots
無。
方法
無元件實例方法。
類型
type TimeType = 'relative' | 'date' | 'datetime' | 'time'
interface TimeProps {
time?: Date | number | string
type?: TimeType
format?: string
unix?: boolean
to?: Date | number | string
relativeLimit?: number
autoUpdate?: boolean
showTitle?: boolean
}