@bash-app/bash-common 29.44.0 → 29.44.2
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/package.json +1 -1
- package/src/utils/dateTimeUtils.ts +60 -26
package/package.json
CHANGED
|
@@ -19,7 +19,7 @@ export const TIME_FORMAT_AM_PM = "h:mm A";
|
|
|
19
19
|
export interface ITime {
|
|
20
20
|
hours: number;
|
|
21
21
|
minutes: number;
|
|
22
|
-
ampm:
|
|
22
|
+
ampm: 'AM' | 'PM';
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export function formatDateRange(startDateTimeArg: Date | null, endDateTimeArg: Date | null): string {
|
|
@@ -40,25 +40,25 @@ export function ensureIsDateTime(possiblyADate: DateTimeArgType): Date | undefin
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export function normalizeDate(validDate: Date | string | undefined | null): Date | undefined {
|
|
43
|
-
if (validDate)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
if (!validDate) return undefined;
|
|
44
|
+
|
|
45
|
+
const date = new Date(validDate);
|
|
46
|
+
|
|
47
|
+
if (isNaN(date.getTime())) {
|
|
48
|
+
console.error("Invalid date provided:", validDate);
|
|
49
|
+
return undefined;
|
|
49
50
|
}
|
|
50
|
-
}
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
// Normalize to UTC and start of minute
|
|
53
|
+
return dayjs(date).utc().startOf("minute").toDate();
|
|
54
|
+
}
|
|
55
|
+
export function normalizeDates(dates: (Date | string | undefined | null)[]): Date[] {
|
|
53
56
|
return dates
|
|
54
|
-
.map((date)
|
|
55
|
-
|
|
56
|
-
})
|
|
57
|
-
.filter((dateTime: Date | undefined): boolean => {
|
|
58
|
-
return !!dateTime
|
|
59
|
-
}) as Date[];
|
|
57
|
+
.map((date) => normalizeDate(date)) // Normalize each date
|
|
58
|
+
.filter((date): date is Date => !!date); // Filter out invalid dates
|
|
60
59
|
}
|
|
61
60
|
|
|
61
|
+
|
|
62
62
|
export function formatDateTimeToISODateTimeString(date: DateTimeArgType): string | undefined {
|
|
63
63
|
if (!date) {
|
|
64
64
|
return undefined;
|
|
@@ -136,19 +136,53 @@ export function compareDateTime(date1: DateTimeArgType,
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
export function setDateButPreserveTime(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
139
|
+
export function setDateButPreserveTime(
|
|
140
|
+
dateArg: DateType,
|
|
141
|
+
dateWithTheTimeYouWantToKeep: DateType | undefined
|
|
142
|
+
): Date {
|
|
143
|
+
if (!dateArg || !dateWithTheTimeYouWantToKeep) {
|
|
144
|
+
console.error("Invalid arguments:", { dateArg, dateWithTheTimeYouWantToKeep });
|
|
145
|
+
throw new Error("Both dateArg and dateWithTheTimeYouWantToKeep are required.");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Get local timezone
|
|
149
|
+
const localTimeZone = dayjs.tz.guess();
|
|
150
|
+
|
|
151
|
+
// Format date and time parts
|
|
152
|
+
const datePart = dayjs(dateArg).tz(localTimeZone).format("YYYY-MM-DD"); // Local date
|
|
153
|
+
const timePart = dayjs(dateWithTheTimeYouWantToKeep).tz(localTimeZone).format("HH:mm"); // Local time
|
|
154
|
+
|
|
155
|
+
// Combine into an ISO string
|
|
156
|
+
const combinedDateTimeString = `${datePart}T${timePart}`;
|
|
157
|
+
console.log("Combining date and time:", {
|
|
158
|
+
datePart,
|
|
159
|
+
timePart,
|
|
160
|
+
combinedDateTimeString,
|
|
161
|
+
localTimeZone,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Convert to UTC
|
|
165
|
+
const combinedDateTime = dayjs.tz(combinedDateTimeString, localTimeZone).toDate();
|
|
166
|
+
if (isNaN(combinedDateTime.getTime())) {
|
|
167
|
+
console.error("Invalid combined datetime:", combinedDateTimeString);
|
|
168
|
+
throw new Error("Invalid date or time format.");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
console.log("Final combined DateTime (UTC):", combinedDateTime);
|
|
143
172
|
return combinedDateTime;
|
|
144
173
|
}
|
|
145
174
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
175
|
+
|
|
176
|
+
export function setTimeOnDate(date: DateType | Date | undefined, parsedTime: ITime | null): Date {
|
|
177
|
+
const isValidDate = dayjs(date).isValid();
|
|
178
|
+
const dateTime = new Date(isValidDate && date ? date : Date.now());
|
|
179
|
+
if (parsedTime) {
|
|
180
|
+
const parsedTimeDate = new Date();
|
|
181
|
+
parsedTimeDate.setHours(parsedTime.hours, parsedTime.minutes, 0); // Will change the date depending on time; corrected later
|
|
182
|
+
const correctTimeOnDate = setDateButPreserveTime(dateTime, parsedTimeDate);
|
|
183
|
+
return correctTimeOnDate;
|
|
184
|
+
}
|
|
185
|
+
return dateTime;
|
|
152
186
|
}
|
|
153
187
|
|
|
154
188
|
export function ensureDateTimeIsLocalDateTime(dateArg: DateType | Dayjs | undefined): Dayjs {
|
|
@@ -197,4 +231,4 @@ export function localDateToUTC(date: Date): Date {
|
|
|
197
231
|
date.getUTCMinutes(), date.getUTCSeconds());
|
|
198
232
|
|
|
199
233
|
return newDate;
|
|
200
|
-
}
|
|
234
|
+
}
|