
Day.js Nuxtモジュール
Moment.jsの代替となる、同じモダンAPIを備えた高速な2kBライブラリ
Day.js Nuxtモジュール (v3対応)
機能
- ⛰ Nuxt 3対応
- 🚠 利用可能な任意のプラグインまたはロケールを有効化
- 🌲 デフォルトのロケールとタイムゾーンを指定
クイックセットアップ
- プロジェクトに
dayjs-nuxtの依存関係を追加
npx nuxi@latest module add dayjs
dayjs-nuxtをnuxt.config.tsのmodulesセクションに追加
export default defineNuxtConfig({
modules: [
'dayjs-nuxt'
]
})
基本的な使い方
テンプレートでDay.jsにアクセスするには、提供されている$dayjsを使用できます。
<template>
<div>
<time :datetime="$dayjs('2023-01-01').utc().toString()"> {{ date }} </time>
</div>
</template>
コンポーザブル
どこからでもDay.jsにアクセスするには、useDayjsコンポーザブルを使用できます。
<script setup>
import { useDayjs } from '#dayjs' // not need if you are using auto import
const dayjs = useDayjs()
dayjs.locale('fr')
dayjs.extend(...)
</script>
設定
任意の数のロケール、プラグインを指定し、デフォルトロケールとデフォルトタイムゾーンを設定できます。
export default defineNuxtConfig({
modules: ['dayjs-nuxt'],
dayjs: {
locales: ['en', 'fr'],
plugins: ['relativeTime', 'utc', 'timezone'],
defaultLocale: 'en',
defaultTimezone: 'America/New_York',
}
})
デフォルトでは、relativeTimeとutcプラグインを含み、常にupdateLocaleをインポートします。
外部プラグイン
export default defineNuxtConfig({
modules: ['dayjs-nuxt'],
dayjs: {
...
externalPlugins: [{
name: 'dayjsBusinessDays',
package: 'dayjs-business-days2',
option: {
holidays: [`2023-12-26`],
holidayFormat: `YYYY-MM-DD`,
}
}]
...
}
})
オプションのdefaultLocaleカスタマイズ
defaultLocale:のロケール文字列の代わりに、カスタムロケールを含む配列を定義できます。詳細については、dayjsカスタマイズを参照してください。
relativeTime設定の例を次に示します。これはゴラムが理解できるようなものを作成してみましょう。
export default defineNuxtConfig({
modules: ['dayjs-nuxt'],
dayjs: {
...
defaultLocale: ['en', {
relativeTime: {
future: "in %s",
past: "%s ago",
s: 'a few secondses',
m: "a minute",
mm: "%d minuteses",
h: "an hour",
hh: "%d hourses",
d: "a day",
dd: "%d dayses",
M: "a month",
MM: "%d monthseses",
y: "a year",
yy: "%d yearseses"
}
}]
...
}
})
開発
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Release new version
npm run release