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

Function getDateTranslation

packages/common/src/i18n/format_date.ts:388–458  ·  view source on GitHub ↗

* Returns the locale translation of a date for a given form, type and width

(
  date: Date,
  locale: string,
  name: TranslationType,
  width: TranslationWidth,
  form: FormStyle,
  extended: boolean,
)

Source from the content-addressed store, hash-verified

386 * Returns the locale translation of a date for a given form, type and width
387 */
388function getDateTranslation(
389 date: Date,
390 locale: string,
391 name: TranslationType,
392 width: TranslationWidth,
393 form: FormStyle,
394 extended: boolean,
395) {
396 switch (name) {
397 case TranslationType.Months:
398 return getLocaleMonthNames(locale, form, width)[date.getMonth()];
399 case TranslationType.Days:
400 return getLocaleDayNames(locale, form, width)[date.getDay()];
401 case TranslationType.DayPeriods:
402 const currentHours = date.getHours();
403 const currentMinutes = date.getMinutes();
404 if (extended) {
405 const rules = getLocaleExtraDayPeriodRules(locale);
406 const dayPeriods = getLocaleExtraDayPeriods(locale, form, width);
407 const index = rules.findIndex((rule) => {
408 if (Array.isArray(rule)) {
409 // morning, afternoon, evening, night
410 const [from, to] = rule;
411 const afterFrom = currentHours >= from.hours && currentMinutes >= from.minutes;
412 const beforeTo =
413 currentHours < to.hours || (currentHours === to.hours && currentMinutes < to.minutes);
414 // We must account for normal rules that span a period during the day (e.g. 6am-9am)
415 // where `from` is less (earlier) than `to`. But also rules that span midnight (e.g.
416 // 10pm - 5am) where `from` is greater (later!) than `to`.
417 //
418 // In the first case the current time must be BOTH after `from` AND before `to`
419 // (e.g. 8am is after 6am AND before 10am).
420 //
421 // In the second case the current time must be EITHER after `from` OR before `to`
422 // (e.g. 4am is before 5am but not after 10pm; and 11pm is not before 5am but it is
423 // after 10pm).
424 if (from.hours < to.hours) {
425 if (afterFrom && beforeTo) {
426 return true;
427 }
428 } else if (afterFrom || beforeTo) {
429 return true;
430 }
431 } else {
432 // noon or midnight
433 if (rule.hours === currentHours && rule.minutes === currentMinutes) {
434 return true;
435 }
436 }
437 return false;
438 });
439 if (index !== -1) {
440 return dayPeriods[index];
441 }
442 }
443 // if no rules for the day periods, we use am/pm by default
444 return getLocaleDayPeriods(locale, form, <TranslationWidth>width)[currentHours < 12 ? 0 : 1];
445 case TranslationType.Eras:

Callers 1

dateStrGetterFunction · 0.85

Calls 7

getLocaleMonthNamesFunction · 0.90
getLocaleDayNamesFunction · 0.90
getLocaleExtraDayPeriodsFunction · 0.90
getLocaleDayPeriodsFunction · 0.90
getLocaleEraNamesFunction · 0.90
isArrayMethod · 0.80

Tested by

no test coverage detected