|
4 | 4 | * Created Date: 2026-01-23 22:00:18 |
5 | 5 | * Author: 3urobeat |
6 | 6 | * |
7 | | - * Last Modified: 2026-03-15 19:56:39 |
| 7 | + * Last Modified: 2026-03-21 23:25:50 |
8 | 8 | * Modified By: 3urobeat |
9 | 9 | * |
10 | 10 | * Copyright (c) 2026 3urobeat <https://github.com/3urobeat> |
@@ -175,3 +175,43 @@ export function formatTime(time: number) { |
175 | 175 |
|
176 | 176 | return `${Math.round(until)} ${untilUnit}`; |
177 | 177 | } |
| 178 | + |
| 179 | + |
| 180 | +// Year used for dates where year is being ignored (I swear this makes sense) |
| 181 | +export const YEARLESS_DATE_YEAR = 2024; // Use a lap year so Feb 29 is always an option |
| 182 | + |
| 183 | +/** |
| 184 | + * Inits a yearless and timeless date object from a timestamp |
| 185 | + * @param timestamp Optional: Timestamp to parse. If undefined, current time is used |
| 186 | + * @returns Returns constructed Date object |
| 187 | + */ |
| 188 | +export function initYearlessDate(timestamp?: number): Date { |
| 189 | + const date = timestamp != undefined ? new Date(timestamp) : new Date(); |
| 190 | + |
| 191 | + date.setUTCFullYear(YEARLESS_DATE_YEAR); |
| 192 | + date.setUTCHours(0, 0, 0, 0); // Intentionally not using UTC here to remove timezone |
| 193 | + |
| 194 | + return date; |
| 195 | +} |
| 196 | + |
| 197 | +/** |
| 198 | + * Is current timestamp between (<= & >=) from & to while ignoring year? |
| 199 | + * @param from From: If greater than to, the previous year will be used internally |
| 200 | + * @param to ... |
| 201 | + * @returns Boolean indicating whether now is between from & to |
| 202 | + */ |
| 203 | +export function isNowBetweenDatesIgnoringYear(from: number, to: number): boolean { |
| 204 | + |
| 205 | + // Parse dates and bring them all onto the same year to effectively ignore it |
| 206 | + const fromDate = initYearlessDate(from); |
| 207 | + const toDate = initYearlessDate(to); |
| 208 | + const nowDate = initYearlessDate(); |
| 209 | + |
| 210 | + // Handle special case where from > to |
| 211 | + fromDate.setUTCFullYear(YEARLESS_DATE_YEAR - (from > to ? 1 : 0)); |
| 212 | + |
| 213 | + // console.debug("[DEBUG] isNowBetweenDatesIgnoringYear: ", fromDate, toDate, nowDate); |
| 214 | + |
| 215 | + return (fromDate.getTime() <= nowDate.getTime()) && (toDate.getTime() >= nowDate.getTime()); |
| 216 | + |
| 217 | +} |
0 commit comments