-1

I have a date as yyyy-mm-dd and i want to convert it into MMM d, yyyy in Apex. I was trying date method and converting it to string. I have today's date as '2023-07-11' and i want to return it as 'Jul 7, 2023'.

Updating the whole code for more understanding, I want this wrapper to receive in JS of LWC and display on UI table with formatted date. I am new to wrapper class, so Please suggest any kind of changes here.

public with sharing class MyTaskListViewController {

//Correct ONE

@AuraEnabled(cacheable=true)
public static List<MyPartnerTaskWrapper> getMyPartnerTask(){
    List<MyPartnerTaskWrapper> wrapList = new List<MyPartnerTaskWrapper>();
    Id userId = UserInfo.getUserId();
    List<Partner_Task__c> myTask = [ SELECT Name, Due_Date__c, Status__c
                                FROM Partner_Task__c where ownerId =:userId AND Status__c != 'Completed' With Security_Enforced];
    system.debug('MyTask '+ myTask);
    if(!myTask.isEmpty()){
        for(Partner_Task__c task : myTask){
            wrapList.add(new MyPartnerTaskWrapper(task));
        }
    }
    system.debug('WRAP '+wrapList);
    return wrapList;
} 

public static String formatTheDate(Date dueDate){
    
} 

//Wrapper Class
public class MyPartnerTaskWrapper{
    @AuraEnabled public Partner_Task__c mtask {get; set;}
    @AuraEnabled public Date formattedDueDate {get;set;}
    //@AuraEnabled public String name {get;set;}
    //@AuraEnabled public Date dueDate {get;set;}
    //@AuraEnabled public String status {get;set;}
    public MyPartnerTaskWrapper(Partner_Task__c mtask){
        this.mtask = mtask;
        this.formattedDueDate =  Date.valueof(mtask.Due_Date__c);
        //this.dueDate = dueDate;
        //this.status = status;
    }
}

}

3 Answers 3

1

Dates can be coerced into Datetime, the "gotcha" to look out for here is that doing so means Salesforce treats it as if the Datetime is in the GMT (UTC +0) timezone.

With that in mind, you can use the formatGmt() method (to avoid automatic timezone adjustment) and use a SimpleDateFormat string to do the work.

Datetime coerced = mtask.Due_Date__c;
formattedDate = coerced.formatGmt('MMM d, yyyy');
0

Since you already have the date in String format, split the string to get year and day values. To get the month name create a separate Map for month names.

String dateString = '2023-07-11';
List<String> dateValues = dateString.split('-');

Map<Integer, String> monthNames = new Map<Integer, String>{
  1 => 'January',
  2 => 'February',
  3 => 'March',
  4 => 'April',
  5 => 'May',
  6 => 'June',
  7 => 'July',
  8 => 'August',
  9 => 'September',
  10 => 'October',
  11 => 'November',
  12 => 'December'
};

String formattedDate =
  monthNames.get(Integer.valueof(dateValues[1])) +
  ' ' +
  dateValues[2] +
  ', ' +
  dateValues[0];
2
  • thanks.. this seems to work individually, but I am using wrapper class so I want my my wrapper class to return the date field with this formatted string. Please find more up to date code in Question.
    – SF Learner
    Commented Jul 12, 2023 at 4:38
  • I changes my column type to 'date' and it automatically changed it's format.
    – SF Learner
    Commented Jul 17, 2023 at 10:57
0

Used this formatting instead.

dateFormat(){
        if(this.myTaskWrappers) {
            this.myTaskWrappers = this.myTaskWrappers.map(item => {
                if(item.Due_Date__c){
                    let dateobj = new Date(item.Due_Date__c);
                    const options1 = {year: 'numeric', month: 'long', day: '2-digit' };
                    const dateFormat = new Date(dateobj.getFullYear(), dateobj.getMonth(), dateobj.getDate());
                    const dateTimeFormat1 = new Intl.DateTimeFormat(this.label.English_language, options1);
                    let convertedDate = dateTimeFormat1.format(dateFormat);
                    console.log(convertedDate);
                }
                return item;
            });
            
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .