UNPKG

react-day-picker

Version:

Flexible date picker component for React

236 lines (219 loc) 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clone = clone; exports.isDate = isDate; exports.addMonths = addMonths; exports.isSameDay = isSameDay; exports.isSameMonth = isSameMonth; exports.isDayBefore = isDayBefore; exports.isDayAfter = isDayAfter; exports.isPastDay = isPastDay; exports.isFutureDay = isFutureDay; exports.isDayBetween = isDayBetween; exports.addDayToRange = addDayToRange; exports.isDayInRange = isDayInRange; exports.getWeekNumber = getWeekNumber; /** * Clone a date object. * * @export * @param {Date} d The date to clone * @return {Date} The cloned date */ function clone(d) { return new Date(d.getTime()); } /** * Return `true` if the passed value is a valid JavaScript Date object. * * @export * @param {any} value * @returns {Boolean} */ function isDate(value) { return value instanceof Date && !isNaN(value.valueOf()); } /** * Return `d` as a new date with `n` months added. * * @export * @param {Date} d * @param {number} n */ function addMonths(d, n) { var newDate = clone(d); newDate.setMonth(d.getMonth() + n); return newDate; } /** * Return `true` if two dates are the same day, ignoring the time. * * @export * @param {Date} d1 * @param {Date} d2 * @return {Boolean} */ function isSameDay(d1, d2) { if (!d1 || !d2) { return false; } return d1.getDate() === d2.getDate() && d1.getMonth() === d2.getMonth() && d1.getFullYear() === d2.getFullYear(); } /** * Return `true` if two dates fall in the same month. * * @export * @param {Date} d1 * @param {Date} d2 * @return {Boolean} */ function isSameMonth(d1, d2) { if (!d1 || !d2) { return false; } return d1.getMonth() === d2.getMonth() && d1.getFullYear() === d2.getFullYear(); } /** * Returns `true` if the first day is before the second day. * * @export * @param {Date} d1 * @param {Date} d2 * @returns {Boolean} */ function isDayBefore(d1, d2) { var day1 = clone(d1).setHours(0, 0, 0, 0); var day2 = clone(d2).setHours(0, 0, 0, 0); return day1 < day2; } /** * Returns `true` if the first day is after the second day. * * @export * @param {Date} d1 * @param {Date} d2 * @returns {Boolean} */ function isDayAfter(d1, d2) { var day1 = clone(d1).setHours(0, 0, 0, 0); var day2 = clone(d2).setHours(0, 0, 0, 0); return day1 > day2; } /** * Return `true` if a day is in the past, e.g. yesterday or any day * before yesterday. * * @export * @param {Date} d * @return {Boolean} */ function isPastDay(d) { var today = new Date(); today.setHours(0, 0, 0, 0); return isDayBefore(d, today); } /** * Return `true` if a day is in the future, e.g. tomorrow or any day * after tomorrow. * * @export * @param {Date} d * @return {Boolean} */ function isFutureDay(d) { var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000); tomorrow.setHours(0, 0, 0, 0); return d >= tomorrow; } /** * Return `true` if day `d` is between days `d1` and `d2`, * without including them. * * @export * @param {Date} d * @param {Date} d1 * @param {Date} d2 * @return {Boolean} */ function isDayBetween(d, d1, d2) { var date = clone(d); date.setHours(0, 0, 0, 0); return isDayAfter(date, d1) && isDayBefore(date, d2) || isDayAfter(date, d2) && isDayBefore(date, d1); } /** * Add a day to a range and return a new range. A range is an object with * `from` and `to` days. * * @export * @param {Date} day * @param {Object} range * @return {Object} Returns a new range object */ function addDayToRange(day) { var range = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { from: null, to: null }; var from = range.from, to = range.to; if (!from) { from = day; } else if (from && to && isSameDay(from, to) && isSameDay(day, from)) { from = null; to = null; } else if (to && isDayBefore(day, from)) { from = day; } else if (to && isSameDay(day, to)) { from = day; to = day; } else { to = day; if (isDayBefore(to, from)) { to = from; from = day; } } return { from: from, to: to }; } /** * Return `true` if a day is included in a range of days. * * @export * @param {Date} day * @param {Object} range * @return {Boolean} */ function isDayInRange(day, range) { var from = range.from, to = range.to; return from && isSameDay(day, from) || to && isSameDay(day, to) || from && to && isDayBetween(day, from, to); } /** * Return the year's week number (as per ISO, i.e. with the week starting from monday) * for the given day. * * @export * @param {Date} day * @returns {Number} */ function getWeekNumber(day) { var date = clone(day); date.setHours(0, 0, 0); date.setDate(date.getDate() + 4 - (date.getDay() || 7)); return Math.ceil(((date - new Date(date.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7); } exports.default = { addDayToRange: addDayToRange, addMonths: addMonths, clone: clone, getWeekNumber: getWeekNumber, isDate: isDate, isDayAfter: isDayAfter, isDayBefore: isDayBefore, isDayBetween: isDayBetween, isDayInRange: isDayInRange, isFutureDay: isFutureDay, isPastDay: isPastDay, isSameDay: isSameDay, isSameMonth: isSameMonth }; //# sourceMappingURL=DateUtils.js.map