3
3
* @author 未知
4
4
*/
5
5
6
- Date . prototype . Format = function ( formatStr ) {
6
+ Date . prototype . format = function ( formatStr ) {
7
7
var str = formatStr ;
8
8
var Week = [ '日' , '一' , '二' , '三' , '四' , '五' , '六' ] ;
9
9
str = str . replace ( / y y y y | Y Y Y Y / , this . getFullYear ( ) ) ;
@@ -20,4 +20,25 @@ Date.prototype.Format = function(formatStr) {
20
20
str = str . replace ( / s s | S S / , this . getSeconds ( ) > 9 ? this . getSeconds ( ) . toString ( ) : '0' + this . getSeconds ( ) ) ;
21
21
str = str . replace ( / s | S / g, this . getSeconds ( ) ) ;
22
22
return str
23
- }
23
+ }
24
+
25
+ // 或
26
+ Date . prototype . format = function ( format ) {
27
+ var o = {
28
+ "M+" : this . getMonth ( ) + 1 , //month
29
+ "d+" : this . getDate ( ) , //day
30
+ "h+" : this . getHours ( ) , //hour
31
+ "m+" : this . getMinutes ( ) , //minute
32
+ "s+" : this . getSeconds ( ) , //second
33
+ "q+" : Math . floor ( ( this . getMonth ( ) + 3 ) / 3 ) , //quarter
34
+ "S" : this . getMilliseconds ( ) //millisecond
35
+ } ;
36
+ if ( / ( y + ) / . test ( format ) ) format = format . replace ( RegExp . $1 ,
37
+ ( this . getFullYear ( ) + "" ) . substr ( 4 - RegExp . $1 . length ) ) ;
38
+ for ( var k in o ) {
39
+ if ( new RegExp ( "(" + k + ")" ) . test ( format ) )
40
+ format = format . replace ( RegExp . $1 , RegExp . $1 . length == 1 ? o [ k ] :( "00" + o [ k ] ) . substr ( ( "" + o [ k ] ) . length ) ) ;
41
+ }
42
+ return format ;
43
+ }
44
+ alert ( new Date ( ) . format ( "yyyy-MM-dd hh:mm:ss" ) ) ;
0 commit comments