@hardlydifficult/date-time 1.0.7 → 1.0.9
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 +21 -67
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/date-time
|
|
2
2
|
|
|
3
|
-
A TypeScript
|
|
3
|
+
A TypeScript utility for converting time durations to milliseconds with strong typing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,90 +14,44 @@ npm install @hardlydifficult/date-time
|
|
|
14
14
|
import { toMilliseconds, type TimeSpan } from "@hardlydifficult/date-time";
|
|
15
15
|
|
|
16
16
|
const duration: TimeSpan = { value: 2.5, unit: "minutes" };
|
|
17
|
-
|
|
17
|
+
const ms = toMilliseconds(duration); // 150_000
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## TimeSpan Conversion
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Converts a time duration to its equivalent in milliseconds using predefined unit multipliers.
|
|
23
23
|
|
|
24
|
-
### TimeSpan Interface
|
|
24
|
+
### `TimeSpan` Interface
|
|
25
25
|
|
|
26
26
|
Represents a time duration with a numeric `value` and a `unit`.
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
| Field | Type | Description |
|
|
29
|
+
|-------|----------|----------------------|
|
|
30
|
+
| value | `number` | The numeric duration |
|
|
31
|
+
| unit | `TimeUnit` | The time unit |
|
|
32
|
+
|
|
33
|
+
### `TimeUnit` Type
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
Supported time units:
|
|
36
36
|
- `"milliseconds"`
|
|
37
37
|
- `"seconds"`
|
|
38
38
|
- `"minutes"`
|
|
39
39
|
- `"hours"`
|
|
40
40
|
- `"days"`
|
|
41
41
|
|
|
42
|
-
### toMilliseconds()
|
|
42
|
+
### `toMilliseconds(timeSpan)`
|
|
43
43
|
|
|
44
|
-
Converts a `TimeSpan` to
|
|
44
|
+
Converts a `TimeSpan` to milliseconds.
|
|
45
45
|
|
|
46
46
|
```typescript
|
|
47
47
|
import { toMilliseconds } from "@hardlydifficult/date-time";
|
|
48
48
|
|
|
49
|
-
//
|
|
50
|
-
toMilliseconds({ value:
|
|
51
|
-
|
|
52
|
-
// Days to milliseconds
|
|
53
|
-
toMilliseconds({ value: 1, unit: "days" }); // 86_400_000
|
|
54
|
-
|
|
55
|
-
// Fractional hours
|
|
56
|
-
toMilliseconds({ value: 0.75, unit: "hours" }); // 2_700_000
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
| Unit | Multiplier (ms) |
|
|
60
|
-
|------------|-----------------|
|
|
61
|
-
| milliseconds | 1 |
|
|
62
|
-
| seconds | 1,000 |
|
|
63
|
-
| minutes | 60,000 |
|
|
64
|
-
| hours | 3,600,000 |
|
|
65
|
-
| days | 86,400,000 |
|
|
66
|
-
|
|
67
|
-
### Example: Duration to Milliseconds Table
|
|
68
|
-
|
|
69
|
-
| Duration | Milliseconds |
|
|
70
|
-
|-------------------------|--------------|
|
|
71
|
-
| 1 second | 1,000 |
|
|
72
|
-
| 1.5 minutes | 90,000 |
|
|
73
|
-
| 2 hours | 7,200,000 |
|
|
74
|
-
| 0.5 days | 43,200,000 |
|
|
75
|
-
|
|
76
|
-
## TimeSpan Usage Details
|
|
49
|
+
// Convert 2 hours
|
|
50
|
+
toMilliseconds({ value: 2, unit: "hours" }); // 7_200_000
|
|
77
51
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
### Type Definition
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
interface TimeSpan {
|
|
84
|
-
value: number;
|
|
85
|
-
unit: TimeUnit;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
type TimeUnit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Additional Examples
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
import { toMilliseconds } from '@hardlydifficult/date-time';
|
|
95
|
-
|
|
96
|
-
toMilliseconds({ value: 2, unit: 'seconds' }); // 2000
|
|
97
|
-
toMilliseconds({ value: 1, unit: 'hours' }); // 3600000
|
|
98
|
-
toMilliseconds({ value: 0.5, unit: 'seconds' }); // 500
|
|
99
|
-
toMilliseconds({ value: 0, unit: 'minutes' }); // 0
|
|
100
|
-
toMilliseconds({ value: 1, unit: 'days' }); // 86400000
|
|
101
|
-
```
|
|
52
|
+
// Convert 0.5 days
|
|
53
|
+
toMilliseconds({ value: 0.5, unit: "days" }); // 43_200_000
|
|
102
54
|
|
|
103
|
-
|
|
55
|
+
// Handle fractional values
|
|
56
|
+
toMilliseconds({ value: 1.5, unit: "minutes" }); // 90_000
|
|
57
|
+
```
|