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

Type casting a Date variable into String type?

         I tried to convert a Date variable into String type using toString() but getting an error. Can any one please mention how to convert a Date variable into String type.

Thanks in adv,
Best Answer chosen by Admin (Salesforce Developers) 
        This function returns all Date-time in desired format.
    public static String getFormattedDateTime(Datetime value, Integer formatType)
        String formattedDate = '';
            if(formatType == 1)        
                formattedDate = value.format('MMM dd, yyyy','GMT - 8:00');
            else if (formatType == 2)        
                formattedDate = value.format('yyyy-MM-dd','GMT - 8:00');
            else if(formatType == 3)
                formattedDate = value.format('MMM dd, yyyy h:mm:ss a (z)','GMT - 8:00');
        return formattedDate;