@@ -22,7 +22,7 @@ function strtotime (text, now) {
22
22
// * returns 3: 1127041200
23
23
// * example 4: strtotime('2009-05-04 08:30:00 GMT');
24
24
// * returns 4: 1241425800
25
- var parsed , match , year , date , days , ranges , len , times , regex , i ;
25
+ var parsed , match , today , year , date , days , ranges , len , times , regex , i ;
26
26
27
27
if ( ! text ) {
28
28
return null ;
@@ -40,87 +40,97 @@ function strtotime (text, now) {
40
40
// dates with two-digit years differently
41
41
// etc...etc...
42
42
// ...therefore we manually parse lots of common date formats
43
- var match = text . match ( / ^ ( \d { 1 , 4 } ) ( [ \- \. \/ \: ] ) ( \d { 1 , 2 } ) ( [ \- \. \/ \: ] ) ( \d { 1 , 4 } ) (?: \s ( \d { 1 , 2 } ) : ( \d { 2 } ) ? : ? ( \d { 2 } ) ? ) ? (?: \s ( [ A - Z ] + ) ? ) ? $ / ) ;
44
- if ( match && match [ 2 ] == match [ 4 ] ) {
45
- if ( match [ 1 ] > 1901 ) {
43
+ match = text . match ( / ^ ( \d { 1 , 4 } ) ( [ \- \. \/ \: ] ) ( \d { 1 , 2 } ) ( [ \- \. \/ \: ] ) ( \d { 1 , 4 } ) (?: \s ( \d { 1 , 2 } ) : ( \d { 2 } ) ? : ? ( \d { 2 } ) ? ) ? (?: \s ( [ A - Z ] + ) ? ) ? $ / ) ;
44
+ if ( match && match [ 2 ] === match [ 4 ] ) {
45
+ if ( match [ 1 ] > 1901 ) {
46
46
switch ( match [ 2 ] ) {
47
47
case '-' : { // YYYY-M-D
48
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) return ( 0 ) ;
48
+ if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) {
49
+ return ( 0 ) ;
50
+ }
49
51
return new Date ( match [ 1 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
50
52
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
51
- break ;
52
53
}
53
54
case '.' : { // YYYY.M.D is not parsed by strtotime()
54
55
return ( 0 ) ;
55
- break ;
56
56
}
57
57
case '/' : { // YYYY/M/D
58
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) return ( 0 ) ;
58
+ if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) {
59
+ return ( 0 ) ;
60
+ }
59
61
return new Date ( match [ 1 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
60
62
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
61
- break ;
62
63
}
63
64
}
64
65
}
65
66
else if ( match [ 5 ] > 1901 ) {
66
67
switch ( match [ 2 ] ) {
67
68
case '-' : { // D-M-YYYY
68
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) return ( 0 ) ;
69
+ if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
70
+ return ( 0 ) ;
71
+ }
69
72
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
70
73
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
71
- break ;
72
74
}
73
75
case '.' : { // D.M.YYYY
74
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) return ( 0 ) ;
76
+ if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
77
+ return ( 0 ) ;
78
+ }
75
79
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
76
80
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
77
- break ;
78
81
}
79
82
case '/' : { // M/D/YYYY
80
- if ( match [ 1 ] > 12 | match [ 3 ] > 31 ) return ( 0 ) ;
83
+ if ( match [ 1 ] > 12 | match [ 3 ] > 31 ) {
84
+ return ( 0 ) ;
85
+ }
81
86
return new Date ( match [ 5 ] , parseInt ( match [ 1 ] , 10 ) - 1 , match [ 3 ] ,
82
87
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
83
- break ;
84
88
}
85
89
}
86
90
}
87
91
else {
88
92
switch ( match [ 2 ] ) {
89
93
case '-' : { // YY-M-D
90
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 | ( match [ 1 ] < 70 & match [ 1 ] > 38 ) ) return ( 0 ) ;
91
- var year = match [ 1 ] >= 0 && match [ 1 ] <= 38 ? + match [ 1 ] + 2000 : match [ 1 ] ;
94
+ if ( match [ 3 ] > 12 | match [ 5 ] > 31 | ( match [ 1 ] < 70 & match [ 1 ] > 38 ) ) {
95
+ return ( 0 ) ;
96
+ }
97
+ year = match [ 1 ] >= 0 && match [ 1 ] <= 38 ? + match [ 1 ] + 2000 : match [ 1 ] ;
92
98
return new Date ( year , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
93
99
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
94
- break ;
95
100
}
96
101
case '.' : { // D.M.YY or H.MM.SS
97
102
if ( match [ 5 ] >= 70 ) { // D.M.YY
98
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) return ( 0 ) ;
103
+ if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
104
+ return ( 0 ) ;
105
+ }
99
106
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
100
107
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
101
108
}
102
- else if ( match [ 5 ] < 60 & ! ( match [ 6 ] ) ) { // H.MM.SS
103
- if ( match [ 1 ] > 23 | match [ 3 ] > 59 ) return ( 0 ) ;
104
- var today = new Date ( ) ;
109
+ if ( match [ 5 ] < 60 & ! ( match [ 6 ] ) ) { // H.MM.SS
110
+ if ( match [ 1 ] > 23 | match [ 3 ] > 59 ) {
111
+ return ( 0 ) ;
112
+ }
113
+ today = new Date ( ) ;
105
114
return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) ,
106
115
match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
107
116
}
108
- else return ( 0 ) ; // invalid format, cannot be parsed
109
- break ;
117
+ return ( 0 ) ; // invalid format, cannot be parsed
110
118
}
111
119
case '/' : { // M/D/YY
112
- if ( match [ 1 ] > 12 | match [ 3 ] > 31 | ( match [ 5 ] < 70 & match [ 5 ] > 38 ) ) return ( 0 ) ;
113
- var year = match [ 5 ] >= 0 && match [ 5 ] <= 38 ? + match [ 5 ] + 2000 : match [ 5 ] ;
120
+ if ( match [ 1 ] > 12 | match [ 3 ] > 31 | ( match [ 5 ] < 70 & match [ 5 ] > 38 ) ) {
121
+ return ( 0 ) ;
122
+ }
123
+ year = match [ 5 ] >= 0 && match [ 5 ] <= 38 ? + match [ 5 ] + 2000 : match [ 5 ] ;
114
124
return new Date ( year , parseInt ( match [ 1 ] , 10 ) - 1 , match [ 3 ] ,
115
125
match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
116
- break ;
117
126
}
118
127
case ':' : { // HH:MM:SS
119
- if ( match [ 1 ] > 23 | match [ 3 ] > 59 | match [ 5 ] > 59 ) return ( 0 ) ;
120
- var today = new Date ( ) ;
128
+ if ( match [ 1 ] > 23 | match [ 3 ] > 59 | match [ 5 ] > 59 ) {
129
+ return ( 0 ) ;
130
+ }
131
+ today = new Date ( ) ;
121
132
return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) ,
122
133
match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 ) / 1000 ;
123
- break ;
124
134
}
125
135
}
126
136
}
0 commit comments