Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 131x 131x 131x 131x 131x 131x 131x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 131x 131x 131x 131x 131x 131x 131x | import moment from "moment";
import { RangeValue } from "./DateRangePicker.types";
import { getMomentDate } from "@utils/date.helpers";
export const formatInput = (val: string): string => {
const sanitized = val.replace(/\D/g, "");
if (sanitized.length < 2) return sanitized;
if (sanitized.length === 2) return `${sanitized}/`;
if (sanitized.length < 4) return formatPartialDate(sanitized);
if (sanitized.length === 4) return `${formatPartialDate(sanitized)}/`;
if (sanitized.length <= 8) return formatFullDate(sanitized);
if (sanitized.length < 10) return formatStartOfRange(sanitized);
if (sanitized.length === 10) return `${formatStartOfRange(sanitized)}/`;
if (sanitized.length < 12) return formatMiddleOfRange(sanitized);
if (sanitized.length === 12) return `${formatMiddleOfRange(sanitized)}/`;
if (sanitized.length <= 16) return formatDateRange(sanitized);
return formatDateRange(sanitized.substring(0, 16));
};
const formatPartialDate = (str: string): string =>
str.replace(/(\d{2})(\d{1,2})/, (_, m, d) => `${m}/${d}`);
const formatFullDate = (str: string): string =>
str.replace(/(\d{2})(\d{2})(\d{1,4})/, (_, m, d, y) => `${m}/${d}/${y}`);
const formatStartOfRange = (str: string): string =>
str.replace(
/(\d{2})(\d{2})(\d{4})(\d{0,2})/,
(_, m1, d1, y1, m2) => `${m1}/${d1}/${y1} - ${m2}`,
);
const formatMiddleOfRange = (str: string): string =>
str.replace(
/(\d{2})(\d{2})(\d{4})(\d{2})(\d{1,2})/,
(_, m1, d1, y1, m2, d2) => `${m1}/${d1}/${y1} - ${m2}/${d2}`,
);
const formatDateRange = (str: string): string =>
str.replace(
/(\d{2})(\d{2})(\d{4})(\d{2})(\d{2})(\d{1,4})/,
(_, m1, d1, y1, m2, d2, d3) => `${m1}/${d1}/${y1} - ${m2}/${d2}/${d3}`,
);
export const formatForInput = (value: RangeValue, useUTCMoment?: boolean) => {
let newInputValue = "";
let startDate = "";
let endDate = "";
Eif (value.startDate) {
if (useUTCMoment) {
startDate = getMomentDate(value.startDate).format("MM/DD/YYYY");
newInputValue += startDate;
} else E{
startDate = moment(value.startDate).format("MM/DD/YYYY");
newInputValue += startDate;
}
}
Eif (value.endDate) {
if (useUTCMoment) {
endDate = getMomentDate(value.endDate).format("MM/DD/YYYY");
newInputValue += value.endDate && ` - ${endDate}`;
} else E{
endDate = moment(value.endDate).format("MM/DD/YYYY");
newInputValue += ` - ${endDate}`;
}
}
return { newInputValue, startDate, endDate };
};
const maxMonths = 12;
const minMonths = 1;
const maxYears = new Date().getFullYear() + 100;
const minYears = 1900;
export const fixDate = ({
dateString,
minDate,
maxDate,
}: {
dateString: string;
minDate?: Date;
maxDate?: Date;
}) => {
// Convert minDate & maxDate to their respective parts
const minYear = minDate ? minDate.getFullYear() : minYears;
const minMonth = minDate ? minDate.getMonth() + 1 : minMonths;
const minDay = minDate ? minDate.getDate() : 1;
const maxYear = maxDate ? maxDate.getFullYear() : maxYears;
const maxMonth = maxDate ? maxDate.getMonth() + 1 : maxMonths;
const maxDay = maxDate ? maxDate.getDate() : 31; // Default max day
return dateString
.split(" - ")
.map((date) => {
const parts = date.split("/");
let month = parts[0] || ""; // MM
let day = parts[1] || ""; // DD
let year = parts[2] || ""; // YYYY
// ✅ Ensure Year is within the allowed range
if (year.length === 4) {
let yearNum = parseInt(year, 10);
yearNum = Math.max(minYear, Math.min(yearNum, maxYear));
year = String(yearNum);
}
// ✅ Ensure Month is within valid range
if (month.length === 2) {
let monthNum = parseInt(month, 10);
if (year === String(minYear)) monthNum = Math.max(monthNum, minMonth);
if (year === String(maxYear)) monthNum = Math.min(monthNum, maxMonth);
monthNum = Math.max(minMonths, Math.min(monthNum, maxMonths));
month = String(monthNum).padStart(2, "0");
}
// ✅ Ensure Day is within valid range for the given Month & Year
if (day.length === 2 && month.length === 2) {
const monthNum = parseInt(month, 10);
let dayNum = parseInt(day, 10);
const daysInMonth = new Date(
parseInt(year) || minYears,
monthNum,
0,
).getDate();
if (year === String(minYear) && monthNum === minMonth) {
dayNum = Math.max(dayNum, minDay);
}
if (year === String(maxYear) && monthNum === maxMonth) {
dayNum = Math.min(dayNum, maxDay);
}
dayNum = Math.max(1, Math.min(dayNum, daysInMonth));
day = String(dayNum).padStart(2, "0");
}
return [month, day, year].filter(Boolean).join("/");
})
.join(" - ");
};
export const validateDateInput = (value: string) => {
const datePattern = "(0[1-9]|1[0-2])/([0-2][0-9]|3[01])/\\d{4}";
const regex = new RegExp(
`^(${datePattern}|${datePattern} - ${datePattern})$`,
);
if (!regex.test(value)) {
return "Invalid date format. Please use mm/dd/yyyy or mm/dd/yyyy - mm/dd/yyyy";
}
return true;
};
export function isValidDateRange(str: string) {
const regex = /^\d{2}\/\d{2}\/\d{4} - \d{2}\/\d{2}\/\d{4}$/;
return regex.test(str);
}
export const stringToDate = (dateStr: string) => {
const isValid = validateDateInput(dateStr);
if (!isValid) return {};
const [firstDate, secondDate] = dateStr.split(" - ");
let startDate;
let endDate;
if (firstDate) {
const [month, day, year] = firstDate.split("/").map(Number);
startDate = new Date(year, month - 1, day);
}
if (secondDate) {
const [month, day, year] = secondDate.split("/").map(Number);
endDate = new Date(year, month - 1, day);
}
return { startDate, endDate };
};
|