DateFormatter is a unified system for formatting dates and times. It keeps the presentation consistent across the whole application and follows the corporate standards.

DateFormatter tracks locale changes through KbqLocaleService on its own and updates the formats when the interface language changes.

DateFormatter methods format a date or a time directly in TypeScript code:

const formattedStringOfDate = this.formatter.absoluteLongDate(this.adapter.today());

Formatting in HTML templates is done with pipes whose names correspond to the DateFormatter methods:

<div>{{ adapter.today() | kbqAbsoluteLongDate }}</div>

The pipes need KbqFormattersModule to be imported — it provides DateFormatter and exports every pipe. Usage examples

The same format is available in three flavours:

FamilyExampleBehaviour
kbq* — recommendedkbqAbsoluteLongDateRecomputes on a KbqLocaleService locale change, caches the result by value, arguments and locale
No prefixabsoluteLongDateA pure pipe. Recomputes only when the input value changes — the string goes stale on a locale change
ImpurePipe suffixabsoluteLongDateImpurePipeAn impure pipe with no cache: reformats on every change detection cycle

The unprefixed and ImpurePipe flavours are deprecated and will be removed in 22.0.0: each one maps to the kbq* pipe of the same format, with the same arguments.

PipeInputArgumentsDateFormatter method
kbqAbsoluteShortDatedatecurrYear?: booleanabsoluteShortDate
kbqAbsoluteLongDatedatecurrYear?: booleanabsoluteLongDate
kbqAbsoluteShortDateTimedateoptions?: DateTimeOptionsabsoluteShortDateTime
kbqAbsoluteLongDateTimedateoptions?: DateTimeOptionsabsoluteLongDateTime
kbqRelativeShortDatedateoptions?: Pick<DateTimeOptions, 'sentenceStart'>relativeShortDate
kbqRelativeLongDatedateoptions?: Pick<DateTimeOptions, 'sentenceStart'>relativeLongDate
kbqRelativeShortDateTimedateoptions?: DateTimeOptionsrelativeShortDateTime
kbqRelativeLongDateTimedateoptions?: DateTimeOptionsrelativeLongDateTime
kbqRangeShortDate[from, to]—rangeShortDate
kbqRangeLongDate[from, to]—rangeLongDate
kbqRangeShortDateTime[from, to]options?: DateTimeOptionsrangeShortDateTime
kbqRangeMiddleDateTime[from, to]options?: DateTimeOptionsrangeMiddleDateTime
kbqRangeLongDateTime[from, to]options?: DateTimeOptionsrangeLongDateTime
kbqDurationShortest[from, to]options?: DateTimeOptionsdurationShortest
kbqDurationShort[from, to]units?: DurationUnit[], fraction?: booleandurationShort
kbqDurationLong[from, to]units?: DurationUnit[], fraction?: booleandurationLong

DateTimeOptions is { seconds?: boolean; milliseconds?: boolean; currYear?: boolean; sentenceStart?: boolean }; sentenceStart is read by the relative formats only. kbqDurationShortest uses only seconds (true by default) and milliseconds from it.

<div>{{ [task.startedAt, task.finishedAt] | kbqDurationShortest }}</div>
<div>{{ [task.startedAt, task.finishedAt] | kbqDurationLong: ['hours', 'minutes'] }}</div>

Pass null instead of one of the bounds and the range pipe switches to the opened-range format ("From January 15", "Until June 20") on its own. No separate pipe is needed for that.

<div>{{ [filter.from, filter.to] | kbqRangeLongDate }}</div>

kbqRangeMiddleDateTime is the exception: the middle format has no opened-range template, so it requires both bounds.

When a date is missing or cannot be parsed, the pipe renders an empty string. The input types allow this: a binding that has not been populated yet is null or undefined, not a date, and for the range and duration pipes that applies to the whole [from, to] tuple as well as to a bound inside it.

The range pipes that support an opened range — kbqRangeShortDate, kbqRangeLongDate, kbqRangeShortDateTime, kbqRangeLongDateTime — render an empty string only when both bounds are missing or invalid; a single valid bound switches them to the opened-range format instead. kbqRangeMiddleDateTime and the duration pipes need both bounds, so they render an empty string as soon as either one is missing or invalid; the duration pipes additionally render an empty string when the start is later than the end.

Call the DateFormatter methods directly if you need to be told about the error instead of hiding it — they throw.

Formats that have no pipe are available through DateFormatter: its public config field holds the templates of the active locale.

private readonly formatter = inject<DateFormatter<DateTime>>(DateFormatter);

format(from: DateTime, to: DateTime): string {
    return this.formatter.rangeDate(from, to, this.formatter.config.rangeTemplates.closedRange.middle);
}

