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
FerozFeroz 

Apex: assign Date/time data type to date datatype

Hello guys, 
I'm having a little difficultly 
I've a 2 custom fields one with date time data type and the other is date data type. 

what I'm trying to do is 

variable1 = variable2 + 30 ; 

where v1 is date datatype and v2 date/time data type .
basically i want to add date from next 30 days


and Im getting an error as "Illegal assingment from daterime to date

AnkaiahAnkaiah (Salesforce Developers) 
Hi Feroz,

Convert datetime field to date and calculate date field.
variable2 dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
variable1  = myDate + 30;

If this helps, please mark it as best aswer.

Thanks!!
mukesh guptamukesh gupta
Hi Feroz,

please use below code:-
 
DateTime variable2  = System.now();
Date myDate = date.newinstance(variable2.year(), variable2.month(), variable2.day());
Date variable1  = myDate.addDays(30);
System.debug('variable1 '+variable1);
OUTPUT:-
DEBUG|variable1 2021-09-09 00:00:00

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
ravi soniravi soni
Hy Feroz,
try below code.
DateTime variable2  = System.now();
Date myDate = date.newinstance(variable2.year(), variable2.month(), variable2.day());
Date variable1  = myDate.addDays(30);
System.debug('variable1 '+variable1);
I'm sure your problam will be solved by using it.
don't forget to mark it as best answer.
Thank you
 
mukesh guptamukesh gupta
Hi Feroz,

If my solution is usful for you then please mark it as best answer.

Regards
Mukesh
Alexia SparrowAlexia Sparrow
Hi, Mr. Gupta, your answer is really useful for me so I mark it best answer. I am a developer and while I was creating the https://writinguniverse.com/free-essay-examples/lord-of-the-flies/ web page then I faced this problem to create an event and your answer helped me resolve my problem. Now, this site is the best writing-related help site and many students take help from this site.