javascriptvue.jsvuetify.js

How to change the date format of v-text-field


I'm working on a Vue.js project using Vuetify, and I'm encountering an issue with the default date format of the v-text-field with the type "date." Currently, the format is mm/dd/yyyy, but I need it to be yyyy/mm/dd.

Here's the code for the date field:

<v-text-field
  type="date"
  label="From Date"
  v-model="from_date"
  ref="fromDateField"
></v-text-field>

Solution

  • <template>
      <v-menu
        v-model="menu"
        :close-on-content-click="false"
        :nudge-right="40"
        transition="scale-transition"
        offset-y
      >
        <template v-slot:activator="{ on }">
          <v-text-field
            v-model="formattedDate"
            label="From Date"
            readonly
            v-on="on"
          ></v-text-field>
        </template>
        <v-date-picker v-model="from_date" @input="menu = false"></v-date-picker>
      </v-menu>
    </template>
    
    <script>
    export default {
      data() {
        return {
          menu: false,
          from_date: null,
        };
      },
      computed: {
        formattedDate() {
          if (!this.from_date) return null;
          const [year, month, day] = this.from_date.split('-');
          return `${year}/${month}/${day}`;
        },
      },
    };
    </script>