1

I noticed that lightning have some problem with datetime format. Seems like issue with $Locale.dateTimeFormat that returns wrong value. I set-up my Salesforce org User locale settings to English(United States) so I expecting datetime value formatted according to Supported Locales: 2/07/2018 8:00 AM but I recieved datetime in following format: Jul 2, 2018 8:00:00 AM. Additionally line: console.log($Locale.dateTimeFormat); shows following format: MMM d, yyyy h:mm:ss a.

This is known and reported Issue or I need additional configuration for correct datetime formats on Lightning JS side?

1 Answer 1

3

The documentation is pretty clear (imo) on this one,

The framework’s number and date formatting are based on Java’s DecimalFormat and DateFormat classes.

and the format sample in it:

"MMM d, yyyy h:mm:ss a"

so:

Jul 2, 2018 8:00:00 AM

seems accurate. That being said, you will have to do the formatting client side. Fortunately, the same documetnation, provides a link towards client-side localization support on input and output components and recommends using lightning:formattedDateTime

Sample component markup:

<aura:attribute name="datetime" type="DateTime"/>
<lightning:formattedDateTime value="{!v.datetime}" timeZone="Europe/Berlin" 
                             year="numeric" month="short" day="2-digit" hour="2-digit"
                             minute="2-digit" second="2-digit"/>

The doc Supported Locales you are referring to is for platform specific supported locales, vs Lightning's $Locale Global Value, which is used to retrieve certain user prefered locales, which in turn can be used to help with the Number and Date Formatting Client side.

5
  • Thanks @glls . You have right but I wonder why $Locale.dateTimeFormat is different from format described in Supported Locales. IMHO this is very misleading. Jul 1, 2018 at 17:24
  • I believe that doc you are referring to is for platform specific supported locales, vs Lightning's $Locale Global Value, which is used to retrieve certain user prefered locales, which in turn can be used to help with the Number and Date Formatting Client side, but i understand your confusion.
    – glls
    Jul 1, 2018 at 17:33
  • I want to get same datetime format in all places in my app based on user locale settings and noticed that. I was looking for best solution for that but I didn't find any reasonable one for my requirement. Only possible solution I have found requires to construct map with locale codes and formats (en_US -> 'M/d/yyyy h:mm a')? What do you think about that @glls? Jul 1, 2018 at 17:46
  • =) That itself is a different question, mind opening a new one with the details and current approach? Thanks
    – glls
    Jul 1, 2018 at 17:47
  • Thanks @glls. I will open new question for that and mark here your answer as best. Jul 1, 2018 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .