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

319
Visualizações
isSame() function in moment.js or Date Validation

I need to validate the date from the user and check if it is in a particular format. If yes, then it will be accepted else it will not be. I am looking for sort of

value.match("regular expression") 

The above works fine if, I have to choose from few formats. So, I came across this moment.js and interested in knowing how to use isSame(). I tried implementing it but unsuccessful. Like :

var x=moment("MM/DD/YYYY") ;
x.isSame("28-02-1999");  // am getting false which is right
var x=moment("28-02-1999","DD-MM-YYYY") ;
x.isSame("28-02-1999");  // am getting false which is wrong

So, please help in that. Thanks

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Docs - Is Same

Check if a moment is the same as another moment.

moment('2010-10-20').isSame('2010-10-20'); // true

If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.

moment('2010-10-20').isSame('2009-12-31', 'year'); // false
moment('2010-10-20').isSame('2010-01-01', 'year'); // true
moment('2010-10-20').isSame('2010-12-31', 'year'); // true
moment('2010-10-20').isSame('2011-01-01', 'year'); // false

Your code

var x=moment("28-02-1999","DD-MM-YYYY"); // working
x.isSame("28-02-1999"); // comparing x to an unrecognizable string

If you try moment("28-02-1999"), you get an invalid date. So comparing x to an invalid date string returns false.

To fix it, either use the default date format (ISO 8601):

var x = moment("28-02-1999","DD-MM-YYYY");
x.isSame("1999-02-28"); // YYYY-MM-DD

Or pass isSame a moment object.

var x = moment("28-02-1999","DD-MM-YYYY");
x.isSame( moment("28-02-1999","DD-MM-YYYY") );
over 4 years ago · Santiago Trujillo 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