absoluteDate, relativeDate, rangeDateTime, duration and openedRangeDate work the same way — they take a template as an argument.

By default dates are rendered in the time zone of the host the code runs on: the user's zone in the browser, the server's zone during server-side rendering. The KBQ_DATE_TIMEZONE token sets the zone once for the whole application — the pipes, DateFormatter, the calendar and the date inputs all switch to it, so there is no offset to pass to every pipe.

bootstrapApplication(App, {
    providers: [kbqDateTimezoneProvider('Europe/Moscow')]
});

The token accepts:

ValueExampleNotes
IANA zone name'Europe/Moscow'Follows the DST rules of that zone
Offset in minutes from UTC180, -330Fixed, with no DST
Offset as a string'+03:00', 'UTC+3', 'GMT+05:30'The same, written out
'utc'
'system'The default — the zone of the host

An unknown zone name does not break rendering: dates stay in the host zone and a warning is logged in development mode. The same applies to an offset outside ±14:00, the widest any zone has ever used; a fractional offset is rounded to whole minutes.

KbqDateTimezoneService.setTimezone() changes the zone without a reload, and the kbq* pipes re-render:

private readonly timezoneService = inject(KbqDateTimezoneService);

setUserTimezone(timezone: string) {
    this.timezoneService.setTimezone(timezone);
}

Pure pipes (the ones without the kbq prefix) do not react to a zone change, exactly as they do not react to a locale change.

The zone reaches the pipes through the date adapter and DateFormatter, and those read KbqDateTimezoneService from the injector that created them. kbqDateTimezoneProvider provides that service alongside the token, so scoping a zone to a subtree means listing it with the adapter and the formatter:

@Component({
    providers: [
        kbqDateTimezoneProvider('Asia/Tokyo'),
        { provide: DateAdapter, useClass: LuxonDateAdapter },
        DateFormatter
    ]
})

With SSR the page is rendered in the server zone and re-rendered in the browser zone after hydration. When the two differ, every date on the page changes at once, which reads as flickering. To avoid it, the server and the client have to receive the same KBQ_DATE_TIMEZONE value.

The most reliable way to pass it is TransferState: the server resolves the zone from a cookie (or from the user profile) and puts it into the state, and the client reads the resolved value — so the two match even when there is no cookie yet.

// server config
export const serverConfig = mergeApplicationConfig(appConfig, {
    providers: [
        provideServerRendering(),
        {
            provide: KBQ_DATE_TIMEZONE,
            useFactory: () => {
                const timezone = readTimezoneCookie(inject(REQUEST, { optional: true })) ?? 'utc';

                inject(TransferState).set(TIMEZONE_KEY, timezone);

                return timezone;
            }
        }
    ]
});

// browser config — take exactly what the server rendered with
export const appConfig: ApplicationConfig = {
    providers: [
        {
            provide: KBQ_DATE_TIMEZONE,
            useFactory: () => inject(TransferState).get(TIMEZONE_KEY, 'utc')
        }
    ]
};

On the very first visit there is no cookie yet, and the page renders with the fallback ('utc' in the example). After hydration, compare it with Intl.DateTimeFormat().resolvedOptions().timeZone, write the cookie and, if the zone has to apply right away, call setTimezone() — that is a single re-render, and only on the first visit. When the zone is stored in the user profile on the server, even the first visit avoids it.

Long format

Name
Value
absoluteLongDate (current year)
December 3
absoluteLongDate (not current year)
December 3, 2025
absoluteLongDateTime (current year)
December 3, 15:10
absoluteLongDateTime (current year) (with seconds)
December 3, 15:10:12
absoluteLongDateTime (current year) (with milliseconds)
December 3, 15:10:12.033
absoluteLongDateTime (not current year)
December 3, 2025, 15:10
absoluteLongDateTime (not current year) (with seconds)
December 3, 2025, 15:10:12
absoluteLongDateTime (not current year) (with milliseconds)
December 3, 2025, 15:10:12.033

Short format

Name
Value
absoluteShortDate (current year)
Dec 3
absoluteShortDate (not current year)
Dec 3, 2025
absoluteShortDateTime (current year)
Dec 3, 15:10
absoluteShortDateTime (current year) (with seconds)
Dec 3, 15:10:12
absoluteShortDateTime (current year) (with milliseconds)
Dec 3, 15:10:12.035
absoluteShortDateTime (not current year)
Dec 3, 2025, 15:10
absoluteShortDateTime (not current year) (with seconds)
Dec 3, 2025, 15:10:12
absoluteShortDateTime (not current year) (with milliseconds)
Dec 3, 2025, 15:10:12.035

