Skip to content
On this page

useDate

Reactive Date.now() with specified refresh rate

Parameters

typescript
import { useDate } from '@elonehoo/pistachio'

const now = useDate({
  refreshMs?: Number,
  sync?: Boolean
})
import { useDate } from '@elonehoo/pistachio'

const now = useDate({
  refreshMs?: Number,
  sync?: Boolean
})
ParametersTypeRequiredDefaultDescription
refreshMsnumberNo1000Refresh rate in milliseconds that the now gets updated
syncBooleanNotrueSync with the clock by the second

State

The useDate function exposes the following reactive state:

typescript
import { useDate } from '@elonehoo/pistachio'

const { now } = useDate()
import { useDate } from '@elonehoo/pistachio'

const { now } = useDate()
StateTypeDescription
nowRef<number>Current time

Methods

The useDate function exposes the following methods:

typescript
import { useDate } from '@elonehoo/pistachio'

const { remove } = useDate()
import { useDate } from '@elonehoo/pistachio'

const { remove } = useDate()
SignatureDescription
removeManually stop the now to be refreshed

Example

Current date: 1679286347975: Mon Mar 20 2023 04:25:47 GMT+0000 (Coordinated Universal Time)

vue
<script setup lang="ts">
import { useDate } from '@elonehoo/pistachio'

const { now } = useDate()
</script>

<template>
  <div>
    <p>
      Current date: {{ now }}: <b>{{ new Date(now) }}</b>
    </p>
  </div>
</template>
<script setup lang="ts">
import { useDate } from '@elonehoo/pistachio'

const { now } = useDate()
</script>

<template>
  <div>
    <p>
      Current date: {{ now }}: <b>{{ new Date(now) }}</b>
    </p>
  </div>
</template>

Released under the MIT License.