@amohamud23/notihub 1.0.1 → 1.0.2
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/src/util/DateUtil.ts +55 -0
package/package.json
CHANGED
package/src/util/DateUtil.ts
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const getDate = (dateStr: string) => {
|
|
2
|
+
const date = new Date(dateStr); // Convert seconds to milliseconds
|
|
3
|
+
const year = date.getFullYear();
|
|
4
|
+
const month = (date.getMonth() + 1).toString().padStart(2, "0"); // Months are zero-based
|
|
5
|
+
const day = date.getDate().toString().padStart(2, "0");
|
|
6
|
+
|
|
7
|
+
const formattedDate = `${month}-${day}-${year}`;
|
|
8
|
+
|
|
9
|
+
return formattedDate;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const getDateFormat = (dateStr: string) => {
|
|
13
|
+
const date = new Date(dateStr);
|
|
14
|
+
const now = new Date();
|
|
15
|
+
const seconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
|
16
|
+
|
|
17
|
+
let interval = seconds / 31536000;
|
|
18
|
+
|
|
19
|
+
if (interval > 1) {
|
|
20
|
+
return date.toLocaleDateString("en-US", {
|
|
21
|
+
year: "numeric",
|
|
22
|
+
month: "short",
|
|
23
|
+
day: "numeric",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
interval = seconds / 2592000;
|
|
27
|
+
if (interval > 1) {
|
|
28
|
+
return date.toLocaleDateString("en-US", {
|
|
29
|
+
year: "numeric",
|
|
30
|
+
month: "short",
|
|
31
|
+
day: "numeric",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
interval = seconds / 86400;
|
|
35
|
+
if (interval > 7) {
|
|
36
|
+
return date.toLocaleDateString("en-US", {
|
|
37
|
+
year: "numeric",
|
|
38
|
+
month: "short",
|
|
39
|
+
day: "numeric",
|
|
40
|
+
});
|
|
41
|
+
} else if (interval > 1) {
|
|
42
|
+
return Math.floor(interval) + "d ago";
|
|
43
|
+
}
|
|
44
|
+
interval = seconds / 3600;
|
|
45
|
+
if (interval > 1) {
|
|
46
|
+
return Math.floor(interval) + "h ago";
|
|
47
|
+
}
|
|
48
|
+
interval = seconds / 60;
|
|
49
|
+
if (interval > 1) {
|
|
50
|
+
return Math.floor(interval) + "m ago";
|
|
51
|
+
}
|
|
52
|
+
return Math.floor(seconds) + "s ago";
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default { getDate, getDateFormat };
|