How to use Moment.js with Vue.js?

Sometimes, we want to use Moment.js with Vue.js.

In this article, we’ll look at how to use Moment.js with Vue.js.

How to use Moment.js with Vue.js?

To use Moment.js with Vue.js, we can call the moment function in our code.

For instance, we write

<template>
  <div>
    {{ moment().format("MMMM Do YYYY, h:mm:ss a") }}
  </div>
</template>

<script>
import moment from "moment";

export default {
  methods: {
    moment(...args) {
      return moment(...args);
    },
  },
};
</script>

to define the moment method that calls the moment function and returns its result.

We spread the args arguments array into the moment function we imported.

Then we can use the moment method in our template code with

{{ moment().format("MMMM Do YYYY, h:mm:ss a") }}

Conclusion

To use Moment.js with Vue.js, we can call the moment function in our code.