Time
Display a date, time or relative time. A native <time> element preserves the machine-readable ISO timestamp, and relative text follows the current locale.
Import
import { HuiTime } from '@nscode/handy-ui'
import type { TimeProps, TimeType } from '@nscode/handy-ui'
Examples
Display types
type accepts date, datetime, time and relative, with datetime as the default. These examples use a fixed reference so their results remain stable.
<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>
Past and future
The rules apply in order: less than one minute uses “just now”; less than one hour uses minutes; the same day uses hours; yesterday or tomorrow includes HH:mm; up to seven calendar days uses days. Beyond that, dates in the same year show month, day and time; other years show the full date. Minutes and hours round down in magnitude, and calendar days follow the local time zone.
<script setup lang="ts">
import { HuiConfigProvider, HuiSpace, HuiTime, localeEn } 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>
<HuiConfigProvider :locale="localeEn">
<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>
</HuiConfigProvider>
</template>
Dates, strings and timestamps
Accepts Date, millisecond timestamps and date strings. Strings without a time zone use local time; ISO strings with a zone identify an exact instant. unix treats numeric time and to values as seconds, leaving strings and Date objects unchanged.
<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>Milliseconds</span><HuiTime :time="milliseconds" /></HuiSpace>
<HuiSpace><span>Seconds</span><HuiTime :time="seconds" unix /></HuiSpace>
<HuiSpace><span>String</span><HuiTime time="2026-06-15 12:00:00" /></HuiSpace>
<HuiSpace><span>ISO</span><HuiTime :time="date.toISOString()" /></HuiSpace>
<HuiSpace
><span>Seconds reference</span
><HuiTime :time="seconds - 180" :to="seconds" unix type="relative"
/></HuiSpace>
</HuiSpace>
</template>
Custom formats
format overrides defaults for date, datetime and time. It does not change relative output. Supported tokens: YYYY, YY, MM, M, DD, D, HH, H, mm, m, ss, s, plus local offsets Z (±HH:mm) and ZZ (±HHmm). Output uses local fields and the offset at that instant, without retaining the input offset. UTC environments emit +00:00 / +0000. Bracketed text is literal, including [Z] / [ZZ]; an empty format renders empty text.
<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="DD/MM/YYYY" />
<HuiTime :time="value" type="datetime" format="YYYY-MM-DD [at] 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>
Updating the reference
An explicit to disables automatic clock subscriptions. Changing it recalculates the result immediately.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiConfigProvider, HuiSpace, HuiTime, localeEn } 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>
<HuiConfigProvider :locale="localeEn">
<HuiSpace direction="vertical" align="start">
<HuiButton @click="to += 60000">Advance reference by one minute</HuiButton>
<HuiTime :time="to" />
<HuiTime :time="value" :to="to" type="relative" />
</HuiSpace>
</HuiConfigProvider>
</template>
Automatic updates
Updates run only for relative with autoUpdate=true and no to. All instances share one 30-second timer. Returning to the foreground refreshes immediately; removing the last subscriber clears the timer and listener. Disabling updates freezes the current snapshot, and enabling them again refreshes immediately.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiConfigProvider, HuiSpace, HuiSwitch, HuiTime, localeEn } from '@nscode/handy-ui'
const autoUpdate = ref(true)
const value = Date.now() - 45000
</script>
<template>
<HuiConfigProvider :locale="localeEn">
<HuiSpace direction="vertical" align="start">
<HuiSpace><HuiSwitch v-model="autoUpdate" /><span>Automatic updates</span></HuiSpace>
<HuiTime :time="value" type="relative" :auto-update="autoUpdate" />
<span>Starts at just now, then shows 1 minute ago after about 30 seconds.</span>
</HuiSpace>
</HuiConfigProvider>
</template>
Day limits and hover titles
relativeLimit defaults to 7. Larger distances show dates, while yesterday and tomorrow remain higher-priority rules. showTitle controls the native hover title with the full date and time.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiConfigProvider, HuiSpace, HuiSwitch, HuiTime, localeEn } from '@nscode/handy-ui'
const extended = ref(true)
const showTitle = ref(true)
</script>
<template>
<HuiConfigProvider :locale="localeEn">
<HuiSpace direction="vertical" align="start">
<HuiSpace
><HuiSwitch v-model="extended" /><span>{{
extended ? '7-day limit' : '2-day limit'
}}</span></HuiSpace
>
<HuiSpace><HuiSwitch v-model="showTitle" /><span>Show full hover title</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>
</HuiConfigProvider>
</template>
Locales
Intl.RelativeTimeFormat generates relative minute, hour and day text using the current language. “Just now” and numeric formats come from the locale pack and accept local HuiConfigProvider overrides.
<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
| Prop | Description | Type | Default |
|---|---|---|---|
| time | Time to display; empty when omitted | Date | number | string | — |
| type | Display type | 'relative' | 'date' | 'datetime' | 'time' | 'datetime' |
| format | Absolute format; empty string is respected | string | Locale format |
| unix | Interpret numeric time and to as seconds | boolean | false |
| to | Fixed reference for relative output | Date | number | string | Current time |
| relativeLimit | Relative day limit; negatives become 0, non-finite values become 7 | number | 7 |
| autoUpdate | Refresh relative times without to automatically | boolean | true |
| showTitle | Full date and time in the relative hover title | boolean | true |
Events
None.
Slots
None.
Methods
No component instance methods.
Types
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
}