@reliverse/datetime 2.2.8 → 2.3.1

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.
Files changed (2) hide show
  1. package/dist/mod.js +32 -44
  2. package/package.json +8 -8
package/dist/mod.js CHANGED
@@ -84,10 +84,7 @@ export function formatCustom(date, format) {
84
84
  const milliseconds = dateObj.getMilliseconds();
85
85
  const ampm = hours >= 12 ? "PM" : "AM";
86
86
  const hours12 = hours % 12 || 12;
87
- return format.replace(FORMAT_PATTERNS.YYYY, String(year)).replace(FORMAT_PATTERNS.YY, String(year).slice(-2)).replace(FORMAT_PATTERNS.MM, String(month).padStart(2, "0")).replace(FORMAT_PATTERNS.M, String(month)).replace(FORMAT_PATTERNS.DD, String(day).padStart(2, "0")).replace(FORMAT_PATTERNS.D, String(day)).replace(FORMAT_PATTERNS.HH, String(hours).padStart(2, "0")).replace(FORMAT_PATTERNS.H, String(hours)).replace(FORMAT_PATTERNS.hh, String(hours12).padStart(2, "0")).replace(FORMAT_PATTERNS.h, String(hours12)).replace(FORMAT_PATTERNS.mm, String(minutes).padStart(2, "0")).replace(FORMAT_PATTERNS.m, String(minutes)).replace(FORMAT_PATTERNS.ss, String(seconds).padStart(2, "0")).replace(FORMAT_PATTERNS.s, String(seconds)).replace(FORMAT_PATTERNS.SSS, String(milliseconds).padStart(3, "0")).replace(
88
- FORMAT_PATTERNS.SS,
89
- String(Math.floor(milliseconds / 10)).padStart(2, "0")
90
- ).replace(FORMAT_PATTERNS.S, String(Math.floor(milliseconds / 100))).replace(FORMAT_PATTERNS.AMPM, ampm).replace(FORMAT_PATTERNS.ampm, ampm.toLowerCase());
87
+ return format.replace(FORMAT_PATTERNS.YYYY, String(year)).replace(FORMAT_PATTERNS.YY, String(year).slice(-2)).replace(FORMAT_PATTERNS.MM, String(month).padStart(2, "0")).replace(FORMAT_PATTERNS.M, String(month)).replace(FORMAT_PATTERNS.DD, String(day).padStart(2, "0")).replace(FORMAT_PATTERNS.D, String(day)).replace(FORMAT_PATTERNS.HH, String(hours).padStart(2, "0")).replace(FORMAT_PATTERNS.H, String(hours)).replace(FORMAT_PATTERNS.hh, String(hours12).padStart(2, "0")).replace(FORMAT_PATTERNS.h, String(hours12)).replace(FORMAT_PATTERNS.mm, String(minutes).padStart(2, "0")).replace(FORMAT_PATTERNS.m, String(minutes)).replace(FORMAT_PATTERNS.ss, String(seconds).padStart(2, "0")).replace(FORMAT_PATTERNS.s, String(seconds)).replace(FORMAT_PATTERNS.SSS, String(milliseconds).padStart(3, "0")).replace(FORMAT_PATTERNS.SS, String(Math.floor(milliseconds / 10)).padStart(2, "0")).replace(FORMAT_PATTERNS.S, String(Math.floor(milliseconds / 100))).replace(FORMAT_PATTERNS.AMPM, ampm).replace(FORMAT_PATTERNS.ampm, ampm.toLowerCase());
91
88
  }
