@capillarytech/cap-ui-utils 3.0.7-alpha.7 → 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/timezone.js +32 -26
package/package.json
CHANGED
package/utils/timezone.js
CHANGED
|
@@ -18,38 +18,44 @@ export const DATE_FORMAT = "DD MMM YYYY h:mm A";
|
|
|
18
18
|
* @returns {string} Formatted date string with strict TZ abbreviation or '-'.
|
|
19
19
|
*/
|
|
20
20
|
export const formatDateWithTimezone = (date, orgTZ = false) => {
|
|
21
|
-
|
|
21
|
+
try{
|
|
22
|
+
if (!date) return "-";
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
let momentDate;
|
|
25
|
+
const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
// Epoch (number or numeric string)
|
|
28
|
+
if (typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))) {
|
|
29
|
+
const timestamp = typeof date === "string" ? parseInt(date, 10) : date;
|
|
30
|
+
const timestampMs = timestamp < 1e12 ? timestamp * 1000 : timestamp;
|
|
31
|
+
momentDate = moment.tz(timestampMs, orgTimezone);
|
|
32
|
+
}
|
|
33
|
+
// ISO string or Date object
|
|
34
|
+
else {
|
|
35
|
+
momentDate = moment(date);
|
|
36
|
+
if(momentDate.isUTC() || orgTZ){
|
|
37
|
+
momentDate = momentDate.tz(orgTimezone);
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
|
-
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
if (!momentDate.isValid()) return "-";
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
// Get strict timezone abbreviation (no offsets like GMT+05:30)
|
|
44
|
+
let tzAbbr;
|
|
45
|
+
if (momentDate.isValid()) {
|
|
46
|
+
const tz = momentDate.tz() || orgTimezone;
|
|
47
|
+
tzAbbr = moment.tz.zone(tz)?.abbr(momentDate.valueOf()) || "UTC";
|
|
48
|
+
} else {
|
|
49
|
+
tzAbbr = "UTC"; // fallback
|
|
50
|
+
}
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
}
|
|
52
|
+
return `${momentDate.format(DATE_FORMAT)} ${tzAbbr}`;
|
|
53
|
+
}
|
|
54
|
+
catch(error){
|
|
55
|
+
console.error(error);
|
|
56
|
+
return "-";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
53
59
|
|
|
54
60
|
/**
|
|
55
61
|
* Returns a tooltip string with the timezone and offset for a given date.
|