UNPKG

1.09 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.differenceInDays = differenceInDays;
7var DAY = 60 * 60 * 24;
8var HOUR = 60 * 60;
9var MINUTE = 60;
10var SECOND = 1;
11
12/**
13 * Get the difference between the start and the end time.
14 * For the countdown we need the days in absolute amounts, and the
15 * hours, minutes & seconds relative.
16 *
17 * If there is no end date passed in, we grab the current time.
18 *
19 * @param {String} start
20 * @param {String} end
21 * @return {Object}
22 */
23function differenceInDays(start, end) {
24 var startDate = start ? new Date(start) : new Date();
25 var endDate = new Date(end);
26 var difference = Math.abs(endDate.getTime() - startDate.getTime()) / 1000;
27
28 var days = Math.floor(difference / DAY);
29 var hours = Math.floor((difference - days * DAY) / HOUR);
30 var minutes = Math.floor((difference - days * DAY - hours * HOUR) / MINUTE);
31 var seconds = Math.floor((difference - days * DAY - hours * HOUR - minutes * MINUTE) / SECOND);
32
33 return {
34 days: days,
35 hours: hours,
36 minutes: minutes,
37 seconds: seconds
38 };
39}
\No newline at end of file