office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
196 lines (194 loc) • 9.24 kB
JavaScript
define(["require", "exports", "../dateValues/DateValues"], function (require, exports, DateValues_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DAYS_IN_WEEK = 7;
var MONTHS_IN_YEAR = 12;
/**
* Returns a date offset from the given date by the specified number of days.
* @param {Date} date - The origin date
* @param {number} days - The number of days to offset. 'days' can be negative.
* @return {Date} A new Date object offset from the origin date by the given number of days
*/
function addDays(date, days) {
var result = new Date(date.getTime());
result.setDate(result.getDate() + days);
return result;
}
exports.addDays = addDays;
/**
* Returns a date offset from the given date by the specified number of weeks.
* @param {Date} date - The origin date
* @param {number} weeks - The number of weeks to offset. 'weeks' can be negative.
* @return {Date} A new Date object offset from the origin date by the given number of weeks
*/
function addWeeks(date, weeks) {
return addDays(date, weeks * DAYS_IN_WEEK);
}
exports.addWeeks = addWeeks;
/**
* Returns a date offset from the given date by the specified number of months.
* The method tries to preserve the day-of-month; however, if the new month does not have enough days
* to contain the original day-of-month, we'll use the last day of the new month.
* @param {Date} date - The origin date
* @param {number} months - The number of months to offset. 'months' can be negative.
* @return {Date} A new Date object offset from the origin date by the given number of months
*/
function addMonths(date, months) {
var result = new Date(date.getTime());
var newMonth = result.getMonth() + months;
result.setMonth(newMonth);
// We want to maintain the same day-of-month, but that may not be possible if the new month doesn't have enough days.
// Loop until we back up to a day the new month has.
// (Weird modulo math is due to Javascript's treatment of negative numbers in modulo)
if (result.getMonth() !== ((newMonth % MONTHS_IN_YEAR) + MONTHS_IN_YEAR) % MONTHS_IN_YEAR) {
result = addDays(result, -result.getDate());
}
return result;
}
exports.addMonths = addMonths;
/**
* Returns a date offset from the given date by the specified number of years.
* The method tries to preserve the day-of-month; however, if the new month does not have enough days
* to contain the original day-of-month, we'll use the last day of the new month.
* @param {Date} date - The origin date
* @param {number} years - The number of years to offset. 'years' can be negative.
* @return {Date} A new Date object offset from the origin date by the given number of years
*/
function addYears(date, years) {
var result = new Date(date.getTime());
result.setFullYear(date.getFullYear() + years);
// We want to maintain the same day-of-month, but that may not be possible if the new month doesn't have enough days.
// Loop until we back up to a day the new month has.
// (Weird modulo math is due to Javascript's treatment of negative numbers in modulo)
if (result.getMonth() !== ((date.getMonth() % MONTHS_IN_YEAR) + MONTHS_IN_YEAR) % MONTHS_IN_YEAR) {
result = addDays(result, -result.getDate());
}
return result;
}
exports.addYears = addYears;
/**
* Returns a date that is a copy of the given date, aside from the month changing to the given month.
* The method tries to preserve the day-of-month; however, if the new month does not have enough days
* to contain the original day-of-month, we'll use the last day of the new month.
* @param {Date} date - The origin date
* @param {number} month - The 0-based index of the month to set on the date.
* @return {Date} A new Date object with the given month set.
*/
function setMonth(date, month) {
return addMonths(date, month - date.getMonth());
}
exports.setMonth = setMonth;
/**
* Compares two dates, and returns true if the two dates (not accounting for time-of-day) are equal.
* @return {boolean} True if the two dates represent the same date (regardless of time-of-day), false otherwise.
*/
function compareDates(date1, date2) {
if (date1 == null && date2 == null) {
return true;
}
else if (date1 == null || date2 == null) {
return false;
}
else {
return (date1.getFullYear() === date2.getFullYear()
&& date1.getMonth() === date2.getMonth()
&& date1.getDate() === date2.getDate());
}
}
exports.compareDates = compareDates;
/**
* Compare the date parts of two dates
* @param {Date} date1 - The first date to compare
* @param {Date} date2 - The second date to compare
* @returns {Number} A negative value if date1 is earlier than date2, 0 if the dates are equal, or a positive value
* if date1 is later than date2.
*/
function compareDatePart(date1, date2) {
return getDatePartHashValue(date1) - getDatePartHashValue(date2);
}
exports.compareDatePart = compareDatePart;
/**
* Gets the date range array including the specified date. The date range array is calculated as the list
* of dates accounting for the specified first day of the week and date range type.
* @param {Date} date - The input date
* @param {DateRangeType} dateRangeType - The desired date range type, i.e., day, week, month, etc.
* @param {DayOfWeek} dayOfWeek - The first day of the week.
* @returns {Date[]} An array of dates representing the date range containing the specified date.
*/
function getDateRangeArray(date, dateRangeType, firstDayOfWeek) {
var datesArray = new Array();
var startDate = null;
var endDate = null;
switch (dateRangeType) {
case DateValues_1.DateRangeType.Day:
startDate = getDatePart(date);
endDate = addDays(startDate, 1);
break;
case DateValues_1.DateRangeType.Week:
startDate = getStartDateOfWeek(getDatePart(date), firstDayOfWeek);
endDate = addDays(startDate, DAYS_IN_WEEK);
break;
case DateValues_1.DateRangeType.Month:
startDate = new Date(date.getFullYear(), date.getMonth(), 1);
endDate = addMonths(startDate, 1);
break;
}
// Populate the dates array with the dates in range
datesArray.push(startDate);
var nextDate = addDays(startDate, 1);
while (!compareDates(nextDate, endDate)) {
datesArray.push(nextDate);
nextDate = addDays(nextDate, 1);
}
return datesArray;
}
exports.getDateRangeArray = getDateRangeArray;
/**
* Checks whether the specified date is in the given date range.
* @param {Date} date - The origin date
* @param {Date[]} dateRange - An array of dates to do the lookup on
* @returns {bool} True if the date matches one of the dates in the specified array, false otherwise.
*/
function isInDateRangeArray(date, dateRange) {
for (var _i = 0, dateRange_1 = dateRange; _i < dateRange_1.length; _i++) {
var dateInRange = dateRange_1[_i];
if (compareDates(date, dateInRange)) {
return true;
}
}
return false;
}
exports.isInDateRangeArray = isInDateRangeArray;
/**
* Gets a new date with the time portion zeroed out, i.e., set to midnight
* @param {Date} date - The origin date
* @returns {Date} A new date with the time set to midnight
*/
function getDatePart(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
/**
* Gets the date for the first day of the week based on the given date assuming
* the specified first day of the week.
* @param {Date} date - The date to find the beginning of the week date for.
* @return {Date} A new date object representing the first day of the week containing the input date.
*/
function getStartDateOfWeek(date, firstDayOfWeek) {
var daysOffset = firstDayOfWeek - date.getDay();
if (daysOffset > 0) {
// If first day of week is > date, go 1 week back, to ensure resulting date is in the past.
daysOffset -= DAYS_IN_WEEK;
}
return addDays(date, daysOffset);
}
/**
* Helper function to assist in date comparisons
*/
function getDatePartHashValue(date) {
// Generate date hash value created as sum of Date (up to 31 = 5 bits), Month (up to 11 = 4 bits) and Year.
/* tslint:disable:no-bitwise */
return date.getDate() + (date.getMonth() << 5) + (date.getFullYear() << 9);
/* tslint:enable:no-bitwise */
}
});
//# sourceMappingURL=DateMath.js.map