📄 useTemporalNow¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 13 |
| 📦 Imports | 6 |
| 🟢 Vue Composition API | 1 |
| 📐 Interfaces | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useTemporalNow/index.ts
📦 Imports¶
| Name | Source |
|---|---|
Pausable |
@vueuse/shared |
Ref |
vue |
ConfigurableScheduler |
../_configurable |
shallowRef |
vue |
watch |
vue |
useRafFn |
../useRafFn |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
watch |
watch | none | none |
Functions¶
useTemporalNow(options: UseTemporalNowOptions): UseTemporalNowReturn¶
Reactive Temporal API with timezone and calendar support.
Parameters:
optionsany: Configuration options
See: https://vueuse.org/useTemporalNow
Raw JSDoc
Calls:
assertTemporalresolveTemporalshallowRef (from vue)TemporalImpl.Now.zonedDateTimeISO(timezone.value).withCalendarschedulerwatch (from vue)now.value.withTimeZonenow.value.withCalendarnow.value.toPlainDatenow.value.toPlainTimenow.value.toPlainDateTimenow.value.toLocaleStringnow.value.addnow.value.subtractTemporalImpl.ZonedDateTime.compare
Internal Comments:
Code
export function useTemporalNow(options: UseTemporalNowOptions = {}): UseTemporalNowReturn {
const {
timezone: initialTimezone = 'UTC',
calendar: initialCalendar = 'gregory',
scheduler = useRafFn,
temporal: customTemporal,
} = options
const TemporalImpl = assertTemporal(resolveTemporal(customTemporal))
const timezone = shallowRef(initialTimezone)
const calendar = shallowRef(initialCalendar)
const now = shallowRef(
TemporalImpl.Now.zonedDateTimeISO(timezone.value).withCalendar(calendar.value),
)
function updateNow() {
now.value = TemporalImpl.Now.zonedDateTimeISO(timezone.value).withCalendar(calendar.value)
}
const { isActive, pause, resume } = scheduler(updateNow)
// Update immediately when timezone/calendar change, rather than waiting for the next tick
watch([timezone, calendar], updateNow)
const toTimezone = (tz: string) => now.value.withTimeZone(tz)
const toCalendar = (cal: string) => now.value.withCalendar(cal)
const toPlainDate = () => now.value.toPlainDate()
const toPlainTime = () => now.value.toPlainTime()
const toPlainDateTime = () => now.value.toPlainDateTime()
const format = (formatOptions?: Intl.DateTimeFormatOptions) => now.value.toLocaleString(undefined, formatOptions)
const add = (duration: Temporal.DurationLike) => now.value.add(duration)
const subtract = (duration: Temporal.DurationLike) => now.value.subtract(duration)
const compare = (other: Temporal.ZonedDateTime | string) => TemporalImpl.ZonedDateTime.compare(now.value, other)
return {
now,
timezone,
calendar,
toTimezone,
toCalendar,
toPlainDate,
toPlainTime,
toPlainDateTime,
format,
add,
subtract,
compare,
isActive,
pause,
resume,
}
}
resolveTemporal(custom: typeof Temporal): typeof Temporal | undefined¶
Parameters:
customtypeof Temporal
Returns: typeof Temporal | undefined
Code
assertTemporal(impl: typeof Temporal | undefined): typeof Temporal¶
Parameters:
impltypeof Temporal | undefined
Returns: typeof Temporal
Code
Internal helpers¶
Declared inside another function in this file.
updateNow(): void¶
Returns: void
Calls:
TemporalImpl.Now.zonedDateTimeISO(timezone.value).withCalendar
Code
toTimezone(tz: string): any¶
Parameters:
tzstring
Returns: any
Calls:
now.value.withTimeZone
toCalendar(cal: string): any¶
Parameters:
calstring
Returns: any
Calls:
now.value.withCalendar
toPlainDate(): any¶
Returns: any
Calls:
now.value.toPlainDate
toPlainTime(): any¶
Returns: any
Calls:
now.value.toPlainTime
toPlainDateTime(): any¶
Returns: any
Calls:
now.value.toPlainDateTime
format(formatOptions: Intl.DateTimeFormatOptions): any¶
Parameters:
formatOptionsIntl.DateTimeFormatOptions
Returns: any
Calls:
now.value.toLocaleString
Code
add(duration: Temporal.DurationLike): any¶
Parameters:
durationTemporal.DurationLike
Returns: any
Calls:
now.value.add
subtract(duration: Temporal.DurationLike): any¶
Parameters:
durationTemporal.DurationLike
Returns: any
Calls:
now.value.subtract
compare(other: Temporal.ZonedDateTime | string): any¶
Parameters:
otherTemporal.ZonedDateTime | string
Returns: any
Calls:
TemporalImpl.ZonedDateTime.compare
Code
Interfaces¶
UseTemporalNowOptions¶
Interface Code
export interface UseTemporalNowOptions extends ConfigurableScheduler {
/**
* Initial timezone
*
* @default 'UTC'
*/
timezone?: string
/**
* Calendar system to use
*
* @default 'gregory'
*/
calendar?: string
/**
* Custom `Temporal` implementation to use, e.g. the `Temporal` export from
* `@js-temporal/polyfill` or another polyfill, instead of relying on the
* global `Temporal` object.
*
* @default globalThis.Temporal
*/
temporal?: typeof Temporal
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
timezone |
string |
✓ | not shown |
calendar |
string |
✓ | not shown |
temporal |
typeof Temporal |
✓ | not shown |
UseTemporalNowReturn¶
Interface Code
export interface UseTemporalNowReturn extends Pausable {
/**
* Current `Temporal.ZonedDateTime`
*/
now: Ref<Temporal.ZonedDateTime>
/**
* Current timezone
*/
timezone: Ref<string>
/**
* Current calendar
*/
calendar: Ref<string>
/**
* Convert to a different timezone
*/
toTimezone: (timezone: string) => Temporal.ZonedDateTime
/**
* Convert to a different calendar
*/
toCalendar: (calendar: string) => Temporal.ZonedDateTime
/**
* Get the `Temporal.PlainDate` (date only)
*/
toPlainDate: () => Temporal.PlainDate
/**
* Get the `Temporal.PlainTime` (time only)
*/
toPlainTime: () => Temporal.PlainTime
/**
* Get the `Temporal.PlainDateTime` (local date/time)
*/
toPlainDateTime: () => Temporal.PlainDateTime
/**
* Format the current date/time
*/
format: (options?: Intl.DateTimeFormatOptions) => string
/**
* Add a duration
*/
add: (duration: Temporal.DurationLike) => Temporal.ZonedDateTime
/**
* Subtract a duration
*/
subtract: (duration: Temporal.DurationLike) => Temporal.ZonedDateTime
/**
* Compare with another date/time
*/
compare: (other: Temporal.ZonedDateTime | string) => number
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
now |
Ref<Temporal.ZonedDateTime> |
✗ | not shown |
timezone |
Ref<string> |
✗ | not shown |
calendar |
Ref<string> |
✗ | not shown |
toTimezone |
(timezone: string) => Temporal.ZonedDateTime |
✗ | not shown |
toCalendar |
(calendar: string) => Temporal.ZonedDateTime |
✗ | not shown |
toPlainDate |
() => Temporal.PlainDate |
✗ | not shown |
toPlainTime |
() => Temporal.PlainTime |
✗ | not shown |
toPlainDateTime |
() => Temporal.PlainDateTime |
✗ | not shown |
format |
(options?: Intl.DateTimeFormatOptions) => string |
✗ | not shown |
add |
(duration: Temporal.DurationLike) => Temporal.ZonedDateTime |
✗ | not shown |
subtract |
(duration: Temporal.DurationLike) => Temporal.ZonedDateTime |
✗ | not shown |
compare |
(other: Temporal.ZonedDateTime \| string) => number |
✗ | not shown |
Generated by Syntax Scribe