Necesito opciones de tiempo solo 11 AM to 1 PM (1:00:00 PM only) y si la hora del sistema es 1:00:01 PM . m. tampoco debería permitirse. si el tiempo pasó después de la 1 PM p. m., necesito obtener la disponibilidad de la opción como falsa. A partir de ahora, hasta 1:59:59 PM . Si la hora de mi sistema se movió a 2 PM solo se volvió falso. Sugiérame cómo obtener la solución para esto. Debo permitir solo la 1:00:00 PM . m. y no se me debe permitir ninguna otra opción.
public timeOptions = ['11 AM', '12 PM', '1 PM']; private currentTime: string[] = new Date() .toLocaleTimeString('en-US') .split(' '); private formattedTime: string = `${this.currentTime[0].split(':')[0]} ${ this.currentTime[1] }`; private timeIndexOnOptions: number = this.timeOptions.indexOf( this.formattedTime ); public timeOptionAvaialable(): void { let isOptionAvailable = this.timeIndexOnOptions != -1 ? true : false; console.log(isOptionAvailable);Parece que desea decidir si currentTime está en un intervalo. ¿Puedo entonces sugerir un enfoque alternativo? Cree un objeto de fecha que represente el comienzo del intervalo y un objeto de fecha que represente el final del intervalo y verifique si la hora actual está en ese intervalo.
const startHours = 11; const endHours = 13; // create a Date object for 11 AM today const startTime = new Date(); startTime.setHours(startHours, 0, 0, 0); // create a Date object for 1 PM today const endTime = new Date(); endTime.setHours(endHours, 0, 0, 0); // check if current time is in the interval const currentTime = new Date(); const isOptionAvailable = currentTime > startTime && currentTime < endTimeaquí hay una pieza de código
const timeOptions = ['11 AM', '12 PM', '1 PM']; const today = new Date().toLocaleTimeString('en-US').split(' ') const timeSplit = today[0].split(':') const index = timeOptions.indexOf(timeSplit[0] + ' ' + today[1]) // it will not wait for 1:00:00 it return false when 12:59:59 hit if (index > -1 && index !== (timeOptions.length - 1)) { console.log(true); } else { console.log(false); }