grommet
Version:
focus on the essential experience
127 lines (124 loc) • 4.81 kB
JavaScript
// Utility functions for the Calendar.
// Just what's needed to avoid having to include a dependency like momentjs.
var DAY_MILLISECONDS = 24 * 60 * 60 * 1000;
export var addDays = function addDays(date, days) {
var result = new Date(date.getTime() + DAY_MILLISECONDS * days);
// Deal with crossing the daylight savings time boundary,
// where adding a day's worth when the time is midnight results in
// being a day off.
var hourDelta = result.getHours() - date.getHours();
// At this point, hourDelta is typically 0 (normal day),
// +23 (November daylight saving), or -23 (March Daylight saving)
// depending on which side of the switch we are on.
// Convert so that hourDelta is either +1 or -1.
if (hourDelta === 23) {
hourDelta -= 24;
} else if (hourDelta === -23) {
hourDelta += 24;
}
result.setHours(result.getHours() - hourDelta);
return result;
};
export var subtractDays = function subtractDays(date, days) {
return addDays(date, -days);
};
export var addMonths = function addMonths(date, months) {
var result = new Date(date);
var years = Math.floor((date.getMonth() + months) / 12);
result.setFullYear(date.getFullYear() + years);
var targetMonth = (date.getMonth() + months) % 12;
result.setMonth(targetMonth < 0 ? 12 + targetMonth : targetMonth);
return result;
};
export var subtractMonths = function subtractMonths(date, months) {
return addMonths(date, -months);
};
export var startOfMonth = function startOfMonth(date) {
var result = new Date(date);
result.setDate(1);
return result;
};
export var endOfMonth = function endOfMonth(date) {
var result = addMonths(date, 1);
result.setDate(0);
return result;
};
export var sameDay = function sameDay(date1, date2) {
return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate();
};
export var sameDayOrAfter = function sameDayOrAfter(date1, date2) {
return date1.getFullYear() > date2.getFullYear() || date1.getFullYear() === date2.getFullYear() && (date1.getMonth() > date2.getMonth() || date1.getMonth() === date2.getMonth() && date1.getDate() >= date2.getDate());
};
export var sameDayOrBefore = function sameDayOrBefore(date1, date2) {
return date1.getFullYear() < date2.getFullYear() || date1.getFullYear() === date2.getFullYear() && (date1.getMonth() < date2.getMonth() || date1.getMonth() === date2.getMonth() && date1.getDate() <= date2.getDate());
};
export var daysApart = function daysApart(date1, date2) {
return Math.floor((date1.getTime() - date2.getTime()) / DAY_MILLISECONDS);
};
// betweenDates takes an array of two elements and checks if the
// supplied date lies between them, inclusive.
// returns 2 if exact match to one end, 1 if between, undefined otherwise
export var betweenDates = function betweenDates(date, dates) {
var result;
if (dates) {
var _ref = Array.isArray(dates) ? dates.map(function (d) {
return d ? new Date(d) : undefined;
}) : [dates, undefined],
from = _ref[0],
to = _ref[1];
if (from && sameDay(date, from) || to && sameDay(date, to)) {
result = 2;
} else if (from && sameDayOrAfter(date, from) && to && sameDayOrBefore(date, to)) {
result = 1;
}
} else {
result = 1;
}
return result;
};
export var getRangePosition = function getRangePosition(date, dates) {
var rangePosition;
if (dates) {
var _ref2 = Array.isArray(dates) ? dates.map(function (d) {
return d ? new Date(d) : undefined;
}) : [dates, undefined],
from = _ref2[0],
to = _ref2[1];
if (from && sameDay(date, from)) rangePosition = 'start';else if (to && sameDay(date, to)) rangePosition = 'end';
}
return rangePosition;
};
// withinDates takes an array of string dates or 2 element arrays and
// checks whether the supplied date matches any string or is between
// any dates in arrays.
// returns 2 if exact match, 1 if between, undefined otherwise
export var withinDates = function withinDates(date, dates) {
var result;
var rangePosition;
if (dates) {
if (Array.isArray(dates)) {
dates.some(function (d) {
if (d instanceof Date) {
if (sameDay(date, d)) {
result = 2;
}
} else {
result = betweenDates(date, d);
rangePosition = getRangePosition(date, d);
}
return result;
});
} else if (sameDay(date, dates)) {
result = 2;
}
}
return [result, rangePosition];
};
export var handleOffset = function handleOffset(date) {
var normalizedDate = new Date(date);
var offset = normalizedDate.getTimezoneOffset();
var hour = normalizedDate.getHours();
// add back offset
normalizedDate.setHours(hour, offset < 0 ? -offset : offset);
return normalizedDate;
};