0

I have the following code:

<ui:outputDateTime value="{!v.selectedOrder.order.CreateTime__c}" format="dd-MMM-yyyy hh:mm a"/>

Which outputs: 05-Jun-2019 05:20 PM

However, when I try to use the newer version:

<lightning:formattedDateTime value="{!v.selectedOrder.order.CreateTime__c}" year="numeric" month="numeric" day="numeric" hour="2-digit" minute="2-digit" timeZoneName="short" hour12="true"/>

I get the error:

The value attribute accepts either a Date object, a timestamp, or a valid ISO8601 formatted string with timezone offset. but we are getting the string value "2019-06-06T00:20:25.000+0000" instead.

I understand the error message (and it means Javascript Date or Timestamp object versus Apex), but I don't understand why it is necessary to do a conversion of an SObject date field to use this widget.

It would be great if someone could share an inline way to do this without having to resort to building a custom Javascript method to convert Apex dates. Thanks!

0

With the Aura component <lightning:outputField> you should be able to do just that what you are looking for: https://developer.salesforce.com/docs/component-library/bundle/lightning:outputField/example

The downside to that approach is that you have to use the <lightning:recordViewForm> component.

So that would look like this:


<lightning:recordViewForm recordId="{!v.selectedOrder.order.Id}" objectApiName="Order">

  <lightning:outputField fieldName="CreateTime__c" />

</lightning:recordViewForm>



As described in you comment, if you really want to keep using the lightning:formattedDateTime, you have to convert the ISO8601 value '2019-06-06T00:20:25.000+0000' to a unix timestamp, or a Javascript Date object.

That can be done like this in a Javascript controller / helper:


var createTime = cmp.get('v.selectedOrder.order.CreateTime__c');//'2019-06-06T00:20:25.000+0000';

var newDate = Date.parse(createTime);

cmp.set('v.selectedOrder.order.CreateTime__c', newDate);


| improve this answer | |
  • Thanks Theodoor, but I think that this might be is a bit different use case. I'm just trying to output it statically, versus as part of a record or form. And what I really want to know is how to use the lightning:formattedDateTime widget. – Shane Jul 2 '19 at 12:05
  • @Shane I have changed my answer in order to help you with your comment – Theodoor Jul 2 '19 at 14:41

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.