(value: unknown)
| 500 | * @return date Never be null/undefined. If invalid, return `new Date(NaN)`. |
| 501 | */ |
| 502 | export function parseDate(value: unknown): Date { |
| 503 | if (value instanceof Date) { |
| 504 | return value; |
| 505 | } |
| 506 | else if (zrUtil.isString(value)) { |
| 507 | // Different browsers parse date in different way, so we parse it manually. |
| 508 | // Some other issues: |
| 509 | // new Date('1970-01-01') is UTC, |
| 510 | // new Date('1970/01/01') and new Date('1970-1-01') is local. |
| 511 | // See issue #3623 |
| 512 | const match = TIME_REG.exec(value); |
| 513 | |
| 514 | if (!match) { |
| 515 | // return Invalid Date. |
| 516 | return new Date(NaN); |
| 517 | } |
| 518 | |
| 519 | // Use local time when no timezone offset is specified. |
| 520 | if (!match[8]) { |
| 521 | // match[n] can only be string or undefined. |
| 522 | // But take care of '12' + 1 => '121'. |
| 523 | return new Date( |
| 524 | +match[1], |
| 525 | +(match[2] || 1) - 1, |
| 526 | +match[3] || 1, |
| 527 | +match[4] || 0, |
| 528 | +(match[5] || 0), |
| 529 | +match[6] || 0, |
| 530 | match[7] ? +match[7].substring(0, 3) : 0 |
| 531 | ); |
| 532 | } |
| 533 | // Timezoneoffset of Javascript Date has considered DST (Daylight Saving Time, |
| 534 | // https://tc39.github.io/ecma262/#sec-daylight-saving-time-adjustment). |
| 535 | // For example, system timezone is set as "Time Zone: America/Toronto", |
| 536 | // then these code will get different result: |
| 537 | // `new Date(1478411999999).getTimezoneOffset(); // get 240` |
| 538 | // `new Date(1478412000000).getTimezoneOffset(); // get 300` |
| 539 | // So we should not use `new Date`, but use `Date.UTC`. |
| 540 | else { |
| 541 | let hour = +match[4] || 0; |
| 542 | if (match[8].toUpperCase() !== 'Z') { |
| 543 | hour -= +match[8].slice(0, 3); |
| 544 | } |
| 545 | return new Date(Date.UTC( |
| 546 | +match[1], |
| 547 | +(match[2] || 1) - 1, |
| 548 | +match[3] || 1, |
| 549 | hour, |
| 550 | +(match[5] || 0), |
| 551 | +match[6] || 0, |
| 552 | match[7] ? +match[7].substring(0, 3) : 0 |
| 553 | )); |
| 554 | } |
| 555 | } |
| 556 | else if (value == null) { |
| 557 | return new Date(NaN); |
| 558 | } |
| 559 |
no outgoing calls
no test coverage detected