MCPcopy Create free account
hub / github.com/angular/angular / isoStringToDate

Function isoStringToDate

packages/common/src/i18n/format_date.ts:975–999  ·  view source on GitHub ↗
(match: RegExpMatchArray)

Source from the content-addressed store, hash-verified

973 * Used instead of `Date.parse` because of browser discrepancies.
974 */
975export function isoStringToDate(match: RegExpMatchArray): Date {
976 const date = new Date(0);
977 let tzHour = 0;
978 let tzMin = 0;
979
980 // match[8] means that the string contains "Z" (UTC) or a timezone like "+01:00" or "+0100"
981 const dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear;
982 const timeSetter = match[8] ? date.setUTCHours : date.setHours;
983
984 // if there is a timezone defined like "+01:00" or "+0100"
985 if (match[9]) {
986 tzHour = Number(match[9] + match[10]);
987 tzMin = Number(match[9] + match[11]);
988 }
989 dateSetter.call(date, Number(match[1]), Number(match[2]) - 1, Number(match[3]));
990 const h = Number(match[4] || 0) - tzHour;
991 const m = Number(match[5] || 0) - tzMin;
992 const s = Number(match[6] || 0);
993 // The ECMAScript specification (https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.11)
994 // defines that `DateTime` milliseconds should always be rounded down, so that `999.9ms`
995 // becomes `999ms`.
996 const ms = Math.floor(parseFloat('0.' + (match[7] || 0)) * 1000);
997 timeSetter.call(date, h, m, s, ms);
998 return date;
999}
1000
1001export function isDate(value: any): value is Date {
1002 return value instanceof Date && !isNaN(value.valueOf());

Callers 1

toDateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected