Modern JavaScript date utility library - date-fns for Nuxt.js
@nuxtjs/date-fns
dependency to your projectyarn add @nuxtjs/date-fns # or npm install @nuxtjs/date-fns
@nuxtjs/date-fns
to the buildModules
section of nuxt.config.js
export default {
buildModules: [
// Simple usage
'@nuxtjs/date-fns',
// With options
['@nuxtjs/date-fns', { /* module options */ }]
]
}
:warning: If you are using Nuxt < v2.9 you have to use 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.
:warning: Use @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.
locales
Array[String]
[]
Locales to be imported.
defaultLocale
String
null
You can preset default locale.
fallbackLocale
String
null
You can preset a fallback locale for when a method is called with an unsupported locale.
format
String
null
You can preset default format.
methods
Array
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>
yarn install
or npm install
npm run dev
Copyright (c) Nuxt Community