ItSolutionStuff.com

How to Change Date Format using Moment in Vue JS?

By Hardik Savani • June 30, 2023
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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Vue Js Sweetalert Modal Notification Example

Read Now →

Vue JS Toggle Switch Button Example

Read Now →

Vue Toastr Notifications Example Code

Read Now →

File Upload using Vue js Axios PHP Example

Read Now →

How to Open a Link in a New Tab in Vue JS?

Read Now →

How to Check Object or Array Empty or Not in Vue JS?

Read Now →

How to Get Element by ID in Vue JS?

Read Now →

Laravel Vue Router Example From Scratch

Read Now →

Laravel Vue Flash Message Example From Scratch

Read Now →

Laravel Vue JS File Upload Using Vue-dropzone Example

Read Now →

Laravel Vue JS Image Upload Example

Read Now →

Laravel Vue JS Infinite Scroll Example with Demo

Read Now →

Laravel Vue JS Pagination Example with Demo

Read Now →

Dynamic Dependent Dropdown using VueJS and PHP

Read Now →