2

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?

  • Looks like you have a Date-Time value where the time is Midnight. Without parsing it to a Date-Time and taking the DateValue of it, you may have difficulties with it if you don't truncate it in your Apex wrapper class to remove the time. I suspect that's what's preventing the formatting from occurring as you'd like since a Date wouldn't have the time element. – crmprogdev Jul 16 '17 at 15:28
  • Looks like you are converting from a Date to a string on the server. To do the conversion on the client in the ui:outputDate you will probably need to have a @AuraEnabled public Date dateValue; field on your wrapper. – Keith C Jul 16 '17 at 18:35
  • You could use my hand built date picker here github.com/rapsacnz/DatePicker/tree/master/src/aura/InputDate. That way you can format it the way you like. Don't forget to include any required dependencies (DatetimeLib + an event, I think) – Caspar Harmer Jul 16 '17 at 18:36
  • Oops not the datepicker - the InputDate component that is part of the picker. It's best feature is that it doesn't force you to show a date picker when you click into it. – Caspar Harmer Jul 16 '17 at 18:55
  • Thanks all - Thanks to you I have fixed the problem . @crmprogdev I have removed the time portion from the string and that fixed it. do you want to write an answer so I can mark it as solved? – Itai Shmida Jul 17 '17 at 6:24
1

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.

  • Thanks @crmprogdev, although I did use 'Date.valueof()' it returned text value with the time element, So I have truncated the time element and that solved the problem. – Itai Shmida Jul 17 '17 at 14:51

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.