|
| 1 | +(function () { |
| 2 | + function getThirdFriday(year, month) { |
| 3 | + const first = new Date(year, month, 1); |
| 4 | + const daysUntilFriday = (5 - first.getDay() + 7) % 7; |
| 5 | + return 1 + daysUntilFriday + 14; |
| 6 | + } |
| 7 | + |
| 8 | + const nyDateFormat = new Intl.DateTimeFormat("en-US", { |
| 9 | + timeZone: "America/New_York", |
| 10 | + year: "numeric", |
| 11 | + month: "2-digit", |
| 12 | + day: "2-digit", |
| 13 | + }); |
| 14 | + |
| 15 | + const nyOffsetFormat = new Intl.DateTimeFormat("en-US", { |
| 16 | + timeZone: "America/New_York", |
| 17 | + timeZoneName: "shortOffset", |
| 18 | + }); |
| 19 | + |
| 20 | + function nyOffsetMinutes(date) { |
| 21 | + const offset = nyOffsetFormat |
| 22 | + .formatToParts(date) |
| 23 | + .find((p) => p.type === "timeZoneName").value; |
| 24 | + if (offset === "GMT") return 0; |
| 25 | + const match = offset.match(/^GMT([+-])(\d{1,2})(?::?(\d{2}))?$/); |
| 26 | + if (!match) throw new Error("Unexpected offset: " + offset); |
| 27 | + const sign = match[1] === "+" ? 1 : -1; |
| 28 | + return sign * (Number(match[2]) * 60 + Number(match[3] || 0)); |
| 29 | + } |
| 30 | + |
| 31 | + function newYorkTimeToDate(year, month, day, hour, minute) { |
| 32 | + const localAsUTC = Date.UTC(year, month, day, hour, minute, 0); |
| 33 | + return new Date(localAsUTC - nyOffsetMinutes(new Date(localAsUTC)) * 60_000); |
| 34 | + } |
| 35 | + |
| 36 | + function nextThirdFridayET(hour, minute) { |
| 37 | + const now = new Date(); |
| 38 | + const parts = Object.fromEntries( |
| 39 | + nyDateFormat |
| 40 | + .formatToParts(now) |
| 41 | + .filter((p) => p.type !== "literal") |
| 42 | + .map((p) => [p.type, p.value]) |
| 43 | + ); |
| 44 | + let year = Number(parts.year); |
| 45 | + let month = Number(parts.month) - 1; |
| 46 | + |
| 47 | + function buildMeeting(y, m) { |
| 48 | + return newYorkTimeToDate(y, m, getThirdFriday(y, m), hour, minute); |
| 49 | + } |
| 50 | + |
| 51 | + let meeting = buildMeeting(year, month); |
| 52 | + if (meeting <= now) { |
| 53 | + month += 1; |
| 54 | + if (month === 12) { |
| 55 | + month = 0; |
| 56 | + year += 1; |
| 57 | + } |
| 58 | + meeting = buildMeeting(year, month); |
| 59 | + } |
| 60 | + return meeting; |
| 61 | + } |
| 62 | + |
| 63 | + const els = document.querySelectorAll(".meeting-next"); |
| 64 | + if (els.length > 0) { |
| 65 | + const text = nextThirdFridayET(12, 0).toLocaleString([], { |
| 66 | + weekday: "long", |
| 67 | + year: "numeric", |
| 68 | + month: "long", |
| 69 | + day: "numeric", |
| 70 | + hour: "numeric", |
| 71 | + minute: "2-digit", |
| 72 | + timeZoneName: "short", |
| 73 | + }); |
| 74 | + els.forEach((el) => { el.textContent = text; }); |
| 75 | + } |
| 76 | +})(); |
0 commit comments