7

When I use the lightning:formattedDateTime in lightning component with a date field and formatting with "numeric", it displays a different value from the field. I use the following line of code:

<lightning:formattedDateTime value="{!task.due_date}" year="numeric" month="numeric" day="numeric" />

And the due_date field, that is a Date only and holds the value "4/24/2018", displays the value "4/23/2018" on the page. But if I use the code without specifying the format, it displays the right value, but not in the format I want (displays "Apr 24, 2018").

Someone knows what can be done to display the right value in the numeric format? I do not want to specify the time zone because it has to display in user's time zone.

  • Allowed values for the month attribute are 2-digit, narrow, short, or long. Have you tried one of those and see how it looks? developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Folkert Apr 25 '18 at 13:47
  • 4
    If task.due_date is a DateTime, then you need to add timeZone="{! $Locale.timezone }" to get the timezone handled correctly. Think not needed if it's a Date though. – Keith C Apr 25 '18 at 14:06
  • @KeithC the 'task.due_date' is a Date, but even so, worked like a charm! Thank you! – Igor Santana Apr 25 '18 at 16:25
  • @KeithC Care to post that information as an Answer proper? – Adrian Larson Jul 20 '18 at 19:05
  • 1
    Why isn't timeZone="{!$Locale.timezone}" default? Strange... – Scott Pelak Aug 2 '18 at 13:26
3

Thank you @KeithC! Even the task.due_date being a Date field, putting the code timeZone="{! $Locale.timezone } works and the timezone is handled correctly!

  • Common etiquette on the forum would be to add a comment where you @mention the person who gave you the answer, prompting them to post an answer so you can properly recognize/reward their assistance. – Adrian Larson Jul 20 '18 at 18:54
  • @AdrianLarson sorry, actually I've tried to mention him, but it didn't work. Could you try to so Keith C post the answer so I can mark as solved? – Igor Santana Jul 20 '18 at 19:03
  • Fair point. I'll just bump that in the comments. Don't forget to accept this answer if he never gets back to you! – Adrian Larson Jul 20 '18 at 19:04
  • 1
    I had a similar issue with the date being incorrect when passing a date value to a column of type 'date' on the lightining:datatable component (which renders a lightning:formattedDateTime component behind the scenes). Unfortunately passing the timezone from the locale didn't work, but manually passing 'UTC' as the timezone value made the dates appear correctly. – Mike Topalovich Sep 4 '18 at 22:04
  • @Mike Topalovich: If you are working in lightning:datatable, and if you want to display the value on date column irrespective of user's locale timezone, you can use type: 'date-local' while defining the column for date in lightning:datatable – Lakshmi Priya Sep 11 '18 at 11:34

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