forked from melvinsembrano/date-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate-diff.js
More file actions
94 lines (75 loc) · 2.46 KB
/
date-diff.js
File metadata and controls
94 lines (75 loc) · 2.46 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Generated by CoffeeScript 2.3.1
(function() {
/*
* DateDiff
* is a minimalized Javascript date arithmetic extension.
* Copyright (c) 2015 Melvin Sembrano (melvinsembrano@gmail.com)
* License: MIT
*/
var DateDiff, divisors;
DateDiff = function(date1, date2) {
this.date1 = date1;
this.date2 = date2;
this.difference = Math.floor(date1 - date2);
};
divisors = {
days: 1000 * 60 * 60 * 24,
hours: 1000 * 60 * 60,
minutes: 1000 * 60,
seconds: 1000
};
DateDiff.prototype.weeks = function() {
return this._roundIt(this.days() / 7);
};
DateDiff.prototype.days = function() {
return this._roundIt(this.difference / divisors.days);
};
DateDiff.prototype.hours = function() {
return this._roundIt(this.difference / divisors.hours);
};
DateDiff.prototype.minutes = function() {
return this._roundIt(this.difference / divisors.minutes);
};
DateDiff.prototype.seconds = function() {
return this._roundIt(this.difference / divisors.seconds);
};
DateDiff.prototype.months = function() {
var eom, ret;
ret = (this.date1.getFullYear() - this.date2.getFullYear()) * 12;
ret += this.date1.getMonth() - this.date2.getMonth();
eom = this.endOfMonth(this.date2).getDate();
ret += (this.date1.getDate() / eom) - (this.date2.getDate() / eom);
return this._roundIt(ret);
};
DateDiff.prototype.years = function() {
var ret;
ret = this.date1.getFullYear() - this.date2.getFullYear();
ret += (this.dayOfYear(this.date1) - this.dayOfYear(this.date2)) / this.daysInYear(this.date2);
return this._roundIt(ret);
};
DateDiff.prototype.endOfMonth = function(date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0);
};
DateDiff.prototype.endOfYear = function(date) {
return new Date(date.getFullYear() + 1, 0, 0);
};
DateDiff.prototype.beginOfYear = function(date) {
return new Date(date.getFullYear(), 0, 0);
};
DateDiff.prototype.dayOfYear = function(date) {
return (date - this.beginOfYear(date)) / divisors.days;
};
DateDiff.prototype.daysInYear = function(date) {
return (this.endOfYear(date) - this.beginOfYear(date)) / divisors.days;
};
DateDiff.prototype._roundIt = function(v) {
return parseFloat(v.toFixed(1));
};
Date.diff = function(date1, date2) {
return new DateDiff(date1, date2);
};
this.DateDiff = DateDiff;
if (typeof module !== "undefined") {
module.exports = DateDiff;
}
}).call(this);