/**
 * @param date date which will be returned as czech date string
 * @returns czech date string, for example "25. 5. 2020", if date is null then returns ""
 */
export function czechStringfromDate(date: Date): string {
    if (date) {
        const ye = new Intl.DateTimeFormat('cz', {year: 'numeric'}).format(date);
        const mo = new Intl.DateTimeFormat('cz', {month: '2-digit'}).format(date);
        const da = new Intl.DateTimeFormat('cz', {day: '2-digit'}).format(date);

        return `${da}. ${mo}. ${ye}`;
    } else {
        return '';
    }
}

/**
 * @param dateString czech date, for example "25. 5. 2020", which will be returned as Date
 * @returns Date if dateString is in czech date format, else null
 */
export function dateFromCzechString(dateString: string): Date {
    if (dateString) {
        const date = dateString.split('.');
        if (date.length === 3) {
            return new Date(+date[2].trim(), +date[1].trim() - 1, +date[0].trim() + 1);
        }
    }

    return null;
}
