dateUnit.js
3.82 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
/*
* @Author: your name
* @Date: 2021-12-30 08:38:29
* @LastEditTime: 2021-12-30 08:38:29
* @LastEditors: Please set LastEditors
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: /SDC-Face-Web/src/assets/js/dateUnit.js
*/
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(date, fmt){
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" : "Sunday",
"1" : "Monday",
"2" : "Tuesday",
"3" : "Wednesday",
"4" : "Thursday",
"5" : "Friday",
"6" : "Saturday"
};
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;
}
}
var utiljs = {};
utiljs.weekComputedfn = {
dates: [],
formatDate: function (date) {
var year = date.getFullYear() + '-';
var month = (date.getMonth() + 1) + '-';
var day = date.getDate();
return month + day;
},
addDate: function (date, n) {
date.setDate(date.getDate() + n);
return date;
},
setDate: function (date) {
dates = [];
var week = date.getDay() - 1;
date = this.addDate(date, week * -1);
currentFirstDate = new Date(date);
for (var i = 0; i < 7; i++) {
dates.push(this.formatDate(i == 0 ? date : this.addDate(date, 1)));
}
return dates;
}
}