DEV Community

Cover image for @angular/common Functions
bob.ts
bob.ts

Posted on

@angular/common Functions

This article all started as I had to format a date and started to bring in Moment.js (a tool that I've used many times over the years) and saw this ...

Considering using Moment in your project? There may be better modern alternatives. For more details and recommendations, please see Project Status in the docs.

After reading through the project status and doing some basic searches I came across the fact that Angular has a series of date functions (and more).

Documentation HERE

In my research, I've seen that pipes are the preferred methods for using this type of functionality; for my particular case, pipes would have been overkill.

However, I did want to introduce this functionality for anyone considering using an external tool that can be managed within Angular itself.

Before starting, HERE is the documentation on DatePipe.

Date Functions

Function Description
formatDate Formats a date according to locale rules.
getLocaleDateFormat Retrieves a localized date-value formatting string.
getLocaleDateTimeFormat Retrieves a localized date-time formatting string.
getLocaleDayNames Retrieves days of the week for the given locale, using the Gregorian calendar.
getLocaleDayPeriods Retrieves day period strings for the given locale.
getLocaleEraNames Retrieves Gregorian-calendar eras for the given locale.
getLocaleExtraDayPeriodRules Retrieves locale-specific rules used to determine which day period to use when more than one period is defined for a locale.
getLocaleExtraDayPeriods Retrieves locale-specific day periods, which indicate roughly how a day is broken up in different languages. For example, for en-US, periods are morning, noon, afternoon, evening, and midnight.
getLocaleFirstDayOfWeek Retrieves the first day of the week for the given locale.
getLocaleMonthNames Retrieves months of the year for the given locale, using the Gregorian calendar.
getLocaleTimeFormat Retrieves a localized time-value formatting string.
getLocaleWeekEndRange Range of week days that are considered the week-end for the given locale.

Currency

Function Description
formatCurrency Formats a number as currency using locale rules.
getCurrencySymbol Retrieves the currency symbol for a given currency code.
getLocaleCurrencyCode Retrieves the default currency code for the given locale.
getLocaleCurrencyName Retrieves the name of the currency for the main country corresponding to a given locale. For example, 'US Dollar' for en-US.
getLocaleCurrencySymbol Retrieves the symbol used to represent the currency for the main country corresponding to a given locale. For example, '$' for en-US.
getNumberOfCurrencyDigits Reports the number of decimal digits for a given currency. The value depends upon the presence of cents in that particular currency.

Numeric

Function Description
formatNumber Formats a number as text, with group sizing, separator, and other parameters based on the locale.
formatPercent Formats a number as a percentage according to locale rules.

Locale

Function Description
getLocaleId Retrieves the locale ID from the currently loaded locale. The loaded locale could be, for example, a global one rather than a regional one.
getLocaleNumberFormat Retrieves a number format for a given locale.
getLocaleNumberSymbol Retrieves a localized number symbol that can be used to replace placeholders in number formats.
getLocalePluralCase Retrieves the plural function used by ICU expressions to determine the plural case to use for a given locale.
registerLocaleData Register global data to be used internally by Angular. See the "I18n guide" to know how to import additional locale data.

Platform

Function Description
isPlatformBrowser Returns whether a platform id represents a browser platform.
isPlatformServer Returns whether a platform id represents a server platform.
isPlatformWorkerApp Returns whether a platform id represents a web worker app platform.
isPlatformWorkerUi Returns whether a platform id represents a web worker UI platform.

Writing

Function Description
getLocaleDirection Retrieves the writing direction of a specified locale.

Conclusion

Angular has a series of date functions (and more).

The documentation is HERE

I've seen that pipes are the preferred methods for using this type of functionality. However, I did want to introduce this functionality for anyone considering using an external tool that can be managed within Angular itself.

Discussion (0)