Modern JavaScript date utility library - date-fns for Nuxt.js
- Add
@nuxtjs/date-fns
dependency to your project
yarn add @nuxtjs/date-fns # or npm install @nuxtjs/date-fns
- Add
@nuxtjs/date-fns
to thebuildModules
section ofnuxt.config.js
export default {
buildModules: [
// Simple usage
'@nuxtjs/date-fns',
// With options
['@nuxtjs/date-fns', { /* module options */ }]
]
}
modules
section in nuxt.config.js
instead of buildModules
.
export default {
buildModules: [
'@nuxtjs/date-fns'
],
dateFns: {
/* module options */
}
}
Add the types to your "types"
array in tsconfig.json
after the @nuxt/types
entry.
@nuxt/vue-app
instead of @nuxt/types
for nuxt < 2.9.
{
"compilerOptions": {
"types": [
"@nuxt/types",
"@nuxtjs/date-fns"
]
}
}
Why?
For typescript to be aware of the additions to the
nuxt Context
, thevue instance
and thevuex store
, the types need to be merged via declaration merging by adding@nuxtjs/date-fns
to your types.
- Type:
Array[String]
- Default:
[]
Locales to be imported.
- Type:
String
- Default:
null
You can preset default locale.
- Type:
String
- Default:
null
You can preset a fallback locale for when a method is called with an unsupported locale.
- Type:
String
- Default:
null
You can preset default format.
- Type:
Array
- Default:
null
Methods to be imported. If not defined all methods are imported.
This module inject $dateFns
to your project:
<template>
<div>
// Using default format and locale
{{ $dateFns.format(new Date()) }}
// Using custom format
{{ $dateFns.format(new Date(), 'yyyy-MM-dd') }}
// Using custom format and locale
{{ $dateFns.format(new Date(), 'yyyy-MM-dd', { locale: 'ru' }) }}
// Using asyncData
{{ dateFormatted }}
</div>
</template>
<script>
export default {
asyncData({ $dateFns }) {
return {
dateFormatted: $dateFns.format(new Date())
}
}
}
</script>
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
npm run dev
Copyright (c) Nuxt Community