time-ago-dk 1.0.0
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/README.md +9 -0
- package/package.json +17 -0
- package/src/index.js +25 -0
- package/test/index.test.js +5 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "time-ago-dk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A tiny JavaScript utility to convert dates into human readable 'time ago' format.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test/index.test.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"time",
|
|
12
|
+
"ago",
|
|
13
|
+
"utility"
|
|
14
|
+
],
|
|
15
|
+
"author": "Darshit Kakadiya",
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function timeAgo(inputDate) {
|
|
2
|
+
const now = new Date();
|
|
3
|
+
const date = new Date(inputDate);
|
|
4
|
+
|
|
5
|
+
const seconds = Math.floor((now - date) / 1000);
|
|
6
|
+
|
|
7
|
+
if (seconds < 5) return "just now";
|
|
8
|
+
|
|
9
|
+
const intervals = [
|
|
10
|
+
{ label: "year", seconds: 31536000 },
|
|
11
|
+
{ label: "month", seconds: 2592000 },
|
|
12
|
+
{ label: "day", seconds: 86400 },
|
|
13
|
+
{ label: "hour", seconds: 3600 },
|
|
14
|
+
{ label: "minute", seconds: 60 },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
for (const interval of intervals) {
|
|
18
|
+
const count = Math.floor(seconds / interval.seconds);
|
|
19
|
+
if (count >= 1) {
|
|
20
|
+
return `${count} ${interval.label}${count > 1 ? "s" : ""} ago`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return `${seconds} seconds ago`;
|
|
25
|
+
}
|