Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
How to check current date is available or not in the date array in typescript

How to check current date is available or not in the date array in typescript. Tried to check with below code, If current is available in between the array dates, still its giving result as false. Kindly suggest me the solution.

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 Respuestas
Responde la pregunta

0

include only checks for an exact match on the array, between is not included

to use include you would have to populate the array with every possible time between the 2,

however you can use > >= and < <= to check for between

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

this is using an alphabetic compare not a date compare so will yield odd results, your should really convert to timestamps or compare the date components, to compare properly eg

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 Denunciar

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 Denunciar

0

A lot of the other answers here use string-based comparisons. To ensure the correct results, you should instead use numeric comparisons. Especially when checking between different days such as between 10PM and 2AM.

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

If you want to perform this check against many items, you should instead use currying to squeeze out some extra performance by pre-calculating the range to compare against.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda