-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
73 lines (65 loc) · 2.05 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Transforms integer minutes into formated hours and minutes string.
*/
export const formatDuration = (minutesTotal: number) => {
const hours = (minutesTotal - (minutesTotal % 60)) / 60;
const minutes = minutesTotal % 60;
const hoursString = `${hours} hour${hours > 1 ? 's' : ''}`;
const minutesString = `${minutes} minute${minutes > 1 ? 's' : ''}`;
if (hours > 0 && minutes > 0) {
return `${hoursString} and ${minutesString}`;
}
if (hours > 0) {
return hoursString;
}
if (minutes > 0) {
return minutesString;
}
return 'not meassured';
};
/**
* Transforms unix timestamp to human readable string.
* @param timestamp unix time
* @param format of returned string, one of 'datetime' | 'date' | 'time'.
* */
export const formatTimestamp = (timestamp: number, format: 'datetime' | 'date' | 'time' = 'datetime') => {
const dt = new Date(timestamp * 1000); // s => ms
switch (format) {
case 'date':
return dt.toLocaleString('en-US', {
day: 'numeric',
year: 'numeric',
month: 'short',
});
case 'time':
return dt.toLocaleString('en-US', {
hour: '2-digit',
minute: 'numeric',
hour12: false,
});
case 'datetime':
default:
return dt.toLocaleString('en-US', {
day: 'numeric',
year: 'numeric',
month: 'long',
hour: '2-digit',
minute: 'numeric',
hour12: false,
});
}
};
/**
* Transforms unix seconds timestamp to human readable string.
* */
export const formatTimestampShort = (ts: number) => {
const dt = new Date(ts * 1000); // s => ms
const minutes = dt.getMinutes();
return `${dt.toLocaleDateString()} ${dt.getHours()}:${minutes < 9 ? '0' : ''}${minutes}`;
};
/**
* Converts JavaSript Date object to unix time integer.
*/
export const datetimeToUnix = (dt: Date) => {
return dt.getTime() / 1000;
};