@hardlydifficult/date-time 1.0.3 → 1.0.5
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 +58 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/date-time
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript library providing human-readable duration types and millisecond conversion utilities.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -11,12 +11,63 @@ npm install @hardlydifficult/date-time
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { toMilliseconds, type TimeSpan } from
|
|
14
|
+
import { toMilliseconds, type TimeSpan } from "@hardlydifficult/date-time";
|
|
15
15
|
|
|
16
|
-
const
|
|
17
|
-
console.log(toMilliseconds(
|
|
16
|
+
const duration: TimeSpan = { value: 2.5, unit: "minutes" };
|
|
17
|
+
console.log(toMilliseconds(duration)); // 150000
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
## Duration Conversion
|
|
21
|
+
|
|
22
|
+
The package provides a strongly-typed `TimeSpan` interface and a utility function `toMilliseconds` for converting time durations to milliseconds.
|
|
23
|
+
|
|
24
|
+
### TimeSpan Interface
|
|
25
|
+
|
|
26
|
+
Represents a time duration with a numeric value and a unit.
|
|
27
|
+
|
|
28
|
+
| Property | Type | Description |
|
|
29
|
+
|----------|----------|-------------------------|
|
|
30
|
+
| value | `number` | The numeric value |
|
|
31
|
+
| unit | `TimeUnit` | The time unit of the value |
|
|
32
|
+
|
|
33
|
+
### TimeUnit Type
|
|
34
|
+
|
|
35
|
+
Supported time units:
|
|
36
|
+
|
|
37
|
+
- `"milliseconds"`
|
|
38
|
+
- `"seconds"`
|
|
39
|
+
- `"minutes"`
|
|
40
|
+
- `"hours"`
|
|
41
|
+
- `"days"`
|
|
42
|
+
|
|
43
|
+
### toMilliseconds Function
|
|
44
|
+
|
|
45
|
+
Converts a `TimeSpan` to its equivalent value in milliseconds.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { toMilliseconds, type TimeSpan } from "@hardlydifficult/date-time";
|
|
49
|
+
|
|
50
|
+
// Convert various durations
|
|
51
|
+
const durations: TimeSpan[] = [
|
|
52
|
+
{ value: 1, unit: "seconds" },
|
|
53
|
+
{ value: 0.5, unit: "hours" },
|
|
54
|
+
{ value: 2, unit: "days" }
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
durations.forEach(duration => {
|
|
58
|
+
console.log(toMilliseconds(duration)); // 1000, 1800000, 172800000
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Example: Duration to Milliseconds Table
|
|
63
|
+
|
|
64
|
+
| Duration | Milliseconds |
|
|
65
|
+
|-------------------------|--------------|
|
|
66
|
+
| 1 second | 1,000 |
|
|
67
|
+
| 1.5 minutes | 90,000 |
|
|
68
|
+
| 2 hours | 7,200,000 |
|
|
69
|
+
| 0.5 days | 43,200,000 |
|
|
70
|
+
|
|
20
71
|
## TimeSpan
|
|
21
72
|
|
|
22
73
|
A strongly-typed duration representation that pairs a numeric value with a time unit, enabling human-readable time specifications that convert to milliseconds.
|
|
@@ -41,8 +92,9 @@ import { toMilliseconds } from '@hardlydifficult/date-time';
|
|
|
41
92
|
|
|
42
93
|
toMilliseconds({ value: 2, unit: 'seconds' }); // 2000
|
|
43
94
|
toMilliseconds({ value: 1, unit: 'hours' }); // 3600000
|
|
44
|
-
toMilliseconds({ value: 0.5, unit: 'seconds' }); // 500
|
|
95
|
+
toMilliseconds({ value: 0.5, unit: 'seconds' }); // 500
|
|
45
96
|
toMilliseconds({ value: 0, unit: 'minutes' }); // 0
|
|
97
|
+
toMilliseconds({ value: 1, unit: 'days' }); // 86400000
|
|
46
98
|
```
|
|
47
99
|
|
|
48
|
-
Supports all time units and handles fractional values correctly.
|
|
100
|
+
Supports all time units (`milliseconds`, `seconds`, `minutes`, `hours`, `days`) and handles fractional values and zero correctly.
|