[READ-ONLY] Mirror of https://github.com/danielroe/nuxt-time. ⏰ SSR-safe time element for Nuxt 3
date nuxt nuxt-module server ssr
0

Configure Feed

Select the types of activity you want to include in your feed.

Vue 46.5%
TypeScript 41.0%
JavaScript 3.8%
Other 8.7%
452 2 11

Clone this repository

https://git.vm.fail/danielroe.dev/nuxt-time https://git.vm.fail/did:plc:n76s36qb5zpfayjlygml66zv
ssh://git@knot1.tangled.sh:2222/danielroe.dev/nuxt-time ssh://git@knot1.tangled.sh:2222/did:plc:n76s36qb5zpfayjlygml66zv

For self-hosted knots, clone URLs may differ based on your setup.


README.md

Nuxt Time#

IMPORTANT

This module is no longer necessary as <NuxtTime> is built into Nuxt from v3.17 onwards.

npm version npm downloads Github Actions Codecov

SSR-safe time element for Nuxt

Features#

  • ✨ SSR/SSG-safe rendering of any date/time
  • 💪 Prevents hydration mismatch on client
  • 🏁 Respects browser locale
  • ✅ Renders semantic <time> element

Installation#

Install and add nuxt-time to your nuxt.config.

npx nuxi@latest module add time
export default defineNuxtConfig({
  modules: ['nuxt-time'],
})

Usage#

To use, you can use the <NuxtTime> component anywhere in your app. It will render a spec-compliant <time> element.

It accepts datetime and locale (optional) properties, along with any property accepted by Intl.DateTimeFormat (see MDN reference). Note: you can provide a Date or number (e.g. Date.now()) via datetime and it will be rendered correctly as an ISO-formatted date.

<template>
  <!--
    Date.now() will safely respect the time on the server, not on the
    client. What's more, there will be no flash between server
    and client locale formatting.
  -->
  <NuxtTime :datetime="Date.now()" second="numeric" month="long" day="numeric" />
</template>

Relative Time Formatting#

Nuxt Time also supports relative time formatting using the Intl.RelativeTimeFormat API. You can enable this feature by setting the relative prop to true.

<template>
  <!--
    This will display the time relative to the current time, e.g., "5 minutes ago"
  -->
  <NuxtTime :datetime="Date.now() - 5 * 60 * 1000" relative />
</template>

💻 Development#

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Stub module with pnpm dev:prepare
  • Run pnpm dev to start playground in development mode

License#

Made with ❤️

Published under the MIT License.