92
89
  export function parseDate(input) {
93
90
  if (input instanceof Date) {
@@ -131,34 +128,17 @@ export function toTimezone(date, timeZone) {
131
128
  const dateObj = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
132
129
  const formatter = getTimezoneFormatter(timeZone);
133
130
  const parts = formatter.formatToParts(dateObj);
134
- const year = Number.parseInt(
135
- parts.find((p) => p.type === "year")?.value ?? "0",
136
- 10
137
- );
131
+ const year = Number.parseInt(parts.find((p) => p.type === "year")?.value ?? "0", 10);
138
132
  const month = Number.parseInt(parts.find((p) => p.type === "month")?.value ?? "0", 10) - 1;
139
- const day = Number.parseInt(
140
- parts.find((p) => p.type === "day")?.value ?? "0",
141
- 10
142
- );
143
- const hour = Number.parseInt(
144
- parts.find((p) => p.type === "hour")?.value ?? "0",
145
- 10
146
- );
147
- const minute = Number.parseInt(
148
- parts.find((p) => p.type === "minute")?.value ?? "0",
149
- 10
150
- );
151
- const second = Number.parseInt(
152
- parts.find((p) => p.type === "second")?.value ?? "0",
153
- 10
154
- );
133
+ const day = Number.parseInt(parts.find((p) => p.type === "day")?.value ?? "0", 10);
134
+ const hour = Number.parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
135
+ const minute = Number.parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
136
+ const second = Number.parseInt(parts.find((p) => p.type === "second")?.value ?? "0", 10);
155
137
  return new Date(Date.UTC(year, month, day, hour, minute, second));
156
138
  }
157
139
  export function getTimezoneOffset(date, timeZone) {
158
140
  const dateObj = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
159
- const utcDate = new Date(
160
- dateObj.toLocaleString("en-US", { timeZone: "UTC" })
161
- );
141
+ const utcDate = new Date(dateObj.toLocaleString("en-US", { timeZone: "UTC" }));
162
142
  const tzDate = new Date(dateObj.toLocaleString("en-US", { timeZone }));
163
143
  return (tzDate.getTime() - utcDate.getTime()) / (1e3 * 60);
164
144
  }
@@ -248,9 +228,7 @@ export function formatDuration(duration) {
248
228
  parts.push(`${duration.years} ${duration.years === 1 ? "year" : "years"}`);
249
229
  }
250
230
  if (duration.months) {
251
- parts.push(
252
- `${duration.months} ${duration.months === 1 ? "month" : "months"}`
253
- );
231
+ parts.push(`${duration.months} ${duration.months === 1 ? "month" : "months"}`);
254
232
  }
255
233
  if (duration.weeks) {
256
234
  parts.push(`${duration.weeks} ${duration.weeks === 1 ? "week" : "weeks"}`);
@@ -262,14 +240,10 @@ export function formatDuration(duration) {
262
240
  parts.push(`${duration.hours} ${duration.hours === 1 ? "hour" : "hours"}`);
263
241
  }
264
242
  if (duration.minutes) {
265
- parts.push(
266
- `${duration.minutes} ${duration.minutes === 1 ? "minute" : "minutes"}`
267
- );
243
+ parts.push(`${duration.minutes} ${duration.minutes === 1 ? "minute" : "minutes"}`);
268
244
  }
269
245
  if (duration.seconds) {
270
- parts.push(
271
- `${duration.seconds} ${duration.seconds === 1 ? "second" : "seconds"}`
272
- );
246
+ parts.push(`${duration.seconds} ${duration.seconds === 1 ? "second" : "seconds"}`);
273
247
  }
274
248
  if (duration.milliseconds) {
275
249
  parts.push(
@@ -289,7 +263,7 @@ export function formatDuration(duration) {
289
263
  if (parts.length === 2) {
290
264
  return `${parts[0]} and ${parts[1]}`;
291
265
  }
292
- return `${parts.slice(0, -1).join(", ")}, and ${parts[parts.length - 1]}`;
266
+ return `${parts.slice(0, -1).join(", ")}, and ${parts.at(-1)}`;
293
267
  }
294
268
  export function addDuration(date, duration) {
295
269
  const dateObj = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
@@ -322,13 +296,27 @@ export function addDuration(date, duration) {
322
296
  }
323
297
  export function subtractDuration(date, duration) {
324
298
  const negatedDuration = {};
325
- if (duration.years) negatedDuration.years = -duration.years;
326
- if (duration.months) negatedDuration.months = -duration.months;
327
- if (duration.weeks) negatedDuration.weeks = -duration.weeks;
328
- if (duration.days) negatedDuration.days = -duration.days;
329
- if (duration.hours) negatedDuration.hours = -duration.hours;
330
- if (duration.minutes) negatedDuration.minutes = -duration.minutes;
331
- if (duration.seconds) negatedDuration.seconds = -duration.seconds;
299
+ if (duration.years) {
300
+ negatedDuration.years = -duration.years;
301
+ }
302
+ if (duration.months) {
303
+ negatedDuration.months = -duration.months;
304
+ }
305
+ if (duration.weeks) {
306
+ negatedDuration.weeks = -duration.weeks;
307
+ }
308
+ if (duration.days) {
309
+ negatedDuration.days = -duration.days;
310
+ }
311
+ if (duration.hours) {
312
+ negatedDuration.hours = -duration.hours;
313
+ }
314
+ if (duration.minutes) {
315
+ negatedDuration.minutes = -duration.minutes;
316
+ }
317
+ if (duration.seconds) {
318
+ negatedDuration.seconds = -duration.seconds;
319
+ }
332
320
  if (duration.milliseconds) {
333
321
  negatedDuration.milliseconds = -duration.milliseconds;
334
322
  }
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@reliverse/datetime",
3
+ "version": "2.3.1",
4
+ "private": false,
3
5
  "description": "DateTime manipulation and formatting utilities for Reliverse ecosystem",
6
+ "license": "MIT",
4
7
  "author": "reliverse",
5
- "version": "2.2.8",
6
- "private": false,
8
+ "files": [
9
+ "dist",
10
+ "package.json"
11
+ ],
7
12
  "type": "module",
8
13
  "main": "./src/mod.ts",
9
14
  "types": "./src/mod.ts",
@@ -15,10 +20,5 @@
15
20
  },
16
21
  "publishConfig": {
17
22
  "access": "public"
18
- },
19
- "files": [
20
- "dist",
21
- "package.json"
22
- ],
23
- "license": "MIT"
23
+ }
24
24
  }