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
test269test269 

converting system date to mm/dd/yyyy format

Hi All,

  i want to convert system date to mm/dd/yyyy format in controller.Any one can you please help me this.

 

 

Thanks in advance.

SRKSRK

convert the date into string & u can do what ever u want with string

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

date tt=system.today();

string ss=tt.format();

system.debug('#########3'+ss);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Gunners_23Gunners_23

You can convert the dateTime to specific date format using the below method

 

DateTime d = Date.Today() ;
String dateStr =  d.format('dd/MM/yyyy') ;

 

and you can covert the date to dateTime using

 

Date dToday = System.Today();
 Datetime dt = datetime.newInstance(dToday.year(), dToday.month(),dToday.day());

 

Let me know if you need anything else

Rajesh SriramuluRajesh Sriramulu

Hi

 

Use format to convert System date to as u like.

 

datetime dt = system.today();
string  st=dt.format();
system.debug(st);

 

Regards,

Rajesh.

ManzaManza
I know this was a while ago but this was my solution
Date d = Date.today();
String dTXT = d.format();
system.debug('!!! dTXT ' + dTXT); //display date like: dd/mm/yyyy
system.debug('!!! new date ' + cleanDate(dTXT)); 

    public String cleanDate(String theDate){
        String [] s = theDate.split('/');
        String newDate= '';
        if(s[0].length() == 1){
            newDate = '0'+s[0];
        }else{
            newDate = s[0];
        }
        newDate = s[1]+'/'+newDate+'/'+s[2];
        return newDate;
    }