Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | 1x 6x 6x 3x 3x 1x 2x 2x 1x 1x 1x 4x 4x 2x 2x 11x 11x 9x 9x 8x 8x 8x 8x 2x 6x 6x 4x 4x 4x 1x 3x 6x 6x 3x 3x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 5x 5x 2x 2x 1x 1x 1x 1x 1x 8x 8x 8x 1x 4x 4x 4x 4x 4x 5x 5x 5x 5x 3x 3x 3x 3x 4x 2x 4x 4x 4x 4x 3x 11x 11x 3x 8x 8x 2x 6x 2x 7x 7x 7x 7x 6x 6x 6x 4x 6x 108x 9x 154x 154x 154x 154x 5x 6x 6x 33x 33x 31x 30x 21x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 3x 2x 2x 2x 2x 3x 3x 1x 1x | import { DAILY_TIME_SLOTS } from "@repo/constants/time";
import { TimeRange, TimeSlot } from "@repo/types";
/**
* Format a date string into a human-readable format.
*
* - If `hideTime` is true, only the date will be returned in the format "Dec 26, 2025".
* - If `hideTime` is false or not provided, both time and date are returned in the format "10:30 AM, Dec 26, 2025".
*
* @param input - The ISO date string to format (e.g., "2025-11-07T10:30:00.000Z").
* @param hideTime - Optional boolean flag to hide the time. Default is false.
* @returns A formatted string representing the date, optionally including time.
*/
export const formatDateTime = (
input: string | number | Date,
hideTime = true,
): string => {
if (!input) return "";
const date = new Date(input);
if (isNaN(date.getTime())) {
return "";
}
const day = date.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
});
if (hideTime) {
return day;
}
const time = date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "2-digit",
hour12: true,
});
return `${time}, ${day}`;
};
/**
* Check if a given date is today.
*
* @param date - The date to check against today.
* @returns True if the given date is today, false otherwise.
*/
export const isToday = (date: Date | null | undefined): boolean => {
if (!date || isNaN(date.getTime())) return false;
const now = new Date();
return (
date.getDate() === now.getDate() &&
date.getMonth() === now.getMonth() &&
date.getFullYear() === now.getFullYear()
);
};
/**
* Merge a date string and a time string into a single ISO date string.
*
* - If `dateInput` is not provided or is an invalid date, null is returned.
* - If `timeStr` is not provided, the ISO date string is returned without the time.
* - If `timeStr` is provided, the hours and minutes are extracted and set on the date.
* - The resulting date is returned as an ISO date string.
*
* @param dateInput - The date string to merge with the time (e.g., "2025-11-07").
* @param timeStr - The time string to merge with the date (e.g., "10:30").
* @returns An ISO date string representing the merged date and time, or null if the date is invalid.
*/
export const mergeDateAndTimeToISO = (
dateInput: string | Date | null | undefined,
timeStr: string | null | undefined,
): string | null => {
if (!dateInput) return null;
const date =
dateInput instanceof Date ? new Date(dateInput) : new Date(dateInput);
if (Number.isNaN(date.getTime())) return null;
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
if (!timeStr) {
return new Date(year, month, day, 0, 0, 0).toISOString();
}
const parts = timeStr.split(":");
if (parts.length < 2) return null;
const hours = Number(parts[0]);
const minutes = Number(parts[1]);
if (
!Number.isFinite(hours) ||
!Number.isFinite(minutes) ||
hours < 0 ||
hours > 23 ||
minutes < 0 ||
minutes > 59
) {
return null;
}
return new Date(year, month, day, hours, minutes, 0).toISOString();
};
/**
* Format date with weekday and optionally hide year if it's the current year.
*
* Examples:
* - Fri, Nov 19
* - Fri, Nov 19, 2024
*/
export const formatDateWithWeekday = (
input: string | number | Date,
): string => {
if (!input) return "";
const date = new Date(input);
if (isNaN(date.getTime())) {
return "";
}
const now = new Date();
const isCurrentYear = date.getFullYear() === now.getFullYear();
const baseFormat: Intl.DateTimeFormatOptions = {
weekday: "short",
month: "short",
day: "numeric",
};
const withYearFormat: Intl.DateTimeFormatOptions = {
...baseFormat,
year: "numeric",
};
return date.toLocaleDateString(
"en-US",
isCurrentYear ? baseFormat : withYearFormat,
);
};
/**
* Format days off value for display.
*
* Examples:
* - 1 Day off
* - 0.5 Day off
*/
export const formatDaysOff = (daysOff: number, startTime?: Date): string => {
// Special case: half day
if (daysOff === 0.5 && startTime) {
const hour = startTime.getHours();
const minute = startTime.getMinutes();
const totalMinutes = hour * 60 + minute;
const MORNING_START = 8 * 60; // 08:00
const AFTERNOON_START = 13 * 60 + 30; // 13:30
if (totalMinutes === MORNING_START) {
return "morning off";
}
if (totalMinutes === AFTERNOON_START) {
return "afternoon off";
}
}
const roundedValue =
daysOff % 1 === 0
? daysOff.toString()
: Number(daysOff.toFixed(1)).toString();
const dayLabel = daysOff === 1 ? "day" : "days";
return `${roundedValue} ${dayLabel} off`;
};
/**
* Compare two dates by calendar day only (year/month/day).
*/
export const isSameDay = (left: Date | null, right: Date | null): boolean => {
if (!left || !right) {
return false;
}
return (
left.getFullYear() === right.getFullYear() &&
left.getMonth() === right.getMonth() &&
left.getDate() === right.getDate()
);
};
/**
* Normalize a date to start of day and return its timestamp.
*/
export const getStartOfDayTime = (value: Date): number => {
const date = new Date(value);
date.setHours(0, 0, 0, 0);
return date.getTime();
};
/**
* Build a date at 00:00:00 with a day offset from today.
*/
export const getPresetDate = (
offsetDays: number,
fromDate: Date = new Date(),
): Date => {
const date = new Date(fromDate);
date.setHours(0, 0, 0, 0);
date.setDate(date.getDate() + offsetDays);
return date;
};
/**
* Format time only in 12-hour format with AM/PM.
*
* Examples:
* - 8:00 AM
* - 9:45 AM
* - 6:30 PM
*/
export const formatTime = (input: string | number | Date): string => {
if (!input) return "";
const date = new Date(input);
if (isNaN(date.getTime())) {
return "";
}
return date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "2-digit",
hour12: true,
});
};
const WORK_HOURS = [
// Morning
{ start: 8, startMin: 0, end: 12, endMin: 0 },
// Afternoon
{ start: 13, startMin: 30, end: 17, endMin: 30 },
];
const HOURS_PER_DAY = 8;
const parseDate = (date: Date, h: number, m: number): Date => {
const d = new Date(date);
d.setHours(h, m, 0, 0);
return d;
};
const overlapHours = (
aStart: Date,
aEnd: Date,
bStart: Date,
bEnd: Date,
): number => {
const start = Math.max(aStart.getTime(), bStart.getTime());
const end = Math.min(aEnd.getTime(), bEnd.getTime());
return Math.max(0, (end - start) / (1000 * 60 * 60));
};
function isWeekend(date: Date): boolean {
const day = date.getDay();
return day === 0 || day === 6;
}
export const calculateLeaveDays = (
startISO: string,
endISO: string,
): number => {
const start = new Date(startISO);
const end = new Date(endISO);
if (end <= start) return 0;
let totalHours = 0;
const current = new Date(start);
current.setHours(0, 0, 0, 0);
while (current <= end) {
// Skip Saturday & Sunday
if (!isWeekend(current)) {
for (const shift of WORK_HOURS) {
const shiftStart = parseDate(current, shift.start, shift.startMin);
const shiftEnd = parseDate(current, shift.end, shift.endMin);
totalHours += overlapHours(start, end, shiftStart, shiftEnd);
}
}
current.setDate(current.getDate() + 1);
}
return Number((totalHours / HOURS_PER_DAY).toFixed(2));
};
/**
* Check if a given date range is expired.
*
* @param start - The start date of the range.
* @param end - The end date of the range. Defaults to the current time.
* @returns True if the range is expired, false otherwise.
*/
export const isExpired = (
startTime: string | Date,
endTime: Date = new Date(),
): boolean => {
if (!startTime) {
return false;
}
const start = startTime instanceof Date ? startTime : new Date(startTime);
if (isNaN(start.getTime()) || isNaN(endTime.getTime())) {
return false;
}
return endTime.getTime() > start.getTime();
};
/**
* Format a given date to the YYYY-MM-DD format.
*
* @param date - The date to format.
* @returns A string representing the formatted date in YYYY-MM-DD format.
*/
export const formatDateToYMD = (date: Date): string => {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}-${m}-${d}`;
};
/**
* Filter the time slots based on the selected date and the start time.
* If the start time is provided, filter out the time slots that are earlier than the start time.
* If the start time is not provided, return all the time slots.
* @param selectedDate - The selected date.
* @param startDate - The start date of the range. Defaults to the selected date.
* @param startTime - The start time of the range. Defaults to the current time.
* @returns An array of time slots that are filtered based on the selected date and the start time.
*/
export const filterTimeSlots = (
selectedDate: Date,
startDate?: Date,
startTime?: string,
): TimeSlot[] => {
let minMinutes = 0;
if (
startDate &&
startTime &&
selectedDate.toDateString() === startDate.toDateString()
) {
minMinutes = timeToMinutes(startTime);
}
return DAILY_TIME_SLOTS.filter((slot) => {
return timeToMinutes(slot.value) > minMinutes;
});
};
/**
* Converts a given time string in the format HH:MM to minutes.
*
* @param time - The time string to convert.
* @returns The number of minutes represented by the time string.
*/
export const timeToMinutes = (time: string) => {
const [h, m] = (time || "").split(":");
const hours = Number(h);
const minutes = Number(m);
return (
(Number.isFinite(hours) ? hours : 0) * 60 +
(Number.isFinite(minutes) ? minutes : 0)
);
};
interface FilterTimeSlotOptions {
min?: number;
max?: number;
excludeRanges?: TimeRange[];
}
export const getBookedTimeSlots = (
slots: TimeSlot[],
options: FilterTimeSlotOptions = {},
): TimeSlot[] => {
const { min, max, excludeRanges = [] } = options;
return slots.filter((slot) => {
const minutes = timeToMinutes(slot.value);
if (min !== undefined && minutes <= min) return true;
if (max !== undefined && minutes > max) return true;
return excludeRanges.some(
(range) => minutes >= range.start && minutes < range.end,
);
});
};
export const isDisabledDate = ({
date,
holidays,
makeUpWorkdays,
startDate,
}: {
date: Date;
holidays: string[];
makeUpWorkdays: string[];
startDate?: Date;
}) => {
const d = new Date(date);
const day = d.getDay(); // 0 = CN, 6 = T7
const dateStr = formatDateToYMD(d);
const holidaySet = new Set(holidays);
const makeUpSet = new Set(makeUpWorkdays);
if (holidaySet.has(dateStr)) return true;
const isWeekendDay = day === 0 || day === 6;
const isMakeUpDay = makeUpSet.has(dateStr);
if (isWeekendDay && !isMakeUpDay) return true;
if (startDate) {
const start = new Date(startDate);
start.setHours(0, 0, 0, 0);
if (d < start) return true;
}
return false;
};
export const getBlockedRoomSlots = (
date: Date | null | undefined,
bookedRanges: TimeRange[],
minMinutes?: number,
): TimeSlot[] => {
if (!date) return [];
return getBookedTimeSlots(DAILY_TIME_SLOTS, {
min: minMinutes,
excludeRanges: bookedRanges,
});
};
type DateFormatVariant = "full" | "short";
export const formatMoveDate = (
inputDate: Date | string | undefined,
variant: DateFormatVariant = "full",
) => {
if (!inputDate) return "";
const date = typeof inputDate === "string" ? new Date(inputDate) : inputDate;
if (isNaN(date.getTime())) return "";
const weekdaysFull = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const weekdaysShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const monthsFull = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const monthsShort = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const hours = date.getUTCHours();
const isMorning = hours < 4;
const session = isMorning ? "Morning" : "Afternoon";
if (variant === "short") {
// Trả về định dạng cho UI nhỏ: "Tue, Nov 25 • Morning"
const day = weekdaysShort[date.getDay()];
const month = monthsShort[date.getMonth()];
const dateNum = date.getDate();
return `${day}, ${month} ${dateNum} • ${session}`;
}
// Mặc định trả về định dạng cũ: "Tuesday morning, November 25"
return `${weekdaysFull[date.getDay()]} ${session.toLowerCase()}, ${monthsFull[date.getMonth()]} ${date.getDate()}`;
};
|