function parseMoodleDate(dateString) { if (!dateString) return new Date(NaN); dateString = dateString.trim(); const now = new Date(); let year = now.getFullYear(); let month = now.getMonth(); let date = now.getDate(); let hours = 0; let minutes = 0; // Expected format matches: "Today, 11:59 PM", "Tomorrow, 11:59 PM", "Sunday, 3 May, 11:59 PM", "3 May 2026, 11:59 PM" // Extract time (e.g., "11:59 PM" or "23:59") const timeMatch = dateString.match(/(\d{1,2}):(\d{2})\s*(AM|PM)?/i); if (timeMatch) { hours = parseInt(timeMatch[1], 10); minutes = parseInt(timeMatch[2], 10); const ampm = timeMatch[3] ? timeMatch[3].toUpperCase() : null; if (ampm === 'PM' && hours < 12) hours += 12; if (ampm === 'AM' && hours === 12) hours = 0; } // Handle "Today" / "Tomorrow" if (dateString.toLowerCase().includes('today')) { // use today's date } else if (dateString.toLowerCase().includes('tomorrow')) { date += 1; } else { // Try to parse "3 May" or "Sunday, 3 May" // Month names const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; const dateMatch = dateString.match(/(\d{1,2})\s+([a-zA-Z]+)(?:\s+(\d{4}))?/); if (dateMatch) { date = parseInt(dateMatch[1], 10); const monthStr = dateMatch[2].toLowerCase(); const monthIndex = months.findIndex(m => m === monthStr); if (monthIndex !== -1) { month = monthIndex % 12; // map short months to same index } if (dateMatch[3]) { year = parseInt(dateMatch[3], 10); } } } const parsedDate = new Date(year, month, date, hours, minutes); return parsedDate; } module.exports = { parseMoodleDate };