marco.pms.web/src/hooks/useFormattedDate.js
Vikas Nale beefe6396c - Add date format hook
- add Ui for Activities
2025-08-12 16:27:25 +05:30

27 lines
805 B
JavaScript

import { format } from "date-fns";
/**
* A custom hook to format a date string, object, or number.
* @param {string | Date | number} date - The date to be formatted.
* @param {string} formatString - The format string (e.g., 'MMMM do, yyyy').
* @returns {string} The formatted date string.
*/
const useFormattedDate = (date, formatString) => {
// Return early if no date is provided
if (!date) {
return "";
}
try {
const dateObj = new Date(date);
// You could also add error handling for invalid dates
// if (!isValid(dateObj)) return 'Invalid Date';
return format(dateObj, formatString);
} catch (error) {
console.error("Error formatting date:", error);
return ""; // Return an empty string or a default value on error
}
};
export default useFormattedDate;