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
|
function
formatDate(date,format) {
var
date =
new
Date(date);
var
year = date.getFullYear();
var
month = date.getMonth();
var
month1 = month + 1;
var
day = date.getDate();
var
hours = date.getHours();
var
minutes = date.getMinutes();
var
seconds = date.getSeconds();
return
format.replace(/yyyy/g, year)
.replace(/yy/g, (year +
''
).substring(2))
.replace(/mm/g, month1 < 10 ?
'0'
+ month1 : month1)
.replace(/m/g, month1)
.replace(/dd/g, day < 10 ?
'0'
+ day : day)
.replace(/d/g, day)
.replace(/hh/g,hours<10?
'0'
+hours:hours)
.replace(/h/g,hours)
.replace(/ii/g,minutes<10?
'0'
+minutes:minutes)
.replace(/i/g,minutes)
.replace(/ss/g,seconds<10?
'0'
+seconds:seconds)
.replace(/s/g,seconds);
}
/*
* 要兼容 yy-m-d h:i:s 这个因为 dateString 和 format 可能不一致处理起来比较麻烦 目前没有兼容
* */
function
parseDate(dateStr,format){
var
reg = /yyyy|mm|dd|hh|ii|ss|.+?/g;
// parse year
var
result =
null
;
var
d =
new
Date();
while
(result=reg.exec(format)){
var
res = result[0];
var
index = result.index;
var
lastIndex = reg.lastIndex;
if
(res==
"yyyy"
){
d.setFullYear(dateStr.substring(index,lastIndex));
}
else
if
(res==
"mm"
){
var
month = dateStr.substring(index,lastIndex);
month = parseInt(month);
month--;
d.setMonth(month);
}
else
if
(res==
"dd"
){
var
date = dateStr.substring(index,lastIndex);
d.setDate(date);
}
else
if
(res==
"hh"
){
var
hours = dateStr.substring(index,lastIndex);
d.setHours(hours);
}
else
if
(res==
"ii"
){
var
minutes = dateStr.substring(index,lastIndex);
d.setMinutes(minutes);
}
else
if
(res==
"ss"
){
var
seconds = dateStr.substring(index,lastIndex);
d.setSeconds(seconds);
}
}
return
d;
}
|
本文转自 antlove 51CTO博客,原文链接:http://blog.51cto.com/antlove/1929626