4

There are these two posts already:

Is there a simple and reliable solution?

(I'd like to add some simple business logic - a comment being editable on the day it is added only. Being able to check for "today" in the user's server-side timezone would seem a common enough need.)

5

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

  • Appreciate you sharing this. Bad new that so much code is needed... – Keith C Dec 4 '17 at 20:21
  • Well, for all the possible formats.... this is pretty concise. My requirement is a bit more stringent in that I need the format to be a match for the user when they run a server side request back from the page. Lightning definitely doesn't provide all the bells and whistles yet so I am finding that rolling my own prevents surprises when I find the limits of SFDC's base components. On the bright side, at least none of the above code requires a server request. – binaryLady Dec 4 '17 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.