All files date-picker.ts

100% Statements 51/51
80% Branches 8/10
100% Functions 7/7
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89  1x   1x 1x   1x 201x     1x     1x 3x 3x   3x 90x 90x     3x     1x 2x     1x 1x 1x 1x 1x   1x 7x 3x   4x 4x       1x   1x 4x 4x 28x 27x 27x   1x     4x     1x     1x 1x   1x 12x 12x     1x     1x 1x 1x 1x     1x                  
 
export const datePicker = () => {
 
    const getAllYears = (): number[] => {
        const years = [];
 
        for (let i = 1900; i <= 2100; i++) {
            years.push(i);
        }
 
        return years;
    }
 
    const getDaysInMonth = (year: number, month: number): number[] => {
        const date = new Date(year, month, 1);
        const days = [];
 
        while (date.getMonth() === month) {
            days.push(new Date(date).getDate());
            date.setDate(date.getDate() + 1);
        }
 
        return days;
    }
 
    const getFirstDayOfMonth = (year: number, month: number): number => {
        return new Date(year, month, 1).getDay();
    }
 
    const arrangeDays = (days: number[], firstDay: number): any[] => {
        const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        const arrangedDays = [];
        let week: { [key: string]: number | null } = {};
        let dayIndex = 0;
 
        for (let i = 0; i < 7; i++) {
            if (i < firstDay || dayIndex >= days.length) {
                week[weekDays[i]] = null;
            } else {
                week[weekDays[i]] = days[dayIndex];
                dayIndex++;
            }
        }
 
        arrangedDays.push(week);
 
        while (dayIndex < days.length) {
            week = {};
            for (let i = 0; i < 7; i++) {
                if (dayIndex < days.length) {
                    week[weekDays[i]] = days[dayIndex];
                    dayIndex++;
                } else {
                    week[weekDays[i]] = null;
                }
            }
            arrangedDays.push(week);
        }
 
        return arrangedDays;
    }
 
    const getMonthNames = (): string[] => {
        const months = [];
 
        for (let i = 0; i < 12; i++) {
            const monthName = new Date(0, i).toLocaleString('default', { month: 'long' });
            months.push(monthName);
        }
 
        return months;
    }
 
    const formatDate = (day: number, month: number, year: number) => {
        const date = new Date(`${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`);
        const dateString = date.toISOString().split('T')[0].replace(/-/g, '/');
        return dateString;
    }
 
    return {
        getAllYears,
        getDaysInMonth,
        getMonthNames,
        getFirstDayOfMonth,
        arrangeDays,
        formatDate
    }
}