@@ -22,10 +22,10 @@ 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 , today , year , date , days , ranges , len , times , regex , i ;
25
+ var parsed , match , today , year , date , days , ranges , len , times , regex , i , fail = false ;
26
26
27
27
if ( ! text ) {
28
- return null ;
28
+ return fail ;
29
29
}
30
30
31
31
// Unecessary spaces
@@ -41,96 +41,107 @@ function strtotime (text, now) {
41
41
// etc...etc...
42
42
// ...therefore we manually parse lots of common date formats
43
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
+
44
45
if ( match && match [ 2 ] === match [ 4 ] ) {
45
46
if ( match [ 1 ] > 1901 ) {
46
47
switch ( match [ 2 ] ) {
47
48
case '-' : { // YYYY-M-D
48
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) {
49
- return ( 0 ) ;
49
+ if ( match [ 3 ] > 12 || match [ 5 ] > 31 ) {
50
+ return fail ;
50
51
}
52
+
51
53
return new Date ( match [ 1 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
52
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
54
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
53
55
}
54
56
case '.' : { // YYYY.M.D is not parsed by strtotime()
55
- return ( 0 ) ;
57
+ return fail ;
56
58
}
57
59
case '/' : { // YYYY/M/D
58
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 ) {
59
- return ( 0 ) ;
60
+ if ( match [ 3 ] > 12 || match [ 5 ] > 31 ) {
61
+ return fail ;
60
62
}
63
+
61
64
return new Date ( match [ 1 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
62
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
65
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
63
66
}
64
67
}
65
- }
66
- else if ( match [ 5 ] > 1901 ) {
68
+ } else if ( match [ 5 ] > 1901 ) {
67
69
switch ( match [ 2 ] ) {
68
70
case '-' : { // D-M-YYYY
69
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
70
- return ( 0 ) ;
71
+ if ( match [ 3 ] > 12 || match [ 1 ] > 31 ) {
72
+ return fail ;
71
73
}
74
+
72
75
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
73
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
76
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
74
77
}
75
78
case '.' : { // D.M.YYYY
76
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
77
- return ( 0 ) ;
79
+ if ( match [ 3 ] > 12 || match [ 1 ] > 31 ) {
80
+ return fail ;
78
81
}
82
+
79
83
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
80
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
84
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
81
85
}
82
86
case '/' : { // M/D/YYYY
83
- if ( match [ 1 ] > 12 | match [ 3 ] > 31 ) {
84
- return ( 0 ) ;
87
+ if ( match [ 1 ] > 12 || match [ 3 ] > 31 ) {
88
+ return fail ;
85
89
}
90
+
86
91
return new Date ( match [ 5 ] , parseInt ( match [ 1 ] , 10 ) - 1 , match [ 3 ] ,
87
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
92
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
88
93
}
89
94
}
90
95
}
91
96
else {
92
97
switch ( match [ 2 ] ) {
93
98
case '-' : { // YY-M-D
94
- if ( match [ 3 ] > 12 | match [ 5 ] > 31 | ( match [ 1 ] < 70 & match [ 1 ] > 38 ) ) {
95
- return ( 0 ) ;
99
+ if ( match [ 3 ] > 12 || match [ 5 ] > 31 || ( match [ 1 ] < 70 && match [ 1 ] > 38 ) ) {
100
+ return fail ;
96
101
}
102
+
97
103
year = match [ 1 ] >= 0 && match [ 1 ] <= 38 ? + match [ 1 ] + 2000 : match [ 1 ] ;
98
104
return new Date ( year , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 5 ] ,
99
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
105
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
100
106
}
101
107
case '.' : { // D.M.YY or H.MM.SS
102
- if ( match [ 5 ] >= 70 ) { // D.M.YY
103
- if ( match [ 3 ] > 12 | match [ 1 ] > 31 ) {
104
- return ( 0 ) ;
108
+ if ( match [ 5 ] >= 70 ) { // D.M.YY
109
+ if ( match [ 3 ] > 12 || match [ 1 ] > 31 ) {
110
+ return fail ;
105
111
}
112
+
106
113
return new Date ( match [ 5 ] , parseInt ( match [ 3 ] , 10 ) - 1 , match [ 1 ] ,
107
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
114
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
108
115
}
109
- if ( match [ 5 ] < 60 & ! ( match [ 6 ] ) ) { // H.MM.SS
110
- if ( match [ 1 ] > 23 | match [ 3 ] > 59 ) {
111
- return ( 0 ) ;
116
+ if ( match [ 5 ] < 60 && ! match [ 6 ] ) { // H.MM.SS
117
+ if ( match [ 1 ] > 23 || match [ 3 ] > 59 ) {
118
+ return fail ;
112
119
}
120
+
113
121
today = new Date ( ) ;
114
122
return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) ,
115
- match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
123
+ match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
116
124
}
117
- return ( 0 ) ; // invalid format, cannot be parsed
125
+
126
+ return fail ; // invalid format, cannot be parsed
118
127
}
119
128
case '/' : { // M/D/YY
120
- if ( match [ 1 ] > 12 | match [ 3 ] > 31 | ( match [ 5 ] < 70 & match [ 5 ] > 38 ) ) {
121
- return ( 0 ) ;
129
+ if ( match [ 1 ] > 12 || match [ 3 ] > 31 || ( match [ 5 ] < 70 && match [ 5 ] > 38 ) ) {
130
+ return fail ;
122
131
}
132
+
123
133
year = match [ 5 ] >= 0 && match [ 5 ] <= 38 ? + match [ 5 ] + 2000 : match [ 5 ] ;
124
134
return new Date ( year , parseInt ( match [ 1 ] , 10 ) - 1 , match [ 3 ] ,
125
- match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
135
+ match [ 6 ] || 0 , match [ 7 ] || 0 , match [ 8 ] || 0 , match [ 9 ] || 0 ) / 1000 ;
126
136
}
127
137
case ':' : { // HH:MM:SS
128
- if ( match [ 1 ] > 23 | match [ 3 ] > 59 | match [ 5 ] > 59 ) {
129
- return ( 0 ) ;
138
+ if ( match [ 1 ] > 23 || match [ 3 ] > 59 || match [ 5 ] > 59 ) {
139
+ return fail ;
130
140
}
141
+
131
142
today = new Date ( ) ;
132
143
return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) ,
133
- match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 ) / 1000 ;
144
+ match [ 1 ] || 0 , match [ 3 ] || 0 , match [ 5 ] || 0 ) / 1000 ;
134
145
}
135
146
}
136
147
}
@@ -198,6 +209,7 @@ function strtotime (text, now) {
198
209
if ( ranges . hasOwnProperty ( range ) && ! splt [ 1 ] . match ( / ^ m o n ( d a y | \. ) ? $ / i) ) {
199
210
return date [ 'set' + ranges [ range ] ] ( date [ 'get' + ranges [ range ] ] ( ) + num ) ;
200
211
}
212
+
201
213
if ( range === 'wee' ) {
202
214
return date . setDate ( date . getDate ( ) + ( num * 7 ) ) ;
203
215
}
@@ -208,6 +220,7 @@ function strtotime (text, now) {
208
220
else if ( ! typeIsNumber ) {
209
221
return false ;
210
222
}
223
+
211
224
return true ;
212
225
}
213
226
@@ -218,12 +231,12 @@ function strtotime (text, now) {
218
231
219
232
match = text . match ( new RegExp ( regex , 'gi' ) ) ;
220
233
if ( ! match ) {
221
- return false ;
234
+ return fail ;
222
235
}
223
236
224
237
for ( i = 0 , len = match . length ; i < len ; i ++ ) {
225
238
if ( ! process ( match [ i ] ) ) {
226
- return false ;
239
+ return fail ;
227
240
}
228
241
}
229
242
0 commit comments