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

Function format

pkgs/core/src/format/index.ts:346–442  ·  view source on GitHub ↗
(
  date: DateArg<Date> & {},
  formatStr: string,
  options?: FormatOptions,
)

Source from the content-addressed store, hash-verified

344 * //=> "3 o'clock"
345 */
346export function format(
347 date: DateArg<Date> & {},
348 formatStr: string,
349 options?: FormatOptions,
350): string {
351 const defaultOptions = getDefaultOptions();
352 const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale;
353
354 const firstWeekContainsDate =
355 options?.firstWeekContainsDate ??
356 options?.locale?.options?.firstWeekContainsDate ??
357 defaultOptions.firstWeekContainsDate ??
358 defaultOptions.locale?.options?.firstWeekContainsDate ??
359 1;
360
361 const weekStartsOn =
362 options?.weekStartsOn ??
363 options?.locale?.options?.weekStartsOn ??
364 defaultOptions.weekStartsOn ??
365 defaultOptions.locale?.options?.weekStartsOn ??
366 0;
367
368 const originalDate = toDate(date, options?.in);
369
370 if (!isValid(originalDate)) {
371 throw new RangeError("Invalid time value");
372 }
373
374 let parts: FormatPart[] = formatStr
375 .match(longFormattingTokensRegExp)!
376 .map((substring) => {
377 const firstCharacter = substring[0];
378 if (firstCharacter === "p" || firstCharacter === "P") {
379 const longFormatter = longFormatters[firstCharacter];
380 return longFormatter(substring, locale.formatLong);
381 }
382 return substring;
383 })
384 .join("")
385 .match(formattingTokensRegExp)!
386 .map((substring) => {
387 // Replace two single quote characters with one single quote character
388 if (substring === "''") {
389 return { isToken: false, value: "'" };
390 }
391
392 const firstCharacter = substring[0];
393 if (firstCharacter === "'") {
394 return { isToken: false, value: cleanEscapedString(substring) };
395 }
396
397 if (formatters[firstCharacter]) {
398 return { isToken: true, value: substring };
399 }
400
401 if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
402 throw new RangeError(
403 "Format string contains an unescaped latin alphabet character `" +

Callers 15

test.tsFile · 0.90
test.tsFile · 0.90
test.tsFile · 0.90
formatRelativeFunction · 0.90
test.tsFile · 0.90
renderChangelogFunction · 0.90
misc.tsFile · 0.90
formatRelativeFunction · 0.50
formatRelativeFunction · 0.50
formatRelativeFunction · 0.50
formatRelativeFunction · 0.50
formatRelativeFunction · 0.50

Calls 7

getDefaultOptionsFunction · 0.90
toDateFunction · 0.90
isValidFunction · 0.90
isProtectedWeekYearTokenFunction · 0.90
cleanEscapedStringFunction · 0.70

Tested by

no test coverage detected