-
Notifications
You must be signed in to change notification settings - Fork 11
/
formatTime.js
33 lines (33 loc) · 893 Bytes
/
formatTime.js
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
/*
* 函数功能:JS格式化时间
*/
var formatTime = function(time, format) {
var t = new Date(time);
//alert(t)
var tf = function(i) {
return (i < 10 ? '0' : '') + i
};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
switch (a) {
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
}
formatTime(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss')