4

So I'm new to VueJs and I'm trying to create a global directive to format date using moment.js. My problem is that the directive is not getting triggered. Idk if I'm calling it right or not.

dateFormat.js

import Vue from "vue";
import moment from "moment";

Vue.directive("formatdate", function(value) {
 if (value) {
 return moment(String(value)).format("MM/DD/YYYY hh:mm");
 }
});

itemList.vue

<span v-formatdate>{{ item.date_added }}</span>

1 Answer 1

5

It looks like you need filters instead of directives:

Vue.filter("formatdate", // function definition here)

And in your template, use it as:

<span>{{ item.date_added | formatdate }}</span>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.