I made my own little helper function to assist with a similar problem. I did employ the use of Moment.js to assist in my solution. I had a requirement involving server-side data and local display which if rendered incorrectly would throw nasty errors when the user tried to save the record back to the server (external object).
For the requirement of displaying server side data correctly—
What I found to work best was to use the global function:
$A.get("$Locale.userLocaleLang")
This retrieves the locale string which can then be passed to my custom function which covers most of the possible global dates (we have tested on most EU countries, South Africa, Russia and North America):
var formatFunc = helper.format($A
.get("$Locale.userLocaleCountry"));
*this can be called directly like in the above case or via another function in the helper class listed below
format : function(loc) {
console.log("locale in function " + loc);
var dFormat = '';
var locale = loc.toLowerCase();`
switch (true) {
case locale.indexOf('us') != -1:
dFormat = 'MM/DD/YYYY';
break;
case locale.indexOf('de') !== -1:
case locale.indexOf('fi') !== -1:
case locale.indexOf('cz') !== -1:
case locale.indexOf('pl') !== -1:
dFormat = 'DD.MM.YYYY';
break;
case locale.indexOf('za') !== -1:
dFormat = 'YYYY/MM/DD';
break;
case locale.indexOf('it') !== -1:
dFormat = 'DD/MM/YYYY';
break;
case locale.indexOf('cn') !== -1:
dFormat = 'YYYY-MM-DD';
break;
case locale.indexOf('fr_be') !== -1:
case locale.indexOf('nl_be') !== -1:
dFormat = 'DD/MM/YYYY';
break;
case locale.indexOf('dk') !== -1:
case locale.indexOf('nl') !== -1:
dFormat = 'DD-MM-YYYY';
break;
case locale.indexOf('fr_ca') !== -1:
case locale.indexOf('sv_se') !== -1:
dFormat = 'YYYY-MM-DD';
break;
default:
dFormat = 'DD/MM/YYYY';
break;
}
return dFormat;
}
*this function can be passed a date string and will return a correctly formatted date for the running user
formatLocale : function(dateString) {
var loc = $A.get("$Locale.userLocaleCountry");`
var locale = this.format(loc);
var date = moment(dateString, "YYYY-MM-DD");
date = moment(date).format(locale);
return date
}
What you get back is the correct formatting for use with Moment like so:
var date = moment(row.LastModifiedDate).format(locale);
Before I went to this length to design my own solution I tried the Lightning output formatted date. With the variety of server data formats it simply was not an adequate solution.
For the requirement of showing today's date—
First I initialize my moment date string, then I pass the date to the formatLocale function in my helper class:
var today = new Date();
var d = helper.formatLocale(today);
I hope this helps you... I certainly went through a good deal of trial and error to get this solution. As you can see you can either call the helper function directly to retrieve the format or you can pass a date to the formatLocale function which will in turn call the format function and pass a formatted date back.
Regards -S
Best Answer
The
ui:outputDate
component has an attribute calledlangLocale
which you can pass a country code to and an attribute calledformat
that should follow thejava.text.SimpleDateFormat
interface. Thus, you should be able to do