@nemigo/helpers 0.4.1 → 0.4.3
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/datetime.d.ts +3 -2
- package/dist/datetime.js +27 -13
- package/package.json +1 -1
package/dist/datetime.d.ts
CHANGED
|
@@ -28,12 +28,13 @@ export declare const abbs: string[];
|
|
|
28
28
|
* const formatted = dateTimeFormatter(new Date(), 'DD.DM.DY TH:TM:TS');
|
|
29
29
|
* console.log(formatted); // "09.10.2023 14:30:59"
|
|
30
30
|
*/
|
|
31
|
-
export declare const formatDateTime: (datetime?: Date | Timestamp | string, format?: string) => string;
|
|
31
|
+
export declare const formatDateTime: (datetime?: Date | Timestamp | string, format?: string, timezone?: number) => string;
|
|
32
32
|
/**
|
|
33
33
|
* Конвертирует строковые представления даты и времени в {@link Timestamp}
|
|
34
34
|
*
|
|
35
35
|
* @param date - Строка даты в формате "DD.MM.YYYY"
|
|
36
36
|
* @param [time="00:00"] - Строка времени в формате "HH:MM"
|
|
37
|
+
* @param [timezone] - Часовой пояс относительно GMT
|
|
37
38
|
*
|
|
38
39
|
* @returns TimeStamp соответствующий указанной дате и времени или 0 при неверном формате
|
|
39
40
|
*
|
|
@@ -41,4 +42,4 @@ export declare const formatDateTime: (datetime?: Date | Timestamp | string, form
|
|
|
41
42
|
* const timestamp = toTimeStamp("11.02.2012", "13:45");
|
|
42
43
|
* console.log(timestamp); // Выведет {@link Timestamp} для "11.02.2012 13:45"
|
|
43
44
|
*/
|
|
44
|
-
export declare const toTimeStamp: (date: string, time?: string) => Timestamp | 0;
|
|
45
|
+
export declare const toTimeStamp: (date: string, time?: string, timezone?: number) => Timestamp | 0;
|
package/dist/datetime.js
CHANGED
|
@@ -54,18 +54,26 @@ export const abbs = [
|
|
|
54
54
|
* const formatted = dateTimeFormatter(new Date(), 'DD.DM.DY TH:TM:TS');
|
|
55
55
|
* console.log(formatted); // "09.10.2023 14:30:59"
|
|
56
56
|
*/
|
|
57
|
-
export const formatDateTime = (datetime = Date.now(), format = "DD.DM.DY TH:TM") => {
|
|
58
|
-
const
|
|
57
|
+
export const formatDateTime = (datetime = Date.now(), format = "DD.DM.DY TH:TM", timezone) => {
|
|
58
|
+
const base = new Date(datetime);
|
|
59
|
+
const useTz = timezone !== undefined;
|
|
60
|
+
const shiftMinutes = useTz ? Math.round(timezone * 60) : 0;
|
|
61
|
+
const shifted = useTz ? new Date(base.valueOf() + shiftMinutes * 60000) : base;
|
|
62
|
+
const seconds = useTz ? shifted.getUTCSeconds() : shifted.getSeconds();
|
|
63
|
+
const minutes = useTz ? shifted.getUTCMinutes() : shifted.getMinutes();
|
|
64
|
+
const hours = useTz ? shifted.getUTCHours() : shifted.getHours();
|
|
65
|
+
const day = useTz ? shifted.getUTCDate() : shifted.getDate();
|
|
66
|
+
const monthIndex = useTz ? shifted.getUTCMonth() : shifted.getMonth();
|
|
67
|
+
const year = useTz ? shifted.getUTCFullYear() : shifted.getFullYear();
|
|
59
68
|
const replacements = {
|
|
60
|
-
TS: pad(
|
|
61
|
-
TM: pad(
|
|
62
|
-
TH: pad(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
DY: date.getFullYear().toString(),
|
|
69
|
+
TS: pad(seconds),
|
|
70
|
+
TM: pad(minutes),
|
|
71
|
+
TH: pad(hours),
|
|
72
|
+
DD: pad(day),
|
|
73
|
+
DM: pad(monthIndex + 1),
|
|
74
|
+
DFM: months[monthIndex],
|
|
75
|
+
DAM: abbs[monthIndex],
|
|
76
|
+
DY: year.toString(),
|
|
69
77
|
};
|
|
70
78
|
const result = [];
|
|
71
79
|
let i = 0;
|
|
@@ -92,6 +100,7 @@ export const formatDateTime = (datetime = Date.now(), format = "DD.DM.DY TH:TM")
|
|
|
92
100
|
*
|
|
93
101
|
* @param date - Строка даты в формате "DD.MM.YYYY"
|
|
94
102
|
* @param [time="00:00"] - Строка времени в формате "HH:MM"
|
|
103
|
+
* @param [timezone] - Часовой пояс относительно GMT
|
|
95
104
|
*
|
|
96
105
|
* @returns TimeStamp соответствующий указанной дате и времени или 0 при неверном формате
|
|
97
106
|
*
|
|
@@ -99,12 +108,17 @@ export const formatDateTime = (datetime = Date.now(), format = "DD.DM.DY TH:TM")
|
|
|
99
108
|
* const timestamp = toTimeStamp("11.02.2012", "13:45");
|
|
100
109
|
* console.log(timestamp); // Выведет {@link Timestamp} для "11.02.2012 13:45"
|
|
101
110
|
*/
|
|
102
|
-
export const toTimeStamp = (date, time = "00:00") => {
|
|
111
|
+
export const toTimeStamp = (date, time = "00:00", timezone) => {
|
|
103
112
|
const [day, month, year] = date.split(".").map(Number);
|
|
104
113
|
const [hours, minutes] = time.split(":").map(Number);
|
|
105
114
|
// Проверка на корректность ввода (если хотя бы одно значение не число)
|
|
106
115
|
// prettier-ignore
|
|
107
116
|
if ([day, month, year, hours, minutes].some(isNaN))
|
|
108
117
|
return 0;
|
|
109
|
-
|
|
118
|
+
if (timezone !== undefined) {
|
|
119
|
+
return Date.UTC(year, month - 1, day, hours, minutes) - timezone * 60 * 60 * 1000;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
return new Date(year, month - 1, day, hours, minutes).getTime();
|
|
123
|
+
}
|
|
110
124
|
};
|