function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shaker Kuncham 1Shaker Kuncham 1 

How to change the string date format to Date format which is coming through External object.

Hi 

How to change the string date format to Date format which is coming through External object.

It should be : YYYY-mm-dd

however its getting yyyymmdd

Code : public class PaymentScheduleController {
    @AuraEnabled
    public static List<PAYMENT_SCHEDULE__x> loadData(String recId){
        List<PAYMENT_SCHEDULE_x> paymentScheduleList = new List<PAYMENT_SCHEDULE_x>();
        if(!String.isEmpty(recId)){
            Giving_History_c sourceObj = [Select Id,Name,Gift_Record_Typec,Gift_Sub_Typec,Pledge_Statusc from Giving_History_c where id=: recId];
            
            if((!String.isEmpty(sourceObj.Gift_Record_Type_c) && (sourceObj.Gift_Record_Type_c == 'Pledge'))
               && (!String.isEmpty(sourceObj.Gift_Sub_Type_c) && !(sourceObj.Gift_Sub_Typec == 'Open Sustaining Pledge' || sourceObj.Gift_Sub_Type_c == 'Open Payroll Pledge'))){
                   
                   paymentScheduleList = [Select Id,PAYMENT_SCHEDULE_AMOUNT__c,
                                          PAYMENT_SCHEDULE_BALANCE__c,
                                          PAYMENT_SCHEDULE_DATE__c,
                                          PAYMENT_SCHEDULE_PLEDGE_NBR__c,
                                          PAYMENT_SCHEDULE_STATUS__c 
                                          from PAYMENT_SCHEDULE__x where 
                                          PAYMENT_SCHEDULE_PLEDGE_NBR_c =: sourceObj.Name order by PAYMENT_SCHEDULE_DATE_c asc];   
               
               }
        }
        return paymentScheduleList;
    }    
}




<aura:iteration items="{!v.PaymentScheduleList}" var="item">
 <tr class="slds-hint-parent">
  <th data-label="Date" scope="row">
     <div class="slds-truncate" >
     <lightning:formattedDateTime value="{!item.PAYMENT_SCHEDULE_DATE__c}" year="numeric" month="numeric" day="numeric"/></div>
  </th>
  <td data-label="Amount">
   <div class="slds-truncate" >{!item.PAYMENT_SCHEDULE_AMOUNT__c}</div>
  </td>
  <td data-label="Balance">
   <div class="slds-truncate" >{!item.PAYMENT_SCHEDULE_BALANCE__c}</div>
  </td>                        
 </tr>                    
</aura:iteration>
Ajay K DubediAjay K Dubedi
Hi Shaker,
    According to your requirement, you can implement your logic by these methods:
    
1) private Date setStringToDateFormat(String myDate) {
       String[] myDateOnly = myDate.split(' ');
       String[] strDate = myDateOnly[0].split('/');
       Integer myIntDate = integer.valueOf(strDate[1]);
       Integer myIntMonth = integer.valueOf(strDate[0]);
       Integer myIntYear = integer.valueOf(strDate[2]);
       Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);
       return d;
   }
   
2) String myDate;
   Date convertedDate = Date.valueOf(myDate);
   
Hope this will be helpful to you.

Thanks.
Ajay Dubedi