Wednesday, July 22, 2009

How to Calculate the Number of Days in a Month

Step 1: Create a Formula Field (Number)

Step 2: Paste this formula : DAY(DATE(YEAR(TODAY()),MONTH(TODAY())+1,1)-1)

Function :

Where MONTH(TODAY()) is the month for which you want to return the number of days, and YEAR(TODAY()) is the year. This function calculates the first day of the next month and subtracts 1, which equals the last day of the current month. The DAY function then gives you the day of the month.





8 comments:

  1. Thank you for the formula. The problem is that in December, MONTH(TODAY())+1 equals 13, which is invalid, and causes an error. How would you handle that problem?

    ReplyDelete
  2. Receiving that error now, any solution? What about an IF statement and "12"? Thoughts

    ReplyDelete
  3. IF(
    MONTH( date ) = 12,
    DATE( YEAR( date ), 12, 31 ),
    DATE( YEAR( date ), MONTH ( date ) + 1, 1 ) - 1
    )

    ReplyDelete
  4. Yes it's December now and i'm having this issue! The IF formula doesn't seem to work.....help!

    ReplyDelete
    Replies
    1. IF(
      MONTH( TODAY() ) = 12,
      31,
      DAY(
      DATE( YEAR( TODAY() ), MONTH ( TODAY() ) + 1, 1 )
      - 1 )
      )

      Delete
    2. sfdcGeek's formula returns the last day of the specified month as a date, my formula returns the last day of the current month as a number.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete

Followers