Skip to content

Instantly share code, notes, and snippets.

@matvaleriano
Last active February 6, 2023 22:57
Show Gist options
  • Save matvaleriano/e427b3964bc0e2bc04218bbc4a1da179 to your computer and use it in GitHub Desktop.
Save matvaleriano/e427b3964bc0e2bc04218bbc4a1da179 to your computer and use it in GitHub Desktop.
custom vue directive to formate date to locale
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