From 61b41c3aa470046cfae758920d8242d8597a0413 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Mon, 7 Jul 2025 15:25:13 +0530 Subject: [PATCH] added utils method that convert UTC TO Local --- src/utils/dateUtils.jsx | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/utils/dateUtils.jsx b/src/utils/dateUtils.jsx index fbe47e74..6a63b6a3 100644 --- a/src/utils/dateUtils.jsx +++ b/src/utils/dateUtils.jsx @@ -1,4 +1,5 @@ -// utils/dateUtils.js +import moment from "moment"; + export const getDateDifferenceInDays = (startDate, endDate) => { if (!startDate || !endDate) { throw new Error("Both startDate and endDate must be provided"); @@ -20,12 +21,12 @@ export const getDateDifferenceInDays = (startDate, endDate) => { return differenceInDays; }; -// export const formatDate = (date) => { -// if (!date) return ""; // Return an empty string if no date -// const dateObj = new Date(date); -// // return dateObj.toISOString().split("T")[0]; -// return dateObj.toLocaleDateString('en-CA'); // Get the date in YYYY-MM-DD format -// }; +export const formatDate = (date) => { + if (!date) return ""; // Return an empty string if no date + const dateObj = new Date(date); + // return dateObj.toISOString().split("T")[0]; + return dateObj.toLocaleDateString('en-CA'); // Get the date in YYYY-MM-DD format +}; export const convertShortTime = (dateString) => { const date = new Date(dateString); @@ -59,25 +60,8 @@ export const checkIfCurrentDate = (dateString) => { currentDate.setHours(0, 0, 0, 0); inputDate.setHours(0, 0, 0, 0); - return currentDate.getTime() === inputDate.getTime(); + return currentDate?.getTime() === inputDate?.getTime(); }; -function utcToLocalTime(utcTime) { - // Convert string to Date if needed - const utcDate = typeof utcTime === 'string' ? new Date(utcTime) : utcTime; - - return new Date(utcDate.getTime()); +export const formatUTCToLocalTime = (datetime) =>{ + return moment.utc(datetime).local().format("MMMM DD, YYYY [at] hh:mm A"); } - -export const formatDate =(utcTime, options = {}) =>{ - const localDate = utcToLocalTime(utcTime); - - const defaultOptions = { - day: '2-digit', - month: '2-digit', - year: 'numeric', - ...options - }; - - return localDate.toLocaleDateString('en-GB', defaultOptions); -} -