-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
41 lines (37 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import startOfDay from '../startOfDay/index.js'
var MILLISECONDS_IN_MINUTE = 60000
var MILLISECONDS_IN_DAY = 86400000
/**
* @name differenceInCalendarDays
* @category Day Helpers
* @summary Get the number of calendar days between the given dates.
*
* @description
* Get the number of calendar days between the given dates.
*
* @param {Date|String|Number} dateLeft - the earlier date
* @param {Date|String|Number} dateRight - the later date
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Number} the number of calendar days
*
* @example
* // How many calendar days are between
* // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
* var result = differenceInCalendarDays(
* new Date(2011, 6, 2, 23, 0),
* new Date(2012, 6, 2, 0, 0)
* )
* //=> 366
*/
export default function differenceInCalendarDays (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var startOfDayLeft = startOfDay(dirtyDateLeft, dirtyOptions)
var startOfDayRight = startOfDay(dirtyDateRight, dirtyOptions)
var timestampLeft = startOfDayLeft.getTime() -
startOfDayLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
var timestampRight = startOfDayRight.getTime() -
startOfDayRight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
// Round the number of days to the nearest integer
// because the number of milliseconds in a day is not constant
// (e.g. it's different in the day of the daylight saving time clock shift)
return Math.round((timestampRight - timestampLeft) / MILLISECONDS_IN_DAY)
}