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

144
Visualizações
moment().isValid returns true when the input is in integers

Code:

  let modifiedValue = attribute_value;
  let isISOFormat = moment(new Date(attribute_value), moment.ISO_8601, true).isValid();
  if (isISOFormat) {
    modifiedValue = formatDateShortCustom(moment(attribute_value).toDate());
  }
  return modifiedValue;
};

In the code above, the attribute value can have

  1. 2021-09-29T18:30:00.000Z
  2. 080921

I want to make sure that when the attribute_value is in (1) format the code
modifiedValue = formatDateShortCustom(moment(attribute_value).toDate()); should execute.

However, this numerical string is also causing the isISOFormat as true hence navigating the formatDateShortCustom method, which I don't want?

Is there anything I am doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I'd suggest wrapping this in a function isISOFormat() and using this to decide whether your input is an ISO 8601 date string.

I'd also suggest passing the input directly to the moment() constructor, rather than creating a Date first:

let attribute_values = ['2021-09-28T11:45:00Z', new Date().toISOString(), 20, null, '28/09/2021', 'is this a date?'];

function testIsISOFormat() {
    for(let attribute_value of attribute_values) {
        console.log(`isISOFormat(${attribute_value}):`, isISOFormat(attribute_value));
    }
}

function isISOFormat(input) {
    return moment(input, moment.ISO_8601, true).isValid();
}

console.log('Testing isISOFormat:');
testIsISOFormat()
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

moment.ISO_8601 is undefined below version 2.7.0.

That's why moment attempts to parse the value regardless of your setting the strict parsing option. moment checks if the string matches known ISO 8601 formats.

I recommend passing the format as the second argument explicitly.

console.log(moment('2021-09-29T18:30:00.000Z', 'YYYY-MM-DDTHH:mm:ss.SSSZ', true).isValid());

console.log(moment('080921', 'YYYY-MM-DDTHH:mm:ss.SSSZ', true).isValid());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

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