67 lines
2.3 KiB
JavaScript
67 lines
2.3 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = differenceInDays;
|
||
|
|
||
|
var _index = _interopRequireDefault(require("../toDate/index.js"));
|
||
|
|
||
|
var _index2 = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
|
||
|
|
||
|
var _index3 = _interopRequireDefault(require("../compareAsc/index.js"));
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* @name differenceInDays
|
||
|
* @category Day Helpers
|
||
|
* @summary Get the number of full days between the given dates.
|
||
|
*
|
||
|
* @description
|
||
|
* Get the number of full day periods between the given dates.
|
||
|
*
|
||
|
* ### v2.0.0 breaking changes:
|
||
|
*
|
||
|
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
|
||
|
*
|
||
|
* @param {Date|Number} dateLeft - the later date
|
||
|
* @param {Date|Number} dateRight - the earlier date
|
||
|
* @returns {Number} the number of full days
|
||
|
* @throws {TypeError} 2 arguments required
|
||
|
*
|
||
|
* @example
|
||
|
* // How many full days are between
|
||
|
* // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
|
||
|
* var result = differenceInDays(
|
||
|
* new Date(2012, 6, 2, 0, 0),
|
||
|
* new Date(2011, 6, 2, 23, 0)
|
||
|
* )
|
||
|
* //=> 365
|
||
|
* // How many days are between
|
||
|
* // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
|
||
|
* var result = differenceInDays(
|
||
|
* new Date(2011, 6, 3, 0, 1),
|
||
|
* new Date(2011, 6, 2, 23, 59)
|
||
|
* )
|
||
|
* //=> 0
|
||
|
*/
|
||
|
function differenceInDays(dirtyDateLeft, dirtyDateRight) {
|
||
|
if (arguments.length < 2) {
|
||
|
throw new TypeError('2 arguments required, but only ' + arguments.length + ' present');
|
||
|
}
|
||
|
|
||
|
var dateLeft = (0, _index.default)(dirtyDateLeft);
|
||
|
var dateRight = (0, _index.default)(dirtyDateRight);
|
||
|
var sign = (0, _index3.default)(dateLeft, dateRight);
|
||
|
var difference = Math.abs((0, _index2.default)(dateLeft, dateRight));
|
||
|
dateLeft.setDate(dateLeft.getDate() - sign * difference); // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
|
||
|
// If so, result must be decreased by 1 in absolute value
|
||
|
|
||
|
var isLastDayNotFull = (0, _index3.default)(dateLeft, dateRight) === -sign;
|
||
|
var result = sign * (difference - isLastDayNotFull); // Prevent negative zero
|
||
|
|
||
|
return result === 0 ? 0 : result;
|
||
|
}
|
||
|
|
||
|
module.exports = exports.default;
|