Skip to main content
  1. Posts/

How to format dates in Vue 3

·490 words·3 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.

If you’ve used Vue for any length of time, you soon find that it doesn’t have a lot of the fancy formatting options that some of the other frameworks seem to have out of the box. I get the feeling that Vue is very focused on minimalism and adding features that aren’t core to the framework is not something they want to do. One of those features, however, is formatting dates.

Have you ever had an ugly date from your data source formatted like "2019-07-16T20:32:21" and thought, “What’s the most straightforward way to make this not unreadable?”

If you’ve read my previous article on this—How to format dates in Vue 2—you know that I offered up the idea of using a Vue Filter to handle date formats. Well…the folks over in the Vue world didn’t like filters very much and killed them off when developing Vue 3. So, we’ll have to find another way of handling this now.

Luckily, there is an approach that we can take that will still give us most of what we want, using mixins, methods, and computed properties.

A Simple Date Formatter for Vue 3
#

First, specify a new Mixin in your code that will take a date string (preferably ISO formatted for maximum compatibility) and returns the date in the standard locale format as specified by Intl.DateTimeFormat:

export default {
    methods: {
        formatDate(dateString) {
            const date = new Date(dateString);
                // Then specify how you want your dates to be formatted
            return new Intl.DateTimeFormat('default', {dateStyle: 'long'}).format(date);
        }
    }
}

Then you can include that mixin in your component and either use it in a computed property or use it right in your template:

<template>
    <div>
      <p>
        {{ formattedDate }}
      </p>
      <p>
        {{ formatDate('2021-09-01T00:00:00-0400') }}
      </p>
    </div>
</template>
<script>
    import formatDateMixin from '../mixins/formatDateMixin.js';

    export default {
        data() {
            return {
                date: '2021-03-24T15:54:32-0400'
            }
        },
        mixins: [formatDateMixin],
        computed: {
            formattedDate() {
                return this.formatDate(this.date);
            }
        }

    }
</script>

More Power!
#

Need something a little more powerful? Including Day.js1 will give us what we need to format dates however we want. You can install it into your project with npm install dayjs.

Now, we can make our mixin into something like this:

import dayjs from 'dayjs';

export default {
    methods: {
        formatDate(dateString) {
            const date = dayjs(dateString);
                // Then specify how you want your dates to be formatted
            return date.format('dddd MMMM D, YYYY');
        }
    }
}

Use it the same way you used the other one.

Day.js has a lot of formatting options to choose from and can even do time from now type dates with an extra plugin.

So while Vue 3 got rid of filters, there are still easy ways to include date formatting in your application.

Check out this post’s code in this Repl


  1. Why Day.js and not Moment? Because Moment is trying to direct users towards better, lighter weight libraries. And Dayjs is only ~2kb and will give us what we need here. ↩︎

Related

How to Load Vue Components on Non-SPA Sites

·970 words·5 mins
There are times when you want to use Vue components, but the application you’re working on is still in the age of jQuery. Is there a way to start using Vue without switching everything to Vue and making a single page application? Can we get all of that beautiful, juicy component action without breaking everything else that’s currently on the site? Also, can we add Vue components without having to rewrite the entire website to use Vue?

What are services in Vue?

·444 words·3 mins
You might have heard the term “service” in relation to a Vue application. But looking through the Vue documentation, you might find that there isn’t any mention of what a service is at all.

How do you maintain multiple loading states in Vuex?

·602 words·3 mins
One of the best ways to improve the user experience in a dynamic application is by setting up loading icons for data coming into your Vuex store. But if you’ve ever tried to set this up on a page with a lot of components that are loading different dynamic data. How do you show the user that data is being loaded when that data might be coming from different, parallel API calls?