فهرست منبع

feat(date-formatting): enhance formatDateYMDtoDMY function with validation and normalization

ebagabee 1 هفته پیش
والد
کامیت
15d8a821f8
1فایلهای تغییر یافته به همراه12 افزوده شده و 3 حذف شده
  1. 12 3
      src/helpers/utils.js

+ 12 - 3
src/helpers/utils.js

@@ -68,12 +68,21 @@ const convertDateTime = (dateTimeString) => {
  * // Output: 23/05/2023 10:07:27
  */
 const formatDateYMDtoDMY = (dateTime) => {
-  const [datePart, timePart] = dateTime.split(" ");
+  if (!dateTime || typeof dateTime !== "string") return "-";
+
+  const normalizedDateTime = dateTime.trim().replace("T", " ");
+  if (!normalizedDateTime) return "-";
+
+  const [datePart, timePart] = normalizedDateTime.split(" ");
   const [year, month, day] = datePart.split("-");
+  if (!year || !month || !day) return "-";
+
   const formattedDate = `${day}/${month}/${year}`;
   if (timePart) {
-    const [hours, minutes, seconds] = timePart.split(":");
-    const formattedTime = `${hours}:${minutes}:${seconds}`;
+    const [hours, minutes, seconds = "00"] = timePart
+      .replace("Z", "")
+      .split(":");
+    const formattedTime = `${hours}:${minutes}:${seconds.split(".")[0]}`;
     return `${formattedDate} ${formattedTime}`;
   }
   return formattedDate;