MCPcopy
hub / github.com/date-fns/date-fns / parse

Function parse

pkgs/core/src/parse/index.ts:361–532  ·  view source on GitHub ↗
(
  dateStr: string,
  formatStr: string,
  referenceDate: DateArg<DateType>,
  options?: ParseOptions<ResultDate>,
)

Source from the content-addressed store, hash-verified

359 * //=> Sun Feb 28 2010 00:00:00
360 */
361export function parse<
362 DateType extends Date,
363 ResultDate extends Date = DateType,
364>(
365 dateStr: string,
366 formatStr: string,
367 referenceDate: DateArg<DateType>,
368 options?: ParseOptions<ResultDate>,
369): ResultDate {
370 const invalidDate = () => constructFrom(options?.in || referenceDate, NaN);
371 const defaultOptions = getDefaultOptions();
372 const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale;
373
374 const firstWeekContainsDate =
375 options?.firstWeekContainsDate ??
376 options?.locale?.options?.firstWeekContainsDate ??
377 defaultOptions.firstWeekContainsDate ??
378 defaultOptions.locale?.options?.firstWeekContainsDate ??
379 1;
380
381 const weekStartsOn =
382 options?.weekStartsOn ??
383 options?.locale?.options?.weekStartsOn ??
384 defaultOptions.weekStartsOn ??
385 defaultOptions.locale?.options?.weekStartsOn ??
386 0;
387
388 if (!formatStr)
389 return dateStr ? invalidDate() : toDate(referenceDate, options?.in);
390
391 const subFnOptions: ParserOptions = {
392 firstWeekContainsDate,
393 weekStartsOn,
394 locale,
395 };
396
397 // If timezone isn't specified, it will try to use the context or
398 // the reference date and fallback to the system time zone.
399 const setters: Setter[] = [
400 new DateTimezoneSetter(options?.in, referenceDate),
401 ];
402
403 const tokens = formatStr
404 .match(longFormattingTokensRegExp)!
405 .map((substring) => {
406 const firstCharacter = substring[0];
407 if (firstCharacter in longFormatters) {
408 const longFormatter = longFormatters[firstCharacter];
409 return longFormatter(substring, locale.formatLong);
410 }
411 return substring;
412 })
413 .join("")
414 .match(formattingTokensRegExp)!;
415
416 const usedTokens: Array<{ token: string; fullToken: string }> = [];
417
418 for (let token of tokens) {

Callers 8

test.tsFile · 0.90
test.tsFile · 0.90
test.tsFile · 0.90
isMatchFunction · 0.90
test.tsFile · 0.90
blockFunction · 0.90
test.tsFile · 0.90
tests.tsFile · 0.85

Calls 9

getDefaultOptionsFunction · 0.90
toDateFunction · 0.90
isProtectedWeekYearTokenFunction · 0.90
invalidDateFunction · 0.70
cleanEscapedStringFunction · 0.70
validateMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected