[SalesForce] Cannot format date with ui:outputDate component

I am trying to format my date data and it is not working.

I am using an apex wrapper class to return my field data into the client side.
All works well, except when I use <ui:outputDate format="dd/MM/yyyy" value="{!v.field.value}"/> the date is never formatted to my needs:

enter image description here

field attribute is of type: YH_PageLayout_CTRL.YH_Field which is my wrapper class, and the wrapper inner class is:

public with sharing class YH_PageLayout_CTRL {

    // sub-class for representing a field to return to client
    public class YH_Field {

        @AuraEnabled public String name;
        @AuraEnabled public String label;
        @AuraEnabled public String value;
        @AuraEnabled public String dataType;
        @AuraEnabled public List<String> picklistValues;


        public YH_Field(String name, String label, String value, String dataType) {
            this.name = name;
            this.label = label;
            this.value = value;
            this.dataType = dataType;
        }

        public YH_Field(String name, String label, String dataType) {
            this.name = name;
            this.label = label;
            this.dataType = dataType;
        }
    }
}

When I use ui:outputDate on other cases where the value is a javascript date object – the formatting works good, but in this case – the value is not a date.

Can anyone help me format my date field?

Best Answer

It appears that you have a Date-Time value where the time is Midnight (00:00:00). You may have difficulties with it if you don't truncate it before putting it in your Apex wrapper class to remove the time value portion. You could also parse it to a Date-Time and use Date.valueof() on it. I strongly suspect that's what's preventing the formatting from occurring in your component since a Date wouldn't have the time element and your component won't know what to do with that portion of the string.

Related Topic