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
It looks like you are not setting the server side controller parameter correctly. This is why you see the value in your JS controller, but it is showing null in your apex controller.
Change this:
action.setParam({"siteId": message});
To this (syntax for array of params):
action.setParams({"siteId": message});
Looks like you are just missing the s at the end of setParams()
I believe you can also set a single param using this syntax:
action.setParam("siteId", message);
Best Answer
The $Locale global value provider returns information about the current user’s preferred locale. You can try using $locale.timezone in Aura to get the time zone and perhaps use it along with new Date to get the Date similar to the current loggged in user's timezone.
The offset -330 translates to UTC + 5:30 which is Asia/Kolkatta, if both your ORG and local Date is same, then it can mean only thing the timezone preference in your SALESFORCE ORG is Asia/Kolkatta. Kindly check your configurations.
Reference : https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/expr_locale_value_provider.htm
Note :