+ Start a Discussion
Shaker Kuncham 1Shaker Kuncham 1 

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


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 {
    public static List<PAYMENT_SCHEDULE__x> loadData(String recId){
        List<PAYMENT_SCHEDULE_x> paymentScheduleList = new List<PAYMENT_SCHEDULE_x>();
            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,
                                          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>
  <td data-label="Amount">
   <div class="slds-truncate" >{!item.PAYMENT_SCHEDULE_AMOUNT__c}</div>
  <td data-label="Balance">
   <div class="slds-truncate" >{!item.PAYMENT_SCHEDULE_BALANCE__c}</div>
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.

Ajay Dubedi