|
201 | 201 | }, |
202 | 202 |
|
203 | 203 | fill: function() { |
204 | | - var d = new Date(this.viewDate), |
205 | | - year = d.getFullYear(), |
206 | | - month = d.getMonth(), |
207 | | - currentDate = this.date.valueOf(); |
| 204 | + var year = this.viewDate.getFullYear(); |
| 205 | + var month = this.viewDate.getMonth(); |
| 206 | + var currentDate = new Date( |
| 207 | + this.date.getFullYear(), this.date.getMonth(), this.date.getDate(), |
| 208 | + 0, 0, 0, 0 |
| 209 | + ); |
208 | 210 | this.picker.find('.datepicker-days th:eq(1)') |
209 | 211 | .text(dates[this.language].months[month] + ' ' + year); |
210 | | - var prevMonth = new Date(year, month-1, 28,0,0,0,0), |
211 | | - day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth()); |
| 212 | + var prevMonth = new Date(year, month-1, 28,0,0,0,0); |
| 213 | + var day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth()); |
212 | 214 | prevMonth.setDate(day); |
213 | 215 | prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7); |
214 | 216 | var nextMonth = new Date(prevMonth); |
|
226 | 228 | } else if (prevMonth.getMonth() > month) { |
227 | 229 | clsName += ' new'; |
228 | 230 | } |
229 | | - if (prevMonth.valueOf() === currentDate) { |
| 231 | + if (prevMonth.valueOf() === currentDate.valueOf()) { |
230 | 232 | clsName += ' active'; |
231 | 233 | } |
232 | 234 | html.push('<td class="day'+clsName+'">'+prevMonth.getDate() + '</td>'); |
|
294 | 296 | this.viewDate.setFullYear(year); |
295 | 297 | } |
296 | 298 | if (this.viewMode !== 0) { |
297 | | - this.date.setFullYear(this.viewDate.getFullYear()); |
298 | | - this.date.setMonth(this.viewDate.getMonth()); |
299 | | - this.date.setDate(this.viewDate.getDate()); |
| 299 | + this.date = new Date( |
| 300 | + this.viewDate.getFullYear(), this.viewDate.getMonth(), this.viewDate.getDate(), |
| 301 | + this.date.getHours(), this.date.getMinutes(), this.date.getSeconds(), |
| 302 | + this.date.getMilliseconds() |
| 303 | + ); |
300 | 304 | this.$element.trigger({ |
301 | 305 | type: 'changeDate', |
302 | 306 | date: this.date, |
|
317 | 321 | month += 1; |
318 | 322 | } |
319 | 323 | var year = this.viewDate.getFullYear(); |
320 | | - this.date.setFullYear(year); |
321 | | - this.date.setMonth(month); |
322 | | - this.date.setDate(day); |
| 324 | + this.date = new Date( |
| 325 | + year, month, day, |
| 326 | + this.date.getHours(), this.date.getMinutes(), this.date.getSeconds(), |
| 327 | + this.date.getMilliseconds() |
| 328 | + ); |
323 | 329 | this.viewDate = new Date(year, month, Math.min(28, day),0,0,0,0); |
324 | 330 | this.fill(); |
325 | 331 | this.set(); |
|
335 | 341 | }, |
336 | 342 |
|
337 | 343 | keypress: function(e) { |
| 344 | + // TODO: Mask the input so the user can only type |
| 345 | + // according to the specified format |
338 | 346 | }, |
339 | 347 |
|
340 | 348 | change: function(e) { |
|
345 | 353 | date: this.date, |
346 | 354 | viewMode: DPGlobal.modes[this.viewMode].clsName |
347 | 355 | }); |
348 | | - } else { |
349 | 356 | this.set(); |
350 | 357 | } |
351 | 358 | }, |
|
375 | 382 | var methodName, property, rv; |
376 | 383 | property = dateFormatComponents[match].property |
377 | 384 | methodName = 'get' + property; |
378 | | - rv = (dateMethods[methodName] || Date.prototype[methodName]).call(d); |
| 385 | + rv = d[methodName](); |
| 386 | + if (methodName === 'getMonth') rv = rv + 1; |
| 387 | + if (methodName === 'getYear') rv = rv + 1900 - 2000; |
379 | 388 | return padLeft(rv.toString(), match.length, '0'); |
380 | 389 | }); |
381 | 390 | }, |
382 | 391 |
|
383 | 392 | parseDate: function(str) { |
384 | | - var match, i, property, methodName, value, rv = new Date(0); |
| 393 | + var match, i, property, methodName, value, parsed = {}; |
385 | 394 | if (!(match = this._formatPattern.exec(str))) |
386 | 395 | return null; |
387 | 396 | for (i = 1; i < match.length; i++) { |
388 | 397 | property = this._propertiesByIndex[i]; |
389 | 398 | if (!property) |
390 | 399 | continue; |
391 | | - methodName = 'set' + property; |
392 | 400 | value = match[i]; |
393 | 401 | if (/^\d+$/.test(value)) |
394 | 402 | value = parseInt(value, 10); |
395 | | - (dateMethods[methodName] || Date.prototype[methodName]).call(rv, value); |
| 403 | + parsed[property] = value; |
396 | 404 | } |
397 | | - return rv; |
| 405 | + return this._finishParsingDate(parsed); |
| 406 | + }, |
| 407 | + |
| 408 | + _finishParsingDate: function(parsed) { |
| 409 | + var year, month, date, hours, minutes, seconds; |
| 410 | + year = parsed.FullYear; |
| 411 | + if (parsed.Year) year = 2000 + parsed.Year; |
| 412 | + if (!year) year = 1970; |
| 413 | + if (parsed.Month) month = parsed.Month - 1; |
| 414 | + else month = 0; |
| 415 | + date = parsed.Date || 1; |
| 416 | + hours = parsed.Hours || 0; |
| 417 | + minutes = parsed.Minutes || 0; |
| 418 | + seconds = parsed.Seconds || 0; |
| 419 | + return new Date(year, month, date, hours, minutes, seconds, 0); |
398 | 420 | }, |
399 | 421 |
|
400 | 422 | _compileFormat: function () { |
|
531 | 553 | var dateFormatComponents = { |
532 | 554 | dd: {property: 'Date', getPattern: function() { return '(0?[1-9]|[1-2][0-9]|3[0-1])\\b';}}, |
533 | 555 | MM: {property: 'Month', getPattern: function() {return '(0?[1-9]|1[0-2])\\b';}}, |
534 | | - month: {property: 'MonthByName', getPattern: function(picker) { |
535 | | - var rv = [], values = dates[picker.language].months; |
536 | | - for (var i = 0; i < values.length; i++) |
537 | | - rv.push(escapeRegExp(values[i])); |
538 | | - return '(' + rv.join('|') + ')'; |
539 | | - }}, |
540 | | - monthShort: {property: 'MonthByShortName', getPattern: function(picker) { |
541 | | - var rv = [], values = dates[picker.language].monthsShort; |
542 | | - for (var i = 0; i < values.length; i++) |
543 | | - rv.push(escapeRegExp(values[i])); |
544 | | - return '(' + rv.join('|') + ')'; |
545 | | - }}, |
546 | | - yyyy: {property: 'FullYear', getPattern: function() {return '(\\d{2}|\\d{4})\\b';}}, |
| 556 | + yy: {property: 'Year', getPattern: function() {return '(\\d{2})\\b'}}, |
| 557 | + yyyy: {property: 'FullYear', getPattern: function() {return '(\\d{4})\\b';}}, |
547 | 558 | hh: {property: 'Hours', getPattern: function() {return '(0?[0-9]|1[0-9]|2[0-3])\\b';}}, |
548 | 559 | mm: {property: 'Minutes', getPattern: function() {return '(0?[0-9]|[1-5][0-9])\\b';}}, |
549 | 560 | ss: {property: 'Seconds', getPattern: function() {return '(0?[0-9]|[1-5][0-9])\\b';}}, |
550 | 561 | ms: {property: 'Milliseconds', getPattern: function() {return '([0-9]{1,3})\\b';}}, |
551 | 562 | }; |
552 | 563 |
|
553 | | - var dateMethods = { |
554 | | - getMonth: function() { |
555 | | - return this.getMonth() + 1; |
556 | | - }, |
557 | | - setMonth: function(m) { |
558 | | - return this.setMonth(m - 1); |
559 | | - } |
560 | | - }; |
561 | | - |
562 | | - var tmp = []; |
563 | | - for (var k in dateFormatComponents) tmp.push(k); |
564 | | - tmp.push('.'); |
| 564 | + var keys = []; |
| 565 | + for (var k in dateFormatComponents) keys.push(k); |
| 566 | + keys[keys.length - 1] += '\\b'; |
| 567 | + keys.push('.'); |
565 | 568 |
|
566 | | - var formatComponent = new RegExp(tmp.join('|')); |
567 | | - tmp.pop(); |
568 | | - var formatReplacer = new RegExp(tmp.join('|'), 'g'); |
| 569 | + var formatComponent = new RegExp(keys.join('\\b|')); |
| 570 | + keys.pop(); |
| 571 | + var formatReplacer = new RegExp(keys.join('\\b|'), 'g'); |
569 | 572 |
|
570 | 573 | function escapeRegExp(str) { |
571 | 574 | // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex |
|
0 commit comments