For example, the formatDate() method formats a date based on the formatString parameter set as the second argument.
formatDate (String | Number | Date date, String formatString)
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.
For more information on all the methods in AuraLocalizationService, see the JavaScript API in the Reference Doc App.
Use $A.localizationService to use the methods in AuraLocalizationService.
var now = new Date(); var dateString = "2017-01-15"; // Returns date in the format "Jun 8, 2017" console.log($A.localizationService.formatDate(now)); // Returns date in the format "Jan 15, 2017" console.log($A.localizationService.formatDate(dateString)); // Returns date in the format "2017 01 15" console.log($A.localizationService.formatDate(dateString, "YYYY MM DD")); // Returns date in the format "June 08 2017, 01:45:49 PM" console.log($A.localizationService.formatDate(now, "MMMM DD YYYY, hh:mm:ss a")); // Returns date in the format "Jun 08 2017, 01:48:26 PM" console.log($A.localizationService.formatDate(now, "MMM DD YYYY, hh:mm:ss a"));