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 ADDED
@@ -0,0 +1,9 @@
1
+ # time-ago-dk
2
+
3
+ A tiny JavaScript utility to convert dates into human readable "time ago" format.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install time-ago-dk
9
+ ```
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
+ }
@@ -0,0 +1,5 @@
1
+ import { timeAgo } from "../src/index.js";
2
+
3
+ console.log(timeAgo(new Date(Date.now() - 60000))); // 1 minute ago
4
+ console.log(timeAgo(new Date(Date.now() - 3600000))); // 1 hour ago
5
+ console.log(timeAgo(new Date(Date.now() - 1000))); // just now