Long format

Name
Value
Before yesterday (not current year)
December 1, 2025
Before yesterday (current year)
December 1, 15:10
Before yesterday (current year) (with seconds)
December 1, 15:10:12
Before yesterday (current year) (with milliseconds)
December 1, 15:10:12.037
Yesterday
Yesterday, 15:10
Yesterday (with seconds)
Yesterday, 15:10:12
Yesterday (with milliseconds)
Yesterday, 15:10:12.037
Today
Today, 14:10
Today (with seconds)
Today, 14:10:12
Today (with milliseconds)
Today, 14:10:12.037
Tomorrow
Tomorrow, 16:10
Tomorrow (with seconds)
Tomorrow, 16:10:12
Tomorrow (with milliseconds)
Tomorrow, 16:10:12.037
After tomorrow (current year)
December 5, 15:10
After tomorrow (current year) (with seconds)
December 5, 15:10:12
After tomorrow (current year) (with milliseconds)
December 5, 15:10:12.037
After tomorrow (not current year)
December 5, 2027

Short format

Name
Value
Before yesterday (not current year)
Dec 1, 2025
Before yesterday (current year)
Dec 1, 15:10
Before yesterday (current year) (with seconds)
Dec 1, 15:10:12
Before yesterday (current year) (with milliseconds)
Dec 1, 15:10:12.045
Yesterday
Yesterday, 15:10
Yesterday (with seconds)
Yesterday, 15:10:12
Yesterday (with milliseconds)
Yesterday, 15:10:12.045
Today
Today, 14:10
Today (with seconds)
Today, 14:10:12
Today (with milliseconds)
Today, 14:10:12.045
Tomorrow
Tomorrow, 16:10
Tomorrow (with seconds)
Tomorrow, 16:10:12
Tomorrow (with milliseconds)
Tomorrow, 16:10:12.045
After tomorrow (current year)
Dec 5, 15:10
After tomorrow (current year) (with seconds)
Dec 5, 15:10:12
After tomorrow (current year) (with milliseconds)
Dec 5, 15:10:12.045
After tomorrow (not current year)
Dec 5, 2027

Sentence case (sentenceStart: false)

Name
Value
Yesterday (long)
Updated yesterday, 15:10
Today (long)
Updated today, 15:10
Tomorrow (long)
Updated tomorrow, 15:10
Yesterday (short)
Updated yesterday, 15:10
Today (short)
Updated today, 15:10
Tomorrow (short)
Updated tomorrow, 15:10

By default, a relative date starts with a capital letter — it can open a sentence. When the date appears mid-sentence, pass { sentenceStart: false } to the pipe or the formatter to lowercase the relative word ("Updated yesterday, 14:48"). The absolute part of the same format is not affected.

Closed range

Long format

Name
Value
rangeLongDate (current month)
December 1–10
rangeLongDate (not current month)
January 1 – February 10
rangeLongDate (start date is not in current year)
January 1, 2025 – February 10, 2026
rangeLongDate (end date is not in current year)
January 1, 2026 – February 10, 2027
rangeLongDateTime (the same day, current year)
December 10, from 10:14 to 11:28
rangeLongDateTime (the same day, current year) (with seconds)
December 10, from 10:14:12 to 11:28:12
rangeLongDateTime (the same day, current year) (with milliseconds)
December 10, from 10:14:12.054 to 11:28:12.054
rangeLongDateTime (the same day, not current year)
January 11, 2025, from 10:14 to 11:28
rangeLongDateTime (the same day, not current year) (with seconds)
January 11, 2025, from 10:14:12 to 11:28:12
rangeLongDateTime (the same day, not current year) (with milliseconds)
January 11, 2025, from 10:14:12.054 to 11:28:12.054
rangeLongDateTime (not current month)
From January 1, 10:14 to February 1, 11:28
rangeLongDateTime (not current month) (with seconds)
From January 1, 10:14:12 to February 1, 11:28:12
rangeLongDateTime (not current month) (with milliseconds)
From January 1, 10:14:12.054 to February 1, 11:28:12.054
rangeLongDateTime (start date is not in current year)
From January 1, 2025, 10:14 to February 1, 2026, 11:28
rangeLongDateTime (start date is not in current year) (with seconds)
From January 1, 2025, 10:14:12 to February 1, 2026, 11:28:12
rangeLongDateTime (start date is not in current year) (with milliseconds)
From January 1, 2025, 10:14:12.054 to February 1, 2026, 11:28:12.054
rangeLongDateTime (end date is not in current year)
From January 1, 2026, 10:14 to February 1, 2025, 11:28
rangeLongDateTime (end date is not in current year) (with seconds)
From January 1, 2026, 10:14:12 to February 1, 2025, 11:28:12
rangeLongDateTime (end date is not in current year) (with milliseconds)
From January 1, 2026, 10:14:12.054 to February 1, 2025, 11:28:12.054

