zavadil-ts-common 1.1.6 → 1.1.7
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/dist/util/DateUtil.d.ts +8 -0
- package/package.json +1 -1
- package/src/util/DateUtil.ts +70 -0
- package/tsconfig.json +2 -3
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare class DateUtil {
|
2
|
+
static formatNumber(n: number, digits?: number): string;
|
3
|
+
static parseDate(d: Date | string | null | undefined): Date | undefined;
|
4
|
+
static formatDateForHumans(d: Date | string | null | undefined): string;
|
5
|
+
static getDurationMs(d1?: Date | string | null, d2?: Date | string | null): number | null;
|
6
|
+
static getSinceDurationMs(d1?: Date | string | null): number | null;
|
7
|
+
static formatDuration(ms?: number | null): string;
|
8
|
+
}
|
package/package.json
CHANGED
@@ -0,0 +1,70 @@
|
|
1
|
+
import {ObjectUtil} from "./ObjectUtil";
|
2
|
+
|
3
|
+
export class DateUtil {
|
4
|
+
|
5
|
+
static formatNumber(n: number, digits = 2) {
|
6
|
+
const s = String(n);
|
7
|
+
return s.padStart(digits, '0');
|
8
|
+
}
|
9
|
+
|
10
|
+
static parseDate(d: Date | string | null | undefined): Date | undefined {
|
11
|
+
if (!d) return undefined;
|
12
|
+
if (typeof d === 'string') {
|
13
|
+
return new Date(d);
|
14
|
+
}
|
15
|
+
return d;
|
16
|
+
}
|
17
|
+
|
18
|
+
static formatDateForHumans(d: Date | string | null | undefined): string {
|
19
|
+
d = DateUtil.parseDate(d);
|
20
|
+
if (!d) return "";
|
21
|
+
|
22
|
+
const year = d.getFullYear();
|
23
|
+
const month = DateUtil.formatNumber(d.getMonth() + 1);
|
24
|
+
const day = DateUtil.formatNumber(d.getDate());
|
25
|
+
const hours = DateUtil.formatNumber(d.getHours());
|
26
|
+
const minutes = DateUtil.formatNumber(d.getMinutes());
|
27
|
+
const seconds = DateUtil.formatNumber(d.getSeconds());
|
28
|
+
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
29
|
+
}
|
30
|
+
|
31
|
+
static getDurationMs(d1?: Date | string | null, d2?: Date | string | null): number | null {
|
32
|
+
d1 = DateUtil.parseDate(d1);
|
33
|
+
d2 = DateUtil.parseDate(d2);
|
34
|
+
if (ObjectUtil.isEmpty(d1) || ObjectUtil.isEmpty(d2)) return null;
|
35
|
+
try {
|
36
|
+
// @ts-ignore
|
37
|
+
return d2.getTime() - d1.getTime();
|
38
|
+
} catch (e) {
|
39
|
+
return null;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
static getSinceDurationMs(d1?: Date | string | null): number | null {
|
44
|
+
return DateUtil.getDurationMs(d1, new Date());
|
45
|
+
}
|
46
|
+
|
47
|
+
static formatDuration(ms?: number | null): string {
|
48
|
+
if (!ms) {
|
49
|
+
return '';
|
50
|
+
}
|
51
|
+
|
52
|
+
let secs = Math.floor(ms / 1000);
|
53
|
+
ms -= secs * 1000;
|
54
|
+
let mins = Math.floor(secs / 60);
|
55
|
+
secs -= mins * 60;
|
56
|
+
let hrs = Math.floor(mins / 60);
|
57
|
+
mins -= hrs * 60;
|
58
|
+
let days = Math.floor(hrs / 24);
|
59
|
+
hrs -= days * 24;
|
60
|
+
|
61
|
+
const items = [];
|
62
|
+
if (days > 0) items.push(`${days}d`);
|
63
|
+
if (hrs > 0) items.push(`${hrs}h`);
|
64
|
+
if (mins > 0) items.push(`${mins}m`);
|
65
|
+
if (secs > 0 && days === 0 && hrs === 0) items.push(`${secs}s`);
|
66
|
+
if (ms > 0 && days === 0 && hrs === 0 && mins === 0) items.push(`${ms}ms`);
|
67
|
+
|
68
|
+
return items.join(' ');
|
69
|
+
}
|
70
|
+
}
|