+ Start a Discussion
balajibondarbalajibondar 

DateTime To Date Conversion Apex

I want to convert DateTime Value to Date Value , Please help me out ????????????
Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert
Try taking the Year, Month, and Day values out of the DateTime variable and pass them into the Date.newInstance() method.

Here is some psuedo-code:

DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());


All Answers

aalbertaalbert
Try taking the Year, Month, and Day values out of the DateTime variable and pass them into the Date.newInstance() method.

Here is some psuedo-code:

DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());


This was selected as the best answer
Ben4817Ben4817
This was extremely helpful.  Thanks for the code.
bprakashbprakash

iam still getting the time value but it set to 00;0:0

can we make this time value excluded?

Please help me any help is greatly appreciated.

Thanks

Bhanu

 

 

 

Priya_MarupudiPriya_Marupudi

Hi Bhanu,

 

try this. I was getting same issue but this resolved my problem...

string datetimestr =    LastRunDate.format('MMMMM dd, yyyy');  

 

this gives you date in this format:------- May 03, 2012

 If you need more formatting on dates and times please follow this link

http://paulforce.wordpress.com/2009/08/27/formatting-time-in-apex/

 

BALA_RAMBALA_RAM
Public Date mydate{get;set;}
public String sdate {get;set;}

DateTime dT = System.now();
myDate = date.newinstance(dT.year(), dT.month(), dT.day());
sdate = String.valueOfmyDate);

Now it's displaying Only Date
BALA_RAMBALA_RAM

Convert Datetime to Date

 

Public Date mydate{get;set;}
public String sdate {get;set;}

 DateTime dT = System.now();
 myDate = date.newinstance(dT.year(), dT.month(), dT.day());
 sdate = String.valueOfmyDate);

Now it's displaying Only Date 

JeffronJeffron

DateTime dt = System.now()

Date d = dt.date();

Michal KaparMichal Kapar
Date d = System.now().date();

System.debug(d);
Anil Kumar GandlaAnil Kumar Gandla
System.debug('Date and Time in GMT is....: '+system.now());
System.debug('Local Date is....: '+system.now().year()+'-'+system.now().month()+'-'+system.now().day());
system.debug('Local Time is....: '+System.now().hour()+':'+system.now().minute()+':'+system.now().second()+':'+system.now().millisecond());
David Roberts 4David Roberts 4
Related time constructs:-
Date todaysDate = system.today();
OR use DateTime class
Date todaysDate = datetime.date();

DateTime tDateTime = datetime.now();
tDateTime = tDateTime.addHours(9);
tDateTime = tDateTime.addMinutes(30);
shashikant pandeyshashikant pandey
You can try below code:
DateTime dT = system.now()
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
String datevalue = String.valueOf(myDate);

Thanks,
shashikant