File size: 2,143 Bytes
c4be319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 };