# Days in this month and days in this year formulas

I have a formulas of days in this month and this year with start and finish dates. My formula counts the number of days in this month/year that fall in a given interval (between Start Date and Close Date). For example, I have Start Date 30.12.2018, and Finish Date 03.01.2019. Days of this (2019) year should be 2 (without last day), and days of this (May) month should be 0.

So, I'm trying to count the number of days between the Start Date and CloseDate that fall in the current Month, and the number of days between the Start Date and CloseDate that fall in the current Year.

But I have troubles with big intervals through the years.   How can I fix these formulas?

Counting days for this month:

``````IF(
AND(
MONTH(TODAY()) = MONTH(Start_Date__c),
MONTH(TODAY()) = MONTH(CloseDate)
),
Days__c,
IF(
AND(
MONTH(TODAY()) > MONTH(Start_Date__c),
MONTH(TODAY()) = MONTH(CloseDate)
),
Days__c - (
(
TODAY() - DAY(TODAY()) + 1
) - Start_Date__c
),
IF(
AND(
MONTH(TODAY()) = MONTH(Start_Date__c),
MONTH(TODAY()) < MONTH(CloseDate)
),
IF(
MONTH(TODAY()) = 12,
DATE(YEAR(TODAY()), 12, 31),
DATE(YEAR(TODAY()), MONTH(TODAY()) + 1, 2) - 1
) - Start_Date__c,
IF(
AND(
MONTH(TODAY()) > MONTH(Start_Date__c),
MONTH(TODAY()) < MONTH(CloseDate)
),
DAY(
DATE(
YEAR(TODAY()), MONTH(TODAY()), 01
), 1
) - 1
),
0
)
)
)
)
``````

Counting days for this year:

``````IF(
AND(
YEAR(TODAY()) = YEAR(Start_Date__c),
YEAR(TODAY()) = YEAR(CloseDate)
),
Days__c,
IF(
AND(
YEAR(TODAY()) > YEAR(Start_Date__c),
YEAR(TODAY()) = YEAR(CloseDate)
),
Days__c - (
(
TODAY() - DAY(TODAY()) + 1
) - Start_Date__c
),
IF(
AND(
YEAR(TODAY()) = YEAR(Start_Date__c),
YEAR(TODAY()) < YEAR(CloseDate)
),
(DATE(YEAR(TODAY()), 12, 31) + 1) - TODAY(),
IF(
AND(
YEAR(TODAY()) > YEAR(Start_Date__c),
YEAR(TODAY()) < YEAR(CloseDate)
),
TODAY() - DATE(YEAR(TODAY()), 1, 1) + 1,
0
)
)
)
)
``````

• This seems like logic which would be much more easily implemented via `Apex Trigger`. – Adrian Larson May 6 '19 at 15:50