"use strict";

function parse(dateString, formatString, referenceDate = new Date) {
    const str = dateString.trim();
    if ("yyyy-MM-dd" === formatString) {
        const [year, month, day] = str.split("-").map(Number);
        return new Date(year, month - 1, day);
    }
    if ("yyyy-M-d" === formatString) {
        const [year, month, day] = str.split("-").map(Number);
        return new Date(year, month - 1, day);
    }
    if ("yyyy/MM/dd" === formatString) {
        const [year, month, day] = str.split("/").map(Number);
        return new Date(year, month - 1, day);
    }
    if ("yyyy/M/d" === formatString) {
        const [year, month, day] = str.split("/").map(Number);
        return new Date(year, month - 1, day);
    }
    if ("MM/dd/yyyy" === formatString) {
        const [month, day, year] = str.split("/").map(Number);
        return new Date(year, month - 1, day);
    }
    if ("yyyy年M月d日" === formatString) {
        const match = str.match(/^(\d{4})年(\d{1,2})月(\d{1,2})(日)?$/);
        if (match) return new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
    }
    const parsed = new Date(str);
    return isNaN(parsed.getTime()) ? new Date(referenceDate) : parsed;
}

function isValid(date) {
    return !isNaN(date.getTime());
}

function differenceInDays(dateLeft, dateRight) {
    const diffTime = Date.UTC(dateLeft.getFullYear(), dateLeft.getMonth(), dateLeft.getDate()) - Date.UTC(dateRight.getFullYear(), dateRight.getMonth(), dateRight.getDate());
    return Math.floor(diffTime / 864e5);
}

function addDays(date, amount) {
    const newDate = new Date(date);
    return newDate.setDate(newDate.getDate() + amount), newDate;
}

function format(date, formatString) {
    const year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(), pad = num => num.toString().padStart(2, "0");
    switch (formatString) {
      case "yyyy-MM-dd":
      default:
        return `${year}-${pad(month)}-${pad(day)}`;

      case "yyyy-M-d":
        return `${year}-${month}-${day}`;

      case "yyyy/MM/dd":
        return `${year}/${pad(month)}/${pad(day)}`;

      case "yyyy/M/d":
        return `${year}/${month}/${day}`;

      case "MM/dd/yyyy":
        return `${pad(month)}/${pad(day)}/${year}`;

      case "yyyy年M月d日":
        return `${year}年${month}月${day}日`;
    }
}

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.format = exports.addDays = exports.differenceInDays = exports.isValid = exports.parse = void 0, 
exports.parse = parse, exports.isValid = isValid, exports.differenceInDays = differenceInDays, 
exports.addDays = addDays, exports.format = format;
//# sourceMappingURL=date.js.map
