Date.js
3.65 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Ext.define('Ext.calendar.util.Date', {
singleton: true,
diffDays: function(start, end) {
var day = 1000 * 60 * 60 * 24,
clear = Ext.Date.clearTime,
diff = clear(end, true).getTime() - clear(start, true).getTime();
return Math.ceil(diff / day);
},
copyTime: function(fromDt, toDt) {
var dt = Ext.Date.clone(toDt);
dt.setHours(
fromDt.getHours(),
fromDt.getMinutes(),
fromDt.getSeconds(),
fromDt.getMilliseconds());
return dt;
},
compare: function(dt1, dt2, precise) {
if (precise !== true) {
dt1 = Ext.Date.clone(dt1);
dt1.setMilliseconds(0);
dt2 = Ext.Date.clone(dt2);
dt2.setMilliseconds(0);
}
return dt2.getTime() - dt1.getTime();
},
// private helper fn
maxOrMin: function(max) {
var dt = (max ? 0: Number.MAX_VALUE),
i = 0,
args = arguments[1],
ln = args.length;
for (; i < ln; i++) {
dt = Math[max ? 'max': 'min'](dt, args[i].getTime());
}
return new Date(dt);
},
max: function() {
return this.maxOrMin.apply(this, [true, arguments]);
},
min: function() {
return this.maxOrMin.apply(this, [false, arguments]);
},
today: function() {
return Ext.Date.clearTime(new Date());
},
/**
* Adds time to the specified date and returns a new Date instance as the result (does not
* alter the original date object). Time can be specified in any combination of milliseconds
* to years, and the function automatically takes leap years and daylight savings into account.
* Some syntax examples:<code><pre>
var now = new Date();
// Add 24 hours to the current date/time:
var tomorrow = Extensible.Date.add(now, { days: 1 });
// More complex, returning a date only with no time value:
var futureDate = Extensible.Date.add(now, {
weeks: 1,
days: 5,
minutes: 30,
clearTime: true
});
</pre></code>
* @param {Date} dt The starting date to which to add time
* @param {Object} o A config object that can contain one or more of the following
* properties, each with an integer value:
*
* - millis
* - seconds
* - minutes
* - hours
* - days
* - weeks
* - months
* - years
*
* You can also optionally include the property "clearTime: true" which will perform all of the
* date addition first, then clear the time value of the final date before returning it.
* @return {Date} A new date instance containing the resulting date/time value
*/
add : function(dt, o) {
if (!o) {
return dt;
}
var ExtDate = Ext.Date,
dateAdd = ExtDate.add,
newDt = ExtDate.clone(dt);
if (o.years) {
newDt = dateAdd(newDt, ExtDate.YEAR, o.years);
}
if (o.months) {
newDt = dateAdd(newDt, ExtDate.MONTH, o.months);
}
if (o.weeks) {
o.days = (o.days || 0) + (o.weeks * 7);
}
if (o.days) {
newDt = dateAdd(newDt, ExtDate.DAY, o.days);
}
if (o.hours) {
newDt = dateAdd(newDt, ExtDate.HOUR, o.hours);
}
if (o.minutes) {
newDt = dateAdd(newDt, ExtDate.MINUTE, o.minutes);
}
if (o.seconds) {
newDt = dateAdd(newDt, ExtDate.SECOND, o.seconds);
}
if (o.millis) {
newDt = dateAdd(newDt, ExtDate.MILLI, o.millis);
}
return o.clearTime ? ExtDate.clearTime(newDt) : newDt;
}
});