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?

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.

  • Thanks @glls . You have right but I wonder why $Locale.dateTimeFormat is different from format described in Supported Locales. IMHO this is very misleading. – gpoluch Jul 1 '18 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 '18 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? – gpoluch Jul 1 '18 at 17:46
  • =) That itself is a different question, mind opening a new one with the details and current approach? Thanks – glls Jul 1 '18 at 17:47
  • Thanks @glls. I will open new question for that and mark here your answer as best. – gpoluch Jul 1 '18 at 17:50

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.