Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Configurando condicionales con moment.js

Estoy usando momentJS para analizar las fechas y horas en mi aplicación, pero me encontré con un problema al intentar establecer condicionales con las horas.

Por ejemplo: necesito validar un campo donde necesito que la hora de dicho campo no sea más de 30 minutos antes que la hora de otro campo (es una práctica aeronáutica, el piloto necesita validar su presencia no más de 30 minutos antes del el motor arranca).

Intenté establecer una var con el límite de tiempo y la consulta IsAfter, pero sigo obteniendo tiempos repetidos en lugar del resultado deseado de 'tiempo -30 minutos'. Aquí está el código.

 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 answers
Answer question

0

Probar:

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

En vez de:

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

Parece funcionar para mí: http://jsfiddle.net/x6q5br7k/6/

(Consulte la consola para ver las diferencias de fecha)

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!