export class DateBuilder { // constructors constructor(date = new Date()) { if (!(date instanceof Date)) { throw new Error("Invalid date object."); } this.date = new Date(date.getTime()); // Create a copy to avoid mutating the original date } // methods addYears(years) { this.date.setFullYear(this.date.getFullYear() + years); return this; } addMonths(months) { this.date.setMonth(this.date.getMonth() + months); return this; } addDays(days) { this.date.setDate(this.date.getDate() + days); return this; } addHours(hours) { this.date.setHours(this.date.getHours() + hours); return this; } addMinutes(minutes) { this.date.setMinutes(this.date.getMinutes() + minutes); return this; } addSeconds(seconds) { this.date.setSeconds(this.date.getSeconds() + seconds); return this; } addMillis(millis) { this.date.setTime(this.date.getTime() + millis); return this; } withDate(year, month, day) { this.date.setFullYear(year, month - 1, day); // month is 0-based return this; } withTime(hour, minute = 0, second = 0, millisecond = 0) { this.date.setHours(hour, minute, second, millisecond); return this; } atStartOfDay() { this.date.setHours(0, 0, 0, 0); return this; } atEndOfDay() { this.date.setHours(23, 59, 59, 999); return this; } build() { return new Date(this.date.getTime()); // Return a copy to avoid external mutation } }