Middle format

Name
Value
rangeMiddleDateTime
December 1, 15:10 – December 10, 15:10
rangeMiddleDateTime (with seconds)
December 1, 15:10:12 – December 10, 15:10:12
rangeMiddleDateTime (with milliseconds)
December 1, 15:10:12.064 – December 10, 15:10:12.064
rangeMiddleDateTime (the same day)
10:14–10:28, December 10
rangeMiddleDateTime (the same day) (with seconds)
10:14:12–10:28:12, December 10
rangeMiddleDateTime (the same day) (with milliseconds)
10:14:12.064–10:28:12.064, December 10
rangeMiddleDateTime (the same day, not current year)
10:14–11:28, January 11, 2025
rangeMiddleDateTime (the same day, not current year) (with seconds)
10:14:12–11:28:12, January 11, 2025
rangeMiddleDateTime (the same day, not current year) (with milliseconds)
10:14:12.064–11:28:12.064, January 11, 2025
rangeMiddleDateTime (not current month)
January 1, 10:14 – February 1, 11:28
rangeMiddleDateTime (not current month) (with seconds)
January 1, 10:14:12 – February 1, 11:28:12
rangeMiddleDateTime (not current month) (with milliseconds)
January 1, 10:14:12.064 – February 1, 11:28:12.064
rangeMiddleDateTime (start date is not in current year)
January 1, 2025, 10:14 – January 1, 2026, 11:28
rangeMiddleDateTime (start date is not in current year) (with seconds)
January 1, 2025, 10:14:12 – January 1, 2026, 11:28:12
rangeMiddleDateTime (start date is not in current year) (with milliseconds)
January 1, 2025, 10:14:12.064 – January 1, 2026, 11:28:12.064
rangeMiddleDateTime (end date is not in current year)
January 1, 2026, 10:14 – January 1, 2027, 11:28
rangeMiddleDateTime (end date is not in current year) (with seconds)
January 1, 2026, 10:14:12 – January 1, 2027, 11:28:12
rangeMiddleDateTime (end date is not in current year) (with milliseconds)
January 1, 2026, 10:14:12.064 – January 1, 2027, 11:28:12.064

Short format

Name
Value
rangeShortDate (current month)
Dec 1–10
rangeShortDate (not current month)
Jan 1 – Feb 10
rangeShortDate (start date is not in current year)
Jan 1, 2025 – Feb 10, 2026
rangeShortDate (end date is not in current year)
Jan 1, 2026 – Feb 10, 2027
rangeShortDateTime (the same day, current year)
10:14–11:28, Dec 10
rangeShortDateTime (the same day, current year) (with seconds)
10:14:12–11:28:12, Dec 10
rangeShortDateTime (the same day, current year) (with milliseconds)
10:14:12.093–11:28:12.093, Dec 10
rangeShortDateTime (the same day, not current year)
10:14–11:28, Jan 11, 2025
rangeShortDateTime (the same day, not current year) (with seconds)
10:14:12–11:28:12, Jan 11, 2025
rangeShortDateTime (the same day, not current year) (with milliseconds)
10:14:12.093–11:28:12.093, Jan 11, 2025
rangeShortDateTime (not current month)
Jan 1, 10:14 – Feb 1, 11:28
rangeShortDateTime (not current month) (with seconds)
Jan 1, 10:14:12 – Feb 1, 11:28:12
rangeShortDateTime (not current month) (with milliseconds)
Jan 1, 10:14:12.093 – Feb 1, 11:28:12.093
rangeShortDateTime (start date is not in current year)
Jan 1, 2025, 10:14 – Feb 1, 2026, 11:28
rangeShortDateTime (start date is not in current year) (with seconds)
Jan 1, 2025, 10:14:12 – Feb 1, 2026, 11:28:12
rangeShortDateTime (start date is not in current year) (with milliseconds)
Jan 1, 2025, 10:14:12.093 – Feb 1, 2026, 11:28:12.093
rangeShortDateTime (end date is not in current year)
Jan 1, 2026, 10:14 – Feb 1, 2027, 11:28
rangeShortDateTime (end date is not in current year) (with seconds)
Jan 1, 2026, 10:14:12 – Feb 1, 2027, 11:28:12
rangeShortDateTime (end date is not in current year) (with milliseconds)
Jan 1, 2026, 10:14:12.093 – Feb 1, 2027, 11:28:12.093

Opened range

Long format

Name
Value
openedRangeLongDate (only start)
From December 3
openedRangeLongDate (only end)
Until December 3
openedRangeLongDate (only start, start date is not in current year)
From December 3 2027
openedRangeLongDate (only end, end date is not in current year)
Until December 3 2027
openedRangeLongDateTime (only start)
From December 3, 15:10
openedRangeLongDateTime (only start) (with seconds)
From December 3, 15:10:12
openedRangeLongDateTime (only start) (with milliseconds)
From December 3, 15:10:12.103
openedRangeLongDateTime (only start, start date is not in current year)
From December 3 2027, 15:10
openedRangeLongDateTime (only start, start date is not in current year) (with seconds)
From December 3 2027, 15:10:12
openedRangeLongDateTime (only start, start date is not in current year) (with milliseconds)
From December 3 2027, 15:10:12.103
openedRangeLongDateTime (only end)
Until December 3, 15:10
openedRangeLongDateTime (only end) (with seconds)
Until December 3, 15:10:12
openedRangeLongDateTime (only end) (with milliseconds)
Until December 3, 15:10:12.103
openedRangeLongDateTime (only end, end date is not in current year)
Until December 3 2027, 15:10
openedRangeLongDateTime (only end, end date is not in current year) (with seconds)
Until December 3 2027, 15:10:12
openedRangeLongDateTime (only end, end date is not in current year) (with milliseconds)
Until December 3 2027, 15:10:12.103

Short format

Name
Value
openedRangeShortDate (only start)
From Dec 3
openedRangeShortDate (only end)
Until Dec 3
openedRangeShortDate (only start, start date is not in current year)
From Dec 3 2027
openedRangeShortDate (only end, end date is not in current year)
Until Dec 3 2027
openedRangeShortDateTime (only start)
From Dec 3, 15:10
openedRangeShortDateTime (only start) (with seconds)
From Dec 3, 15:10:12
openedRangeShortDateTime (only start) (with milliseconds)
From Dec 3, 15:10:12.107
openedRangeShortDateTime (only start, start date is not in current year)
From Dec 3 2027, 15:10
openedRangeShortDateTime (only start, start date is not in current year) (with seconds)
From Dec 3 2027, 15:10:12
openedRangeShortDateTime (only start, start date is not in current year) (with milliseconds)
From Dec 3 2027, 15:10:12.107
openedRangeShortDateTime (only end)
Until Dec 3, 15:10
openedRangeShortDateTime (only end) (with seconds)
Until Dec 3, 15:10:12
openedRangeShortDateTime (only end) (with milliseconds)
Until Dec 3, 15:10:12.107
openedRangeShortDateTime (only end, end date is not in current year)
Until Dec 3 2027, 15:10
openedRangeShortDateTime (only end, end date is not in current year) (with seconds)
Until Dec 3 2027, 15:10:12
openedRangeShortDateTime (only end, end date is not in current year) (with milliseconds)
Until Dec 3 2027, 15:10:12.107

The shortest format

Name
Seconds
Only minutes
Seconds
0:25
Seconds and milliseconds
0:25,125
Minutes and seconds
2:25
0:02
Hours, minutes and seconds
48:02:25
48:02

Long format

Name
Value
Seconds
21 seconds
Minutes and seconds
1 minute 25 seconds
Minutes
22 minutes
Minutes (more than hour)
120 minutes
Hours and minutes
1 hour 21 minutes
Hours and minutes (more than day)
32 hours 20 minutes
Hours
32 hours
Days and hours
1 day 8 hours
Days
2 days
Days (more then week)
21 days
Weeks and days
2 weeks 1 day
Weeks
2 weeks
Months and weeks
1 month 3 weeks
Months
2 months
Months with fraction
1.5 months
Years and months
3 years 11 months
Years
5 years
Years with fraction
5.5 years

Short format

Name
Value
Seconds and milliseconds
21.365 s
Seconds
21 s
Minutes and seconds
1 min 25 s
Minutes
22 min
Minutes (more than hour)
120 min
Hours and minutes
1 h 21 min
Hours and minutes (more than day)
32 h 20 min
Hours
32 h
Days and hours
1 d 8 h
Days
2 d
Days (more then week)
15 d
Weeks and days
2 w 1 d
Weeks
2 w
Months and weeks
1 mo 3 w
Months
2 mo
Months with fraction
2.5 mo
Years and months
3 y 11 mo
Years
5 y
Years with fraction
5.5 y
Suggestions for improvement
If you found a mistake or want to improve the article, create an issue on GitHub.