Newer Version Available
Formatting Dates in JavaScript
For example, the formatDate() method formats a date based on the formatString parameter set as the second argument.
The date parameter can be a String, Number, or most typically a JavaScript Date. If you provide a String value, use ISO 8601 format to avoid parsing warnings.
The formatString parameter contains tokens to format a date and time. For example, "YYYY-MM-DD" formats 15th January, 2017 as "2017-01-15". The default format string comes from the $Locale value provider.
This table shows the list of tokens supported in formatString.
| Description | Token | Output |
|---|---|---|
| Day of month | d | 1 … 31 |
| Month | M | 1 ... 12 |
| Month (short name) | MMM | Jan … Dec |
| Month (full name) | MMMM | January … December |
| Year | y | 2017 |
| Year (identical to y) | Y | 2017 |
| Year (two digit) | YY | 17 |
| Year (four digit) | YYYY | 2017 |
| Hour of day (1-12) | h | 1 … 12 |
| Hour of day (0-23) | H | 0 … 23 |
| Hour of day (1-24) | k | 1 … 24 |
| Minute | m | 0 … 59 |
| Second | s | 0 … 59 |
| Fraction of second | SSS | 000 … 999 |
| AM or PM | a | AM or PM |
| AM or PM (identical to a) | A | AM or PM |
| Zone offset from UTC | Z | -12:00 … +14:00 |
| Quarter of year | Q | 1 … 4 |
| Week of year | w | 1 … 53 |
| Week of year (ISO) | W | 1 … 53 |
There are similar methods that differ in their default output values.
- formatDateTime()—The default formatString outputs datetime instead of date.
- formatDateTimeUTC()—Formats a datetime in UTC standard time.
- formatDateUTC()—Formats a date in UTC standard time.
For more information on all the methods in AuraLocalizationService, see the JavaScript API in the Reference Doc App.
Example
Use $A.localizationService to use the methods in AuraLocalizationService.