0

I have a Controller in which I need to update the StartDateTime of an Event, but only the Date part of the field - the time needs to stay the same. How do I do this? Do I need to extract the Time part from the old date first and then do a datetime.parse with the new date and the old time?

Or is there some nifty function that will only update the Date part?

2 Answers 2

2

I believe you can leverage the built in function for adding only the days

.addDays method

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm#apex_System_Datetime_addDays

1
  • That would work, but there doesn't seem to be a DateDiff function in apex in order to determine the number of days to add - I need to calculate the difference between the old date and the new date.
    – Irene
    Oct 8, 2015 at 2:58
2

Combined with @ZenSeeker's help and what I found on another blog this is what works:

 integer intDays =  e.StartDateTime.Date().daysBetween(new.EventDate__c);
 e.StartDateTime = e.StartDateTime.addDays(intDays);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.