added utils method that convert UTC TO Local

This commit is contained in:
Pramod Mahajan 2025-07-07 15:25:13 +05:30
parent 76aa9200f7
commit 61b41c3aa4

View File

@ -1,4 +1,5 @@
// utils/dateUtils.js import moment from "moment";
export const getDateDifferenceInDays = (startDate, endDate) => { export const getDateDifferenceInDays = (startDate, endDate) => {
if (!startDate || !endDate) { if (!startDate || !endDate) {
throw new Error("Both startDate and endDate must be provided"); throw new Error("Both startDate and endDate must be provided");
@ -20,12 +21,12 @@ export const getDateDifferenceInDays = (startDate, endDate) => {
return differenceInDays; return differenceInDays;
}; };
// export const formatDate = (date) => { export const formatDate = (date) => {
// if (!date) return ""; // Return an empty string if no date if (!date) return ""; // Return an empty string if no date
// const dateObj = new Date(date); const dateObj = new Date(date);
// // return dateObj.toISOString().split("T")[0]; // return dateObj.toISOString().split("T")[0];
// return dateObj.toLocaleDateString('en-CA'); // Get the date in YYYY-MM-DD format return dateObj.toLocaleDateString('en-CA'); // Get the date in YYYY-MM-DD format
// }; };
export const convertShortTime = (dateString) => { export const convertShortTime = (dateString) => {
const date = new Date(dateString); const date = new Date(dateString);
@ -59,25 +60,8 @@ export const checkIfCurrentDate = (dateString) => {
currentDate.setHours(0, 0, 0, 0); currentDate.setHours(0, 0, 0, 0);
inputDate.setHours(0, 0, 0, 0); inputDate.setHours(0, 0, 0, 0);
return currentDate.getTime() === inputDate.getTime(); return currentDate?.getTime() === inputDate?.getTime();
}; };
function utcToLocalTime(utcTime) { export const formatUTCToLocalTime = (datetime) =>{
// Convert string to Date if needed return moment.utc(datetime).local().format("MMMM DD, YYYY [at] hh:mm A");
const utcDate = typeof utcTime === 'string' ? new Date(utcTime) : utcTime;
return new Date(utcDate.getTime());
} }
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);
}