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

141
Visualizações
Setting up conditionals with moment.js

I'm using momentJS to parse the dates and times in my application, but I ran into a problem when trying to set conditionals with the times.

For example: I need to validate a field where I need the time of said field to be no more than 30 minutes before the time of another field (it's an aviation practice, the pilot needs to validate his presence no more than 30 minutes before the engine starts).

I've tried to set a var with the time limit and the IsAfter query, but I keep getting repeating times instead of the desired result of 'time -30 minutes'. Here's the code.

var firstTime = new moment(
  this._dateTimeToDateSQL(
    this.form.acionamento,
    $sessao.getDateFormat() + ' HH:MM'
  )
)

var secondTime = new moment(
  this._dateTimeToDateSQL(
    this.form.tripulacao[0].apresentacao,
    $sessao.getDateFormat() + ' HH:MM'
  )
)

var timeLimit = moment(firstTime.subtract(30, 'minutes'))

if (moment(secondTime).isAfter(firstTime)) {
  this.form.tripulacao[0].err = 'error'
  pass = false
} else if (moment(secondTime).isAfter(timeLimit)) {
  this.form.tripulacao[0].err =
    'time limit reached'
  pass = false
} else {
  this.form.tripulacao[0].err = ''
}

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

0

Try:

var timeLimit = moment(firstTime).subtract(30, 'minutes');

Instead of:

var timeLimit = moment(firstTime.subtract(30, 'minutes'));

Seems to work for me: http://jsfiddle.net/x6q5br7k/6/

(Check the console to see the date differences)

let now        = new Date(),
    firstTime  = moment(new Date(now.getTime() + 60 * 60000)), // now plus an hour
    secondTime = moment(now),                                  // now
    timeLimit  = moment(firstTime).subtract(30, 'minutes');    // 30 minutes from now
    
console.log(
    "firstTime:"  + firstTime.toString(), 
    "secondTime:" + secondTime.toString(), 
    "timeLimit:"  + timeLimit.toString()
)

alert(
    moment(secondTime).isAfter(firstTime)
        ? 'error'
        : moment(secondTime).isAfter(timeLimit)
            ? 'time limit reached'
            : ''
);
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