@kadconsulting/dry
Version:
KAD Reusable Component Library
26 lines • 1.31 kB
JavaScript
// dateTimeStr: "May 8, 2024 - 9:00 AM"
const isWithin24HoursOfNowDenver = (dateTimeStr) => {
if (!dateTimeStr || dateTimeStr === '' || dateTimeStr === null) {
return false;
}
// Get the current time in the Denver time zone
const nowDenver = new Date().toLocaleString('en-US', {
timeZone: 'America/Denver',
});
const nowDenverDate = new Date(nowDenver);
// const formattedDenverDate = formatDateFromIso(nowDenverDate.toISOString()); // "May 9, 2024 - 9:00 AM"
// Parse the input datetime string
const [inputDate, inputTime] = dateTimeStr.split(' - ');
const [monthDay, year] = inputDate.split(', ');
const [month, day] = monthDay.split(' ');
const [hour, minute, ampm] = inputTime.split(/:| /);
const inputHour = ampm === 'PM' && hour !== '12' ? parseInt(hour) + 12 : parseInt(hour);
const inputDateTime = new Date(`${month} ${day}, ${year} ${inputHour}:${minute}:00`);
// Calculate the time difference in hours
const timeDiffHours = Math.abs(nowDenverDate.getTime() - inputDateTime.getTime()) /
(1000 * 60 * 60);
// Return true if the time difference is within 24 hours, false otherwise
return timeDiffHours <= 24;
};
export default isWithin24HoursOfNowDenver;
//# sourceMappingURL=isWithin24HoursOfNowDenver.js.map