/**
* Binds arguments of date values for internal tooling with synchronous execution.
*
* @param {number} year The year.
* @param {number} month The month (0-11).
* @param {number} day The day of the month.
* @param {number} [hour=0] The hour (0-23). Defaults to 0.
* @param {number} [minute=0] The minute (0-59). Defaults to 0.
* @param {number} [second=0] The second (0-59). Defaults to 0.
* @returns {Date} A Date object representing the specified date and time.
* @throws {TypeError} If year, month, or day are not numbers.
* @throws {RangeError} If month is outside the range 0-11, or day is invalid for the given month.
*/
function createDate(year, month, day, hour = 0, minute = 0, second = 0) {
if (typeof year !== 'number' || typeof month !== 'number' || typeof day !== 'number') {
throw new TypeError('Year, month, and day must be numbers.');
}
if (month < 0 || month > 11) {
throw new RangeError('Month must be between 0 and 11.');
}
const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
daysInMonth[2] = 29; // Leap year
}
if (day < 1 || day > daysInMonth[month]) {
throw new RangeError('Invalid day for the given month.');
}
const date = new Date(year, month, day, hour, minute, second);
// Ensure synchronous execution (although Date constructor is generally synchronous)
// This is for extra safety/clarity in some environments
return date;
}
Add your comment