How to Change Date Format using Moment in Vue JS?

By Hardik Savani June 30, 2023 Category : Vue.JS

I want to show you chage date format using filter with moment in vue js app. If you are looking for change date formate from your default formate like yyyy-mm-dd hh mm ss. You can simply change your date time format using moment npm plugin.

As know, moment js is a very popular for date time. moment js will make simpler way to change date format, change timezone etc.

Here, you can easily display date with your formate. in this example we will convert date format yyyy-mm-dd hh:mm:ss to MM/DD/YYYY hh:mm.

Install moment

first we need to install moment npm package that will allow to change date formate in vue.js.

npm install moment

Use moment

We need to use moment package in main.js file of vue js app.

src/main.js

import Vue from 'vue'

import App from './App.vue'

import moment from 'moment'

Vue.config.productionTip = false

Vue.filter('formatDate', function(value) {

if (value) {

return moment(String(value)).format('MM/DD/YYYY hh:mm')

}

});

new Vue({

render: h => h(App),

}).$mount('#app')

Create Example Component

Here, we will create Example.vue component with following code.

src/components/Example.vue

<template>

<div class="container" style="text-align:center">

<div class="large-12 medium-12 small-12 cell">

<h1 style="font-family:ubuntu">Vue js toggle button example - ItSolutionStuff.com</h1>

<p>{{ yourDateString | formatDate}}</p>

</div>

</div>

</template>

<script>

export default {

data(){

return {

yourDateString: '2018-12-24 04:19:23'

}

}

}

</script>

Now you can run vue app by using following command:

npm run serve

I hope it can help you...

Shares