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 | 4x 4x 2x 2x 7x 7x 3x 4x 11x 11x 3x 8x 8x 2x 6x 5x 5x 5x 5x 4x 4x 4x 4x 4x 19x 19x 19x 19x 374x 374x 374x 374x 4x 154x 154x 154x 154x 154x 16x 16x 3x 13x 6x 6x 3x 10x 2x 2x 1x 9x | /**
* 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()
);
};
/**
* 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()
);
};
/**
* 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();
};
/**
* 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;
};
/**
* Normalize a date to the start of the day (00:00:00).
*/
export const toStartOfDay = (date: Date): Date => {
const normalized = new Date(date);
normalized.setHours(0, 0, 0, 0);
return normalized;
};
/**
* Add a number of days to a date.
*/
export const addDays = (date: Date, offset: number): Date => {
const next = new Date(date);
next.setDate(next.getDate() + offset);
return next;
};
/**
* Convert a date to total minutes from the start of the day.
*/
export const toMinutes = (date: Date): number => date.getHours() * 60 + date.getMinutes();
/**
* 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);
};
/**
* Normalize a date value from various formats (Date, string, number, or object with toDate method).
*/
export const normalizeDateValue = (value: unknown): Date | null => {
if (value instanceof Date && !isNaN(value.getTime())) {
return value;
}
if (typeof value === "string" || typeof value === "number") {
const parsed = new Date(value);
if (!isNaN(parsed.getTime())) {
return parsed;
}
}
if (
value &&
typeof value === "object" &&
"toDate" in value &&
typeof (value as { toDate: () => unknown }).toDate === "function"
) {
const parsed = (value as { toDate: () => unknown }).toDate();
if (parsed instanceof Date && !isNaN(parsed.getTime())) {
return parsed;
}
}
return null;
};
|