/** * Enhanced Market Countdown Timer * * This script displays a countdown timer showing time until market open when closed, * and time until market close when open. It also updates the visual indicator. */ document.addEventListener("DOMContentLoaded", function() { const countdownElement = document.getElementById("market-countdown"); const statusElement = document.getElementById("market-status"); const timerElement = document.getElementById("countdown-timer"); const marketIconElement = document.getElementById("market-icon"); if (!countdownElement || !statusElement || !timerElement || !marketIconElement) return; // Market hours (9:30 AM to 4:00 PM Eastern Time) const MARKET_OPEN_HOUR = 9; const MARKET_OPEN_MINUTE = 30; const MARKET_CLOSE_HOUR = 16; const MARKET_CLOSE_MINUTE = 0; function updateCountdown() { const now = new Date(); // Convert to Eastern Time for market hours const easternTime = new Date(now.toLocaleString("en-US", {timeZone: "America/New_York"})); const day = easternTime.getDay(); const currentHours = easternTime.getHours(); const currentMinutes = easternTime.getMinutes(); const currentSeconds = easternTime.getSeconds(); // Check if it's a weekend (0 = Sunday, 6 = Saturday) const isWeekend = day === 0 || day === 6; // Calculate current time in minutes since midnight const currentTimeInMinutes = currentHours * 60 + currentMinutes; const marketOpenInMinutes = MARKET_OPEN_HOUR * 60 + MARKET_OPEN_MINUTE; const marketCloseInMinutes = MARKET_CLOSE_HOUR * 60 + MARKET_CLOSE_MINUTE; // Check if market is open const isMarketOpen = !isWeekend && currentTimeInMinutes >= marketOpenInMinutes && currentTimeInMinutes < marketCloseInMinutes; // Update the countdown class countdownElement.className = isMarketOpen ? "market-countdown market-open" : "market-countdown market-closed"; // Add pulse animation to the status indicator when market is open if (isMarketOpen) { marketIconElement.classList.add("fa-pulse"); } else { marketIconElement.classList.remove("fa-pulse"); } // Calculate time remaining let timeRemaining; let statusText; if (isWeekend) { // If it's weekend, show time until Monday market open const daysUntilMonday = day === 0 ? 1 : 2; const nextMarketOpen = new Date(easternTime); nextMarketOpen.setDate(nextMarketOpen.getDate() + daysUntilMonday); nextMarketOpen.setHours(MARKET_OPEN_HOUR, MARKET_OPEN_MINUTE, 0, 0); timeRemaining = Math.floor((nextMarketOpen - easternTime) / 1000); statusText = "CLOSED - Opens in"; } else if (isMarketOpen) { // If market is open, show time until close const secondsUntilClose = (marketCloseInMinutes - currentTimeInMinutes) * 60 - currentSeconds; timeRemaining = secondsUntilClose; statusText = "OPEN - Closes in"; } else if (currentTimeInMinutes < marketOpenInMinutes) { // If it's before market open today const secondsUntilOpen = (marketOpenInMinutes - currentTimeInMinutes) * 60 - currentSeconds; timeRemaining = secondsUntilOpen; statusText = "CLOSED - Opens in"; } else { // If it's after market close today const nextMarketOpen = new Date(easternTime); nextMarketOpen.setDate(nextMarketOpen.getDate() + 1); nextMarketOpen.setHours(MARKET_OPEN_HOUR, MARKET_OPEN_MINUTE, 0, 0); // If tomorrow is weekend, adjust to Monday const tomorrowDay = nextMarketOpen.getDay(); if (tomorrowDay === 0) { // Sunday nextMarketOpen.setDate(nextMarketOpen.getDate() + 1); } else if (tomorrowDay === 6) { // Saturday nextMarketOpen.setDate(nextMarketOpen.getDate() + 2); } timeRemaining = Math.floor((nextMarketOpen - easternTime) / 1000); statusText = "CLOSED - Opens in"; } // Format the time remaining const displayHours = Math.floor(timeRemaining / 3600); const displayMinutes = Math.floor((timeRemaining % 3600) / 60); const displaySeconds = timeRemaining % 60; const formattedTime = `${displayHours.toString().padStart(2, "0")}:${displayMinutes.toString().padStart(2, "0")}:${displaySeconds.toString().padStart(2, "0")}`; // Update the DOM statusElement.textContent = statusText; timerElement.textContent = formattedTime; } // Update immediately and then every second updateCountdown(); setInterval(updateCountdown, 1000); });