Last active
February 6, 2023 22:57
-
-
Save matvaleriano/e427b3964bc0e2bc04218bbc4a1da179 to your computer and use it in GitHub Desktop.
custom vue directive to formate date to locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DEFAULT_LOCALE = "pt-BR"; | |
/* | |
* Custom vue directive to format date in text content | |
*/ | |
function localeDate(el, binding) { | |
const locale = binding.value || DEFAULT_LOCALE; | |
el.textContent = new Date(el.textContent).toLocaleDateString(locale); | |
} | |
/* how to configure */ | |
app.directive('localeDate', { mounted: localeDate }) | |
/* how to use */ | |
// <span v-locale-date>2023-02-06T22:57:13.911Z</span> | |
// <span v-locale-date>{{ myDateVar }}</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment