-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.js
More file actions
203 lines (180 loc) · 5.07 KB
/
Time.js
File metadata and controls
203 lines (180 loc) · 5.07 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* 传入参数,生成相应对象。
* @param date js Date对象或者yyyy-MM-dd hh:mm:ss,
*/
(function(global) {
'use strict';
var Calendar = function(date) {
//设置正确的日期
this.d = new Date(0);
if(date instanceof Date) {
this.d = date;
} else if (typeof date === 'string') {
var ds = date.split(' ');
ds[1] = ds[1] || '00:00:00';
var dates = ds[0].split('-'),
times = ds[1].split(':');
this.d.setFullYear(dates[0], dates[1] - 1, dates[2]);
this.d.setHours(times[0] || '00');
this.d.setMinutes(times[1] || '00');
this.d.setSeconds(times[2] || '00');
} else if (date instanceof Calendar){
return date;
}
this.data = this.fillData(this.d);
return this;
}
global.Calendar = Calendar;
var cp = Calendar.prototype;
/**
* 补全字段到两位
*/
cp.complete = function(time, completion) {
time = parseInt(time);
if (completion) {
time = (time < 10) ? ('0' + time) : ('' + time);
}
return time;
}
/**
* 根据时间补充其他字段
*/
cp.fillData = function(d) {
d = d || this.d;
var data = {
year : d.getFullYear(),
month : d.getMonth() + 1,
date : d.getDate(),
hour : d.getHours(),
minute : d.getMinutes(),
second : d.getSeconds(),
day : '星期'
};
var dayInfo = ['日','一','二','三','四','五','六'];
data['day'] += dayInfo[d.getDay()];
return data;
}
/**
* 格式化输出
*/
cp.format = function(format) {
format = format || '';
return format.replace(/y+/g, this.data['year'])
.replace(/M+/g, this.complete(this.data['month'], true))
.replace(/d+/g, this.complete(this.data['date'], true))
.replace(/h+/g, this.complete(this.data['hour'], true))
.replace(/m+/g, this.complete(this.data['minute'], true))
.replace(/s+/g, this.complete(this.data['second'], true))
.replace(/D+/g, this.data['day']);
}
/**
* 为指定的日历字段添加或减去指定的时间量
*/
cp.add = function(field, amount) {
if(!field || !amount) {
return;
}
var newVal = this.data[field] + amount;
switch(field) {
case 'year': this.d.setFullYear(newVal); break;
case 'month':
this.d.setDate(1);
this.d.setMonth(newVal);
this.d.setDate(0);
if(this.d.getDate() > this.data['date']) {
this.d.setDate(this.data['date']);
}
break;
case 'date': this.d.setDate(newVal); break;
case 'hour': this.d.setHours(newVal); break;
case 'minute': this.d.setMinutes(newVal); break;
case 'second': this.d.setSeconds(newVal); break;
}
this.data = this.fillData(this.d);
return this;
}
/**
* 比较两个时间
*/
cp.compareTo = function(anotherCalendar) {
anotherCalendar = new Calendar(anotherCalendar);
var anotherTime = anotherCalendar.get('d', true).getTime();
return this.get('d').getTime() - anotherTime;
}
/**
* 获得某一字段的数值, 默认不进行格式化
*/
cp.get = function(field, completion) {
var result = undefined;
if(field === 'd') {
return this.d;
} else if(field) {
result = this.data[field];
}
if(result !== undefined && result !== null && field !== 'day') {
result = this.complete(result, completion);
}
return result;
}
/**
* 当前时间的下个月
* keepDay: true 日期不变,
* false 日期变为月末
*/
cp.nextMonth = function(keepDay) {
this.d.setDate(1);
this.d.setMonth(this.data['month'] + 1);
this.d.setDate(0);
if(keepDay && this.d.getDate() > this.data['date']) {
this.d.setDate(this.data['date']);
}
this.data = this.fillData(this.d);
return this;
}
/**
* 当前时间的上个月
*/
cp.lastMonth = function(keepDay) {
this.d.setDate(0);
if(keepDay && this.d.getDate() > this.data['date']) {
this.d.setDate(this.data['date']);
}
this.data = this.fillData(this.d);
return this;
}
/**
* 当前时间的明天
*/
cp.tomorrow = function() {
this.d.setDate(this.data['date'] + 1);
this.data = this.fillData(this.d);
return this;
}
/**
* 当前时间的昨天
*/
cp.yesterday = function() {
this.d.setDate(this.data['date'] - 1);
this.data = this.fillData(this.d);
return this;
}
/**
* 参数月份中的天数
*/
Calendar.daysInMonth = function(year, month) {
year = parseInt(year);
month = parseInt(month);
var d = new Date();
d.setFullYear(year);
d.setDate(1);
d.setMonth(month);
d.setDate(0);
return d.getDate();
}
/**
* 获得当前时间的日历
*/
Calendar.now = function() {
return new Calendar(new Date());
}
})(window || {});