@nemigo/helpers 0.4.2 → 0.4.4

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.
@@ -28,7 +28,17 @@ 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
+ /**
33
+ * Готовый пресет для {@link formatDateTime}
34
+ *
35
+ * `DD.DM.DY TH:TM (МСК)` с МСК-флагом
36
+ *
37
+ * @example
38
+ * const mskTime = formatMSK(Date.now());
39
+ * console.log(mskTime); // "09.10.2023 14:30 (МСК)"
40
+ */
41
+ export declare const formatMSK: (timestamp?: Timestamp, format?: string) => string;
32
42
  /**
33
43
  * Конвертирует строковые представления даты и времени в {@link Timestamp}
34
44
  *
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 date = new Date(datetime);
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(date.getSeconds()),
61
- TM: pad(date.getMinutes()),
62
- TH: pad(date.getHours()),
63
- //...
64
- DD: pad(date.getDate()),
65
- DM: pad(date.getMonth() + 1),
66
- DFM: months[date.getMonth()],
67
- DAM: abbs[date.getMonth()],
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;
@@ -86,6 +94,16 @@ export const formatDateTime = (datetime = Date.now(), format = "DD.DM.DY TH:TM")
86
94
  }
87
95
  return result.join("");
88
96
  };
97
+ /**
98
+ * Готовый пресет для {@link formatDateTime}
99
+ *
100
+ * `DD.DM.DY TH:TM (МСК)` с МСК-флагом
101
+ *
102
+ * @example
103
+ * const mskTime = formatMSK(Date.now());
104
+ * console.log(mskTime); // "09.10.2023 14:30 (МСК)"
105
+ */
106
+ export const formatMSK = (timestamp = Date.now(), format = "DD.DM.DY TH:TM (МСК)") => formatDateTime(timestamp, format, 3);
89
107
  //...
90
108
  /**
91
109
  * Конвертирует строковые представления даты и времени в {@link Timestamp}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/helpers",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "Vlad Logvin",