Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

0

you can use v-mask

<input
    v-mask="'##/##/####'"
    type="text"
    v-model.lazy="dateVal"
    maxlength="10"
  />
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda