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
westjohnwestjohn 

Converting Date-time to date

if any possible to convert date-time to date field?

 

Example:

 

Date/Time 

Tue Jul 10 00:00:00 GMT 2012

 

Expected Output:(Date output)

Tue Jul 10, 2012

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hello westjohn,

 

Your output is not a valid date format, But you could be able to get that format in string with help of following code.

 

Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
String formattedDate=dt.format('EEE MMM dd hh:mm:ss')+' GMT '+ dt.format('yyyy');
System.debug('formattedDate : ' + formattedDate);

 

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

You can use date(),a instance method of DateTime class.

westjohnwestjohn

thanks Alok_vibrant..

But i didn't get the exact answer.. i only get the following answer

 

2012-07-10 00:00:00

 

But i want the exact answer(Tue Jul 10, 2012)

 

My code 

 

dateTime dd = Acc.LastActivityDate;
Date dd1 = dd.Date();

 

How to get the exact format from date() function?

Rahul SharmaRahul Sharma

Hello westjohn,

 

Your output is not a valid date format, But you could be able to get that format in string with help of following code.

 

Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
String formattedDate=dt.format('EEE MMM dd hh:mm:ss')+' GMT '+ dt.format('yyyy');
System.debug('formattedDate : ' + formattedDate);

 

This was selected as the best answer
westjohnwestjohn

Thanks Rahul..