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
crocodilecrocodile 

Type casting a Date variable into String type?

Hi,
         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,
-Vissu
Best Answer chosen by Admin (Salesforce Developers) 
iceberg4uiceberg4u
/*
        This function returns all Date-time in desired format.
    */
    public static String getFormattedDateTime(Datetime value, Integer formatType)
    {
        String formattedDate = '';
        if(value!=null)
        {
            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;
    }