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
billgreenhawbillgreenhaw 

Adding a year to date and day to last day of the month

Hi,

 

I need to take a date field and update another date field to be the original date + 1 year.  And also take the day from the original date and make it the last day of the month.

 

Can I do this by workflow? Or do I need to write some APEX for it?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
nylonnylon

This formula returns the last day on the month, probably.

 

DATE(YEAR(Date2__c)+IF(MONTH(Date2__c)=12,1,0),MOD(MONTH(Date2__c),12)+1,1)-1

All Answers

TrimbleAgTrimbleAg

Well, part one is easy, use a formula date field and do a:

 

Date1_c + 365

 

Now part 2 is where your going to have issues.... I dont see how to do this, but I am sure people out here will know, but it would require a trigger I would imagine.

 

PB

Cloud on FireCloud on Fire
Date1__c + 365 won't exactly work in a leap year, would it? You could try something like this: DATE(YEAR(Date1__c)+1,MONTH(Date1__c),CASE(MONTH(Date1__c),1,31,2,28,3,31,4,30...)) If you need to set to Feb 29 in leap years then you'll need to put an IF in the result for 2 in the CASE statement
nylonnylon

This formula returns the last day on the month, probably.

 

DATE(YEAR(Date2__c)+IF(MONTH(Date2__c)=12,1,0),MOD(MONTH(Date2__c),12)+1,1)-1

This was selected as the best answer