dateUnit.js
3.77 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
var dateUnit={
dateAdd:function(strInterval, NumDay, dtTmp){
if (dtTmp == null | dtTmp == "")
dtTmp = new Date();
switch (strInterval) {
case "m":
return new Date(Date.parse(dtTmp) + (60000 * NumDay));
case "h":
return new Date(Date.parse(dtTmp) + (3600000 * NumDay));
case "d":
return new Date(Date.parse(dtTmp) + (86400000 * (NumDay + 1)));
case "w":
return new Date(Date.parse(dtTmp) + ((86400000 * 7) * NumDay)
+ 86400000);
case "M":
return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + NumDay, dtTmp
.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
.getSeconds());
case "y":
return new Date((dtTmp.getFullYear() + NumDay), dtTmp.getMonth(), dtTmp
.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
.getSeconds());
}
},
dateFormat:function(paramDate, fmt){
var date = null
date = typeof paramDate === 'string'
? new Date(paramDate.replace(/-/g, '/'))
: paramDate
var o = {
"M+" : date.getMonth() + 1, // 月份
"d+" : date.getDate(), // 日
"h+" : date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 小时
"H+" : date.getHours(), // 小时
"m+" : date.getMinutes(), // 分
"s+" : date.getSeconds(), // 秒
"q+" : Math.floor((date.getMonth() + 3) / 3), // 季度
"S" : date.getMilliseconds()
// 毫秒
};
var week = {
"0" : "\u65e5",
"1" : "\u4e00",
"2" : "\u4e8c",
"3" : "\u4e09",
"4" : "\u56db",
"5" : "\u4e94",
"6" : "\u516d"
};
// var week = {
// "0" : "Sun.",
// "1" : "Mon.",
// "2" : "Tues.",
// "3" : "Wed.",
// "4" : "Thur.",
// "5" : "Fri.",
// "6" : "Sat."
// };
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt
.replace(
RegExp.$1,
((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f"
: "\u5468")
: "")
+ week[date.getDay() + ""]);
}
// if (/(E+)/.test(fmt)) {
// fmt = fmt
// .replace(
// RegExp.$1,
// ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? ""
// : "")
// : "")
// + week[date.getDay() + ""]);
// }
for ( var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
: (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
},
strToDate:function(strDate) {
var st = strDate;
var date;
if(st.indexOf(' ')!=-1){
var a = st.split(" ");
var b = a[0].split("-");
var c = a[1].split(":");
date = new Date(b[0], b[1], b[2], c[0], c[1], c[2]);
}else{
var c = st.split(":");
if(c[2]){
date= new Date(2000, 1, 1, c[0], c[1], c[2]);
}else{
date= new Date(2000, 1, 1, c[0], c[1], 0);
}
}
return date;
},
dateNeat: function(date,type) {
var clickDate = new Date(date.getTime());
var nowDate = new Date();
switch(type) {
case 'day':
return dateUnit.dateFormat(nowDate,'yyyy-MM-dd')
case 'week':
dateUnit.compareTime(clickDate,nowDate)
// return dateUnit.dateFormat(nowDate,'yyyy-MM-dd');
case 'month':
var yearMonth = dateUnit.dateFormat(nowDate,'yyyy-MM');
return yearMonth + '-01';
case 'year':
var year = dateUnit.dateFormat(nowDate, 'yyyy');
return year + '-01-01'
}
},
compareTime: function(click_date,now_date) {
var weekDay = now_date.getDay();
var startDay = weekDay ? weekDay - 1 : 6;
var now_t = now_date.setDate(now_date.getDate() - startDay);
var week_Day = click_date.getDay();
var start_Day = week_Day ? week_Day - 1 : 6;
var click_t = click_date.setDate(click_date.getDate() - start_Day);
}
}