How to format date in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

Today, I am going to show you How do you format dates in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • Format date

This article will guide you to how do I format date in vueJS.

Setup the Vue (Optional)

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. the component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <div id="app">
    <FirstComponent />
  </div>
</template>

<script>
import FirstComponent from "./components/FirstComponent.vue";
export default {
  name: "App",
  components: {
    FirstComponent,
  },
};
</script>

Format date

To disable the button we need to add the disabled property to the button tag.

FirstComponent.js

<template>
  <div id="app">
    <p>Date without format: {{ date }}</p>
    <p v-if="datafarmat">Formated date: {{ formate }}</p>
    <button v-on:click="formatdate">Format Date</button>
  </div>
</template>
<script>
export default {
  name: "FirstComponent",
  data() {
    return {
      date: "2022-12-25 00:10:16.2451106",
      formate: "",
      datafarmat: false,
    };
  },
  methods: {
    formatdate() {
      const dateUTC = new Date(this.date);
      this.formate = dateUTC.toUTCString();
      this.datafarmat=true;
    },
  },
};
</script>

For now, let’s check the output.

formate date in vuejs

All the best 👍.

Follow me on Twitter