export function calcDaysBetweenDates(dateString1, dateString2) { const date1 = new Date(dateString1); const date2 = new Date(dateString2); const timeDifference = Math.abs(date2 - date1); // Calculate the time difference in milliseconds return Math.ceil(timeDifference / (1000 * 60 * 60 * 24)); // Convert milliseconds to days } export function convertDateToDateString(date) { // returns YYYY-MM-DD format if (date instanceof Date !== true) return null; return ( `${date.getFullYear() }-${ (`0${date.getMonth() + 1}`).slice(-2) }-${ (`0${date.getDate()}`).slice(-2)}` ); } export function convertDateStringToDate(dateString) { // dateString must be YYYY-MM-DD format if (typeof dateString !== 'string') return null; const dateParts = dateString.split('-'); return new Date(dateParts[0], parseInt(dateParts[1], 10) - 1, dateParts[2]); } export function getDateDifferenceInDays(startDate, endDate) { const date1 = new Date(endDate); date1.setHours(0, 0, 0, 0); const date2 = new Date(startDate); date2.setHours(0, 0, 0, 0); // To calculate the time difference of two dates const DifferenceInTime = date1.getTime() - date2.getTime(); // To calculate the no. of days between two dates return DifferenceInTime / (1000 * 3600 * 24); } export function getDisplayTextForDurationLength(durationMinimum, durationMaximum) { const isLongAppointment = durationMaximum >= 120; const isDurationRange = durationMinimum !== durationMaximum; const adjustedMinimum = isLongAppointment ? durationMinimum / 60 : durationMinimum; const adjustedMaximum = isLongAppointment ? durationMaximum / 60 : durationMaximum; const durationText = isDurationRange ? `${adjustedMinimum} - ${adjustedMaximum}` : adjustedMinimum; const unitText = isLongAppointment ? 'hours' : 'minutes'; return `${durationText} ${unitText}`; } export function militaryToTwelveHourTime(timeString) { // Expected input: "HH:MM" if (typeof timeString !== 'string') return null; let hours = parseInt(timeString.split(':')[0], 10); const minutes = timeString.split(':')[1]; const meridianNotation = hours > 11 ? 'PM' : 'AM'; if (hours > 12) { hours -= 12; } return `${hours}:${minutes} ${meridianNotation}`; } export function sumDateString(dateString, daysToAdd) { // dateString must be YYYY-MM-DD format if (typeof dateString !== 'string') return null; const date = convertDateStringToDate(dateString); date.setDate(date.getDate() + daysToAdd); return convertDateToDateString(date); } export function get12HourTimeMobileFormat(time) { // Check correct time format and split into components let timeString = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)?$/) || [time]; if (timeString.length > 1) { // If time format correct const min = timeString[3]; timeString = timeString.slice(1); // Remove full string match value if (Number(min) === 0) { timeString = timeString.slice(0, 1); // Remove minute value timeString[1] = +timeString[0] < 12 ? ' AM' : ' PM'; // Set AM/PM } else { timeString[5] = +timeString[0] < 12 ? ' AM' : ' PM'; // Set AM/PM } timeString[0] = +timeString[0] % 12 || 12; // Adjust hours } return timeString.join(''); // return adjusted time or original string } export function get12HourTimeFormat(time) { // Check correct time format and split into components let timeString = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)?$/) || [time]; if (timeString.length > 1) { // If time format correct timeString = timeString.slice(1); // Remove full string match value timeString[5] = +timeString[0] < 12 ? ' AM' : ' PM'; // Set AM/PM timeString[0] = +timeString[0] % 12 || 12; // Adjust hours } return timeString.join(''); // return adjusted time or original string } export function getDateFormat(date, format) { if (date instanceof Date !== true) return; const year = date.getFullYear(); const month = (`0${date.getMonth() + 1}`).slice(-2); const day = (`0${date.getDate()}`).slice(-2); const hour = (`0${date.getHours()}`).slice(-2); const minute = (`0${date.getMinutes()}`).slice(-2); const second = (`0${date.getSeconds()}`).slice(-2); // eslint-disable-next-line consistent-return return format .replace('yyyy', year) .replace('MM', month) .replace('dd', day) .replace('hh', hour) .replace('HH', hour) .replace('mm', minute) .replace('ss', second); } export function padTo2Digits(time) { // Use the built-in method toString() with a radix of 10 to convert the time value to a decimal string // eslint-disable-next-line no-param-reassign time = time.toString(10); // Use the conditional operator to check if the length of the string is less than 2 return time.length < 2 // If yes, prepend a '0' to the string and return it // If no, return the original string ? `0${time}` : time; } export function convertMsToTime(milliseconds) { let seconds = Math.floor(milliseconds / 1000); let minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); seconds %= 60; minutes %= 60; // commenting to get 24 time format // hours = hours % 24; return `${padTo2Digits(hours)}${padTo2Digits(minutes)}`; } export function calculateDuration(startDate, endDate) { if (startDate instanceof Date !== true) return; if (endDate instanceof Date !== true) return; // eslint-disable-next-line consistent-return return convertMsToTime(endDate - startDate); } export function combineDateAndTime(date, time) { // Use the Date.parse() method to convert the date and time strings to a numeric value const timestamp = Date.parse(`${date}T${time}`); // Use the new Date() constructor to create a new date object from the numeric value const newDate = new Date(timestamp); // Return the new date object return newDate; } export function addMinutes(date, minutes) { return new Date(date.getTime() + minutes * 60000); } export function shortTimeString(date) { // Use a ternary operator to check if the input is a valid date object return date instanceof Date // Use the built-in method toLocaleTimeString() to get the short time string in the current locale // Return undefined if the input is not a valid date object ? date.toLocaleTimeString('en-us', { hour: 'numeric', minute: 'numeric', hour12: true }) : undefined; } export function convertToSeconds({ years, months, weeks, days, hours, minutes, seconds }) { let total = seconds ?? 0; total += (minutes ?? 0) * 60; total += (hours ?? 0) * 60 * 60; total += (days ?? 0) * 24 * 60 * 60; total += (weeks ?? 0) * 7 * 24 * 60 * 60; total += (months ?? 0) * 30 * 24 * 60 * 60; total += (years ?? 0) * 365 * 24 * 60 * 60; return total; }