export function isValid(date) {
    return date instanceof Date && !isNaN(date.getTime());
}

export function differenceInDays(dateLeft, dateRight) {
    if (!isValid(dateLeft) || !isValid(dateRight)) throw new Error("Invalid date passed to differenceInDays");
    const leftTime = Date.UTC(dateLeft.getFullYear(), dateLeft.getMonth(), dateLeft.getDate()), rightTime = Date.UTC(dateRight.getFullYear(), dateRight.getMonth(), dateRight.getDate());
    return Math.floor((rightTime - leftTime) / 864e5);
}

export function addDays(date, amount) {
    if (!isValid(date)) throw new Error("Invalid date passed to addDays");
    const newDate = new Date(date);
    return newDate.setDate(newDate.getDate() + amount), newDate;
}

export function parse(dateString, format, referenceDate = new Date) {
    const yearMatch = format.match(/y+/), monthMatch = format.match(/M+/), dayMatch = format.match(/d+/);
    if (!yearMatch || !monthMatch || !dayMatch) throw new Error("Format string must contain yyyy, MM (or M), and dd (or d)");
    const yearIndex = format.indexOf(yearMatch[0]), monthIndex = format.indexOf(monthMatch[0]), dayIndex = format.indexOf(dayMatch[0]), year = parseInt(dateString.substring(yearIndex, yearIndex + yearMatch[0].length), 10), month = parseInt(dateString.substring(monthIndex, monthIndex + monthMatch[0].length), 10) - 1, day = parseInt(dateString.substring(dayIndex, dayIndex + dayMatch[0].length), 10), fullYear = year < 100 ? 2e3 + year : year, date = new Date(fullYear, month, day);
    return date.getFullYear() !== fullYear || date.getMonth() !== month || date.getDate() !== day ? new Date(NaN) : date;
}

export function format(date, format) {
    if (!isValid(date)) throw new Error("Invalid date passed to format");
    const year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate();
    return format.replace("yyyy", year.toString()).replace("MM", month.toString().padStart(2, "0")).replace("M", month.toString()).replace("dd", day.toString().padStart(2, "0")).replace("d", day.toString());
}
//# sourceMappingURL=date-tools.js.map