+ 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
Robert howard 5Robert howard 5
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. https://loanhome.hatenablog.com  https://autodealerz.blogspot.com
mrferdos mrferdosmrferdos mrferdos
  • Great to be here in your article or post, whatever, I figure I ought to likewise buckle down for my own site like I see some great and refreshed working in your site. news blog 
selis asleiselis aslei
What is the best way to use the saleforce service form in specif page? I want to use for my review blog (https://www.gearsireview.com/).
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Date d = date.today();
 String dt = DateTime.newInstance(d.year(),d.month(),d.day()).format('MM-d-YYYY');
system.debug(dt);

Thanks
Arec JohnsonArec Johnson
Very helpful thread. I've used this code for one of my web page. You can check it here https://welderexpert.com/best-miller-welding-helmets/
sivareddy bvsivareddy bv
You can try below code:
DateTime myDateTime = system.now();
Date myDate = myDateTime.date(); (or) myDateTime.dateGMT();
Oleksii TarkovOleksii Tarkov
OR myDate = Date.ValueOf(myDateTime);