Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
How to format the date on typing in an input field in vuejs with moment?

I have a text field containing the date value in (MM/DD/YYYY) format. Whenever I type 01242022, it should be autoformatted and shown as 01/24/2022. I tried the following way where the value is shown as expected, but I want it in real-time. This is working when we change the focus of the input element. Any help is appreciated. Thanks

 Vue.filter("formatDate", function (value) {
        let dateValue = value;
        if (dateValue.length === 2 || dateValue.length === 5) {
          dateValue = value + "/";
        }
        return dateValue;
      });
      const app = new Vue({
        el: "#app",
        data() {
          return {
            date: new Date(),
          };
        },
        computed: {
          dateVal: {
            get() {
              // eslint-disable-next-line no-extra-boolean-cast
              if (!!this.date) return moment(this.date, "MMDDYYYY").format("L");
              return this.date;
            },
            set(value) {
              this.date = moment(value, "MMDDYYYY").format("L");;
            },
          },
        },
        methods: {
          onInput(e) {
            this.dateVal = this.$options.filters.formatDate(e.target.value);
          },
        },
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <script src="https://momentjs.com/downloads/moment.min.js"></script>
    <div id="app">
      <input
        type="text"
        v-model.lazy="dateVal"
        maxlength="10"
      />
    </div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use v-mask

<input
    v-mask="'##/##/####'"
    type="text"
    v-model.lazy="dateVal"
    maxlength="10"
  />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda