tiny-relative-time 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/LICENSE +7 -0
- package/README.md +46 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/package.json +17 -0
- package/src/index.ts +65 -0
- package/test.js +22 -0
- package/tsconfig.json +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Prajakta_Dhatrak
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
tiny-relative-time is a lightweightand versatile package to format dates and timestamps into human-friendly relative times. It works for both JavaScript and TypeScript and supports multiple formatting modes, including natural language and short compact format
|
|
2
|
+
|
|
3
|
+
✨ Features
|
|
4
|
+
Format relative time in natural language:
|
|
5
|
+
Compact formats
|
|
6
|
+
Multiple modes for flexibility
|
|
7
|
+
|
|
8
|
+
📦 Installation
|
|
9
|
+
Install the package using npm:
|
|
10
|
+
npm install tiny-relative-time
|
|
11
|
+
|
|
12
|
+
🚀 Usage
|
|
13
|
+
relTime() works with Date objects, timestamps, or ISO strings. The second argument is one of "natural" | "minuteRounded" | "compact" | "compactMinute" modes. In TypeScript, you can optionally import the Mode type for type safety.”
|
|
14
|
+
|
|
15
|
+
⚡JavaScript
|
|
16
|
+
const { relTime } = require('tiny-relative-time');
|
|
17
|
+
console.log(relTime(new Date(Date.now() - 10 * 1000), "natural"));
|
|
18
|
+
console.log(relTime(Date.now() - 10 * 1000, "minuteRounded"));
|
|
19
|
+
console.log(relTime(new Date(Date.now() - 10 * 1000).toISOString(), "compact"));
|
|
20
|
+
console.log(relTime(Date.now() - 10 * 1000, "compactMinute"));
|
|
21
|
+
|
|
22
|
+
🛡️TypeScript
|
|
23
|
+
import { relTime, Mode } from 'tiny-relative-time';
|
|
24
|
+
const dateInput: Date = new Date(Date.now() - 10 * 1000);
|
|
25
|
+
const timestampInput: number = Date.now() - 10 * 1000;
|
|
26
|
+
const isoInput: string = new Date(Date.now() - 10 * 1000).toISOString();
|
|
27
|
+
const mode: Mode = "natural";
|
|
28
|
+
console.log(relTime(dateInput, mode));
|
|
29
|
+
console.log(relTime(timestampInput, "minuteRounded"));
|
|
30
|
+
console.log(relTime(isoInput, "compact"));
|
|
31
|
+
console.log(relTime(timestampInput, "compactMinute"));
|
|
32
|
+
|
|
33
|
+
🗒️Modes
|
|
34
|
+
1. natural: Standard natural language; 10 seconds ago
|
|
35
|
+
2. minuteRounded: Rounds times to nearest minute; very recent times show "just now"
|
|
36
|
+
3. compact: Short, concise format; 10s
|
|
37
|
+
4. compactMinute: Short format with focus on minutes; less than 1 minute shows 0m
|
|
38
|
+
|
|
39
|
+
✔️Supported Input
|
|
40
|
+
1. Date object
|
|
41
|
+
2. Timestamp (number in milliseconds)
|
|
42
|
+
3. ISO string ("2023-01-01T12:00:00Z")
|
|
43
|
+
|
|
44
|
+
🤝 Contributing Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
|
|
45
|
+
|
|
46
|
+
📄 License This project is licensed under the MIT License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG,eAAe,GAAG,SAAS,GAAG,eAAe,CAAC;AAiB7E,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,IAAI,GAAE,IAAgB,GAAG,MAAM,CA+CrF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relTime = relTime;
|
|
4
|
+
const UNITS = [
|
|
5
|
+
{ label: "year", short: "yr", seconds: 31536000 },
|
|
6
|
+
{ label: "month", short: "mo", seconds: 2592000 },
|
|
7
|
+
{ label: "day", short: "d", seconds: 86400 },
|
|
8
|
+
{ label: "hour", short: "h", seconds: 3600 },
|
|
9
|
+
{ label: "minute", short: "m", seconds: 60 },
|
|
10
|
+
{ label: "second", short: "s", seconds: 1 },
|
|
11
|
+
];
|
|
12
|
+
function relTime(input, mode = "natural") {
|
|
13
|
+
const now = Date.now();
|
|
14
|
+
const date = new Date(input).getTime();
|
|
15
|
+
if (isNaN(date))
|
|
16
|
+
throw new Error("Invalid Date");
|
|
17
|
+
let diffSec = Math.floor((now - date) / 1000);
|
|
18
|
+
const isFuture = diffSec < 0;
|
|
19
|
+
diffSec = Math.abs(diffSec);
|
|
20
|
+
switch (mode) {
|
|
21
|
+
case "minuteRounded":
|
|
22
|
+
if (diffSec < 30)
|
|
23
|
+
return "just now";
|
|
24
|
+
diffSec = Math.round(diffSec / 60) * 60;
|
|
25
|
+
break;
|
|
26
|
+
case "compactMinute":
|
|
27
|
+
if (diffSec < 60)
|
|
28
|
+
return "0m";
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
for (let unit of UNITS) {
|
|
32
|
+
const value = Math.floor(diffSec / unit.seconds);
|
|
33
|
+
if (value >= 1) {
|
|
34
|
+
switch (mode) {
|
|
35
|
+
case "natural":
|
|
36
|
+
case "minuteRounded":
|
|
37
|
+
return `${value} ${unit.label}${value > 1 ? "s" : ""} ago`;
|
|
38
|
+
case "compact":
|
|
39
|
+
return `${value}${unit.short}`;
|
|
40
|
+
case "compactMinute":
|
|
41
|
+
if (unit.label === "minute")
|
|
42
|
+
return `${value}m`;
|
|
43
|
+
return `${value}${unit.short}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
switch (mode) {
|
|
48
|
+
case "natural":
|
|
49
|
+
case "minuteRounded":
|
|
50
|
+
return "just now";
|
|
51
|
+
case "compact":
|
|
52
|
+
return "0s";
|
|
53
|
+
case "compactMinute":
|
|
54
|
+
return "0m";
|
|
55
|
+
default:
|
|
56
|
+
return "just now";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAiBA,0BA+CC;AAxDD,MAAM,KAAK,GAAe;IACxB,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE;IACjD,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;IACjD,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE;IAC5C,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE;IAC5C,EAAE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IAAI,IAAI,CAAC,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC;gBAC7D,KAAK,SAAS;oBACZ,OAAO,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;gBACjC,KAAK,eAAe;oBAClB,IAAI,IAAI,CAAC,KAAK,KAAK,QAAQ;wBAAE,OAAO,GAAG,KAAK,GAAG,CAAC;oBAChD,OAAO,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC;IAED,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,SAAS,CAAC;QACf,KAAK,eAAe;YAClB,OAAO,UAAU,CAAC;QACpB,KAAK,SAAS;YACZ,OAAO,IAAI,CAAC;QACd,KAAK,eAAe;YAClB,OAAO,IAAI,CAAC;QACd;YACE,OAAO,UAAU,CAAC;IACtB,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tiny-relative-time",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight relative time package as a formatter for JavaScript and TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "npm run build && node test.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["time ago","time formatter","ago","relative","relative time","date formatter","time difference","javascript time"],
|
|
12
|
+
"author": "Prajakta Dhatrak",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.9.3"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type Mode = "natural" | "minuteRounded" | "compact" | "compactMinute";
|
|
2
|
+
|
|
3
|
+
interface TimeUnit {
|
|
4
|
+
label: string;
|
|
5
|
+
short: string;
|
|
6
|
+
seconds: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const UNITS: TimeUnit[] = [
|
|
10
|
+
{ label: "year", short: "yr", seconds: 31536000 },
|
|
11
|
+
{ label: "month", short: "mo", seconds: 2592000 },
|
|
12
|
+
{ label: "day", short: "d", seconds: 86400 },
|
|
13
|
+
{ label: "hour", short: "h", seconds: 3600 },
|
|
14
|
+
{ label: "minute", short: "m", seconds: 60 },
|
|
15
|
+
{ label: "second", short: "s", seconds: 1 },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
export function relTime(input: Date | number | string, mode: Mode = "natural"): string {
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
const date = new Date(input).getTime();
|
|
21
|
+
if (isNaN(date)) throw new Error("Invalid Date");
|
|
22
|
+
|
|
23
|
+
let diffSec = Math.floor((now - date) / 1000);
|
|
24
|
+
|
|
25
|
+
const isFuture = diffSec < 0;
|
|
26
|
+
diffSec = Math.abs(diffSec);
|
|
27
|
+
|
|
28
|
+
switch (mode) {
|
|
29
|
+
case "minuteRounded":
|
|
30
|
+
if (diffSec < 30) return "just now";
|
|
31
|
+
diffSec = Math.round(diffSec / 60) * 60;
|
|
32
|
+
break;
|
|
33
|
+
case "compactMinute":
|
|
34
|
+
if (diffSec < 60) return "0m";
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (let unit of UNITS) {
|
|
39
|
+
const value = Math.floor(diffSec / unit.seconds);
|
|
40
|
+
if (value >= 1) {
|
|
41
|
+
switch (mode) {
|
|
42
|
+
case "natural":
|
|
43
|
+
case "minuteRounded":
|
|
44
|
+
return `${value} ${unit.label}${value > 1 ? "s" : ""} ago`;
|
|
45
|
+
case "compact":
|
|
46
|
+
return `${value}${unit.short}`;
|
|
47
|
+
case "compactMinute":
|
|
48
|
+
if (unit.label === "minute") return `${value}m`;
|
|
49
|
+
return `${value}${unit.short}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
switch (mode) {
|
|
55
|
+
case "natural":
|
|
56
|
+
case "minuteRounded":
|
|
57
|
+
return "just now";
|
|
58
|
+
case "compact":
|
|
59
|
+
return "0s";
|
|
60
|
+
case "compactMinute":
|
|
61
|
+
return "0m";
|
|
62
|
+
default:
|
|
63
|
+
return "just now";
|
|
64
|
+
}
|
|
65
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const { relTime } = require("./dist/index");
|
|
2
|
+
|
|
3
|
+
const twoHoursAgo = new Date(Date.now() - 2 * 3600 * 1000);
|
|
4
|
+
const oneMinuteAgo = Date.now() - 10 * 1000;
|
|
5
|
+
const fiveMinutesAgoIso = new Date(Date.now() - 5 * 60 * 1000).toISOString();
|
|
6
|
+
console.log(twoHoursAgo, oneMinuteAgo, fiveMinutesAgoIso);
|
|
7
|
+
|
|
8
|
+
console.log("natural (Date):", relTime(twoHoursAgo, "natural"));
|
|
9
|
+
console.log("natural (timestamp):", relTime(oneMinuteAgo, "natural"));
|
|
10
|
+
console.log("natural (ISO):", relTime(fiveMinutesAgoIso, "natural"));
|
|
11
|
+
|
|
12
|
+
console.log("minuteRounded (Date):", relTime(twoHoursAgo, "minuteRounded"));
|
|
13
|
+
console.log("minuteRounded (timestamp):", relTime(oneMinuteAgo, "minuteRounded"));
|
|
14
|
+
console.log("minuteRounded (ISO):", relTime(fiveMinutesAgoIso, "minuteRounded"));
|
|
15
|
+
|
|
16
|
+
console.log("compact (Date):", relTime(twoHoursAgo, "compact"));
|
|
17
|
+
console.log("compact (timestamp):", relTime(oneMinuteAgo, "compact"));
|
|
18
|
+
console.log("compact (ISO):", relTime(fiveMinutesAgoIso, "compact"));
|
|
19
|
+
|
|
20
|
+
console.log("compactMinute (Date):", relTime(twoHoursAgo, "compactMinute"));
|
|
21
|
+
console.log("compactMinute (timestamp):", relTime(oneMinuteAgo, "compactMinute"));
|
|
22
|
+
console.log("compactMinute (ISO):", relTime(fiveMinutesAgoIso, "compactMinute"));
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/index.ts"],
|
|
15
|
+
"exclude": ["node_modules"]
|
|
16
|
+
}
|