|
|
|
@@ -335,6 +335,59 @@ export const generateDateArray = (startDate: string | Date, endDate: string | Da
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {string | null} formatted date in the format of yyyy-mm-dd to be used in payload
|
|
|
|
|
* @description Returns date in the formatted format to be used in payload
|
|
|
|
|
* @param {Date | string} date
|
|
|
|
|
* @example renderFormattedPayloadDate("Jan 01, 20224") // "2024-01-01"
|
|
|
|
|
*/
|
|
|
|
|
export const renderFormattedPayloadDate = (date: Date | string | undefined | null): string | undefined => {
|
|
|
|
|
// Parse the date to check if it is valid
|
|
|
|
|
const parsedDate = getDate(date);
|
|
|
|
|
// return if undefined
|
|
|
|
|
if (!parsedDate) return;
|
|
|
|
|
// Check if the parsed date is valid before formatting
|
|
|
|
|
if (!isValid(parsedDate)) return; // Return null for invalid dates
|
|
|
|
|
// Format the date in payload format (yyyy-mm-dd)
|
|
|
|
|
const formattedDate = format(parsedDate, "yyyy-MM-dd");
|
|
|
|
|
return formattedDate;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {string} safely formatted date or fallback text
|
|
|
|
|
* @description Safely formats a date using renderFormattedPayloadDate and date-fns format, with fallback for invalid dates
|
|
|
|
|
* @param {Date | string | undefined | null} date
|
|
|
|
|
* @param {string} formatToken (optional) // default "MMM dd, yyyy"
|
|
|
|
|
* @param {string} fallback (optional) // default "Invalid date"
|
|
|
|
|
* @example renderSafeFormattedDate("2024-01-01") // "Jan 01, 2024"
|
|
|
|
|
* @example renderSafeFormattedDate(null) // "Invalid date"
|
|
|
|
|
* @example renderSafeFormattedDate("2024-01-01", "MM/dd/yyyy", "N/A") // "01/01/2024"
|
|
|
|
|
*/
|
|
|
|
|
export const renderSafeFormattedDate = (
|
|
|
|
|
date: Date | string | undefined | null,
|
|
|
|
|
formatToken: string = "MMM dd, yyyy",
|
|
|
|
|
fallback: string = "Invalid date"
|
|
|
|
|
): string => {
|
|
|
|
|
if (!date) return fallback;
|
|
|
|
|
|
|
|
|
|
// Use renderFormattedPayloadDate to get a properly formatted payload date
|
|
|
|
|
const payloadDate = renderFormattedPayloadDate(date);
|
|
|
|
|
|
|
|
|
|
// If renderFormattedPayloadDate returns undefined/null, return fallback
|
|
|
|
|
if (!payloadDate) return fallback;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Parse and format the payload date
|
|
|
|
|
const parsedDate = getDate(payloadDate);
|
|
|
|
|
if (!parsedDate || !isValid(parsedDate)) return fallback;
|
|
|
|
|
|
|
|
|
|
return format(parsedDate, formatToken);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Return fallback if any error occurs during formatting
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Formats merged date range display with smart formatting
|
|
|
|
|
* - Single date: "Jan 24, 2025"
|
|
|
|
|
* - Same year, same month: "Jan 24 - 28, 2025"
|
|
|
|
@@ -388,4 +441,4 @@ export const formatDateRange = (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|