Search code examples
salesforceapex

Difference between month calculator in salesforce formula field


I have two dates 1.statdate : 12-11-2022 2.enddate. : 02-20-2023

if the start date is 1-15th day then the result should be the current month ex: mm-dd-yyyy[12-11-2022] Then December 1st if the start date is 16th-31st day then the result should be the current month EX: mm-dd-yyyy[12-22-2022] Then January 1st.

if the EndDate is 1-15th day then the result should be the current month+1 EX: mm-dd-yyyy[02-11-2022] Then January 31st if the EndDate is 16-31sh day then the result should be the current month+1 EX: mm-dd-yyyy[02-20-2022] Then Feb 28th

input start date result: December 1st input end date result: Feb 28th Result[3] which is three months from the start date to the end date.

Can we do this in the formula field? I am able to do it in apex it worked but I was unable to do it in the formula field any help would be appreciated.


Solution

  • ROUND(((IF(DAY(Return_To_Work__c) <= 15, DATE( YEAR(Return_To_Work__c) ,
    MONTH(Return_To_Work__c) -1,(DAY(Return_To_Work__c)- 
     DAY(Return_To_Work__c)+
     28 + MOD(((MONTH(Return_To_Work__c) -1) + 
      FLOOR((MONTH(Return_To_Work__c) -1)/8)), 2) + MOD(2, 
      (MONTH(Return_To_Work__c) -1)) + 2 * FLOOR(1/(MONTH(Return_To_Work__c) 
      -1))))
     ,IF(DAY(Return_To_Work__c) >= 16,DATE( YEAR(Return_To_Work__c) 
     ,MONTH(Return_To_Work__c),(DAY(Return_To_Work__c)- 
      DAY(Return_To_Work__c)+28 + MOD(((MONTH(Return_To_Work__c)) + 
        FLOOR((MONTH(Return_To_Work__c))/8)), 2) + MOD(2, 
      (MONTH(Return_To_Work__c))) + 2 * 
       FLOOR(1/(MONTH(Return_To_Work__c))))),NULL)) -
      IF(DAY(First_Day_Of_Leave__c) <= 15, DATE( YEAR(First_Day_Of_Leave__c) 
      , MONTH(First_Day_Of_Leave__c) ,(DAY(First_Day_Of_Leave__c)- 
      DAY(First_Day_Of_Leave__c)+1)),IF(DAY(First_Day_Of_Leave__c) >= 
        16,DATE( YEAR(First_Day_Of_Leave__c) 
      ,MONTH(First_Day_Of_Leave__c)+1,(DAY(First_Day_Of_Leave__c)- 
       DAY(First_Day_Of_Leave__c)+1)),NULL)))/30),0)