How to Change Date Format using Moment in 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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Vue Js Sweetalert Modal Notification Example
- Vue JS Toggle Switch Button Example
- Vue Toastr Notifications Example Code
- File Upload using Vue js Axios PHP Example
- How to Open a Link in a New Tab in Vue JS?
- How to Check Object or Array Empty or Not in Vue JS?
- How to Get Element by ID in Vue JS?
- Laravel Vue Router Example From Scratch
- Laravel Vue Flash Message Example From Scratch
- Laravel Vue JS File Upload Using Vue-dropzone Example
- Laravel Vue JS Image Upload Example
- Laravel Vue JS Infinite Scroll Example with Demo
- Laravel Vue JS Pagination Example with Demo
- Dynamic Dependent Dropdown using VueJS and PHP