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

168
Views
Cómo verificar que la fecha actual esté disponible o no en la matriz de fechas en mecanografiado

Cómo verificar que la fecha actual esté disponible o no en la matriz de fechas en mecanografiado. Intenté verificar con el código a continuación, si la corriente está disponible entre las fechas de la matriz, sigue dando un resultado falso. Amablemente sugiérame la solución.

 let time1= ['5:00:00 PM','5:59:59 PM']; const dateCheck = new Date().toLocaleTimeString('en-US'); const status = time1.includes(dateCheck) ?true:false; console.log(status);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

incluir solo controles para una coincidencia exacta en la matriz, entre no está incluido

para usar include tendrías que llenar la matriz con cada tiempo posible entre el 2,

sin embargo, puede usar > >= y < <= para buscar entre

 const dateCheck:date = new Date().toLocaleTimeString('en-US'); const time1:string[] = ['12:00:00 PM','12:59:59 PM']; const status: boolean = (time1[0]<=dateCheck && time1[1]>=dateCheck) console.log(dateCheck,status); //output "12:56:13 PM", true

esto está usando una comparación alfabética, no una comparación de fecha, por lo que arrojará resultados extraños, realmente debería convertir a marcas de tiempo o comparar los componentes de fecha, para comparar correctamente, por ejemplo

 const dateCheck:date = new Date() const time1 = [{ hour:12, minute:0 },{ hour:17, minute:59 }] //convert to number of minutes since midnight so you can use a numeric compare const dayMins = dateCheck.getHours()*60+ dateCheck.getMinutes() const start = time1[0].hour*60+ time1[0].minute const end = time1[1].hour*60+ time1[1].minute const status = ( dayMins>=start && dayMins<=end ) console.log(dateCheck.toLocaleTimeString(),status)
about 4 years ago · Juan Pablo Isaza Report

0

 let times = ['5:00:00 PM', '5:59:59 PM']; startTime = new Date('2022-01-01 ' + times[0]).getTime() endTime = new Date('2022-01-01 ' + times[1]).getTime() testTime = new Date('2022-01-01 ' + '5:30:00 PM').getTime() const isBetween = testTime > startTime && testTime < endTime;

about 4 years ago · Juan Pablo Isaza Report

0

Muchas de las otras respuestas aquí usan comparaciones basadas en cadenas. Para garantizar los resultados correctos, debe usar comparaciones numéricas. Especialmente al verificar entre diferentes días, como entre las 10 p. m. y las 2 a. m.

 function checkTime(timeCheck, timeRange) { const datePart = '2000-01-01 ', // used to anchor times to the same date d0 = new Date(datePart + timeRange[0]).getTime(), d1 = new Date(datePart + timeRange[1]).getTime(), dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime(); if (isNaN(d0) || isNaN(d1) || isNaN(dToCheck)) throw new TypeError('invalid time format'); return d0 < d1 ? d0 <= dToCheck && dToCheck <= d1 // handle times on the same day : d1 <= dToCheck || dToCheck <= d0; // handle times on different days } const timeRange = ['5:00:00 PM','5:59:59 PM']; checkTime('5:15:00 PM', timeRange); // true checkTime('5:15 PM', timeRange); // true checkTime('5:15:00 AM', timeRange); // false checkTime('5:15 AM', timeRange); // false checkTime(new Date().toLocaleTimeString('en-US'), timeRange); // depends checkTime(new Date(), timeRange); // depends checkTime(new Date('2021-01-01T12:00:00Z'), timeRange); // depends on timezone

Si desea realizar esta verificación con muchos elementos, debe usar curry para obtener un rendimiento adicional al calcular previamente el rango con el que comparar.

 function buildCheckTime(startTime, endTime) { const datePart = '2000-01-01 ', // used to anchor times to the same date d0 = new Date(datePart + startTime).getTime(), d1 = new Date(datePart + endTime).getTime(); if (isNaN(d0) || isNaN(d1)) throw new TypeError('invalid time format'); return d0 < d1 ? function(timeCheck) { // handle times on the same day const dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime(); if (isNaN(dToCheck)) throw new TypeError('invalid time format'); return d0 <= dToCheck && dToCheck <= d1; } : function (timeCheck) { // handle times on different days const dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime(); if (isNaN(dToCheck)) throw new TypeError('invalid time format'); return d1 <= dToCheck || dToCheck <= d0; } } const checkTime = buildCheckTime('5:00:00 PM','5:59:59 PM'); checkTime('5:15:00 PM'); // true checkTime('5:15 PM'); // true checkTime('5:15:00 AM'); // false checkTime('5:15 AM'); // false checkTime(new Date().toLocaleTimeString('en-US')); // depends checkTime(new Date()); // depends checkTime(new Date('2021-01-01T12:00:00Z')); // depends on timezone
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!