Help and Support

XL: How to Calculate the Number of Days in a Month

Article ID:214313
Last Review:January 24, 2007
Revision:2.3
This article was previously published under Q214313

SUMMARY

In Microsoft Excel, you can use date functions to calculate the number of days in a month. For example, you can use the following formula to return the number of days in a month, based on a given starting date
=DAY(DATE(YEAR(The_Date),MONTH(The_Date)+1,0))
where The_Date is the starting date.

This formula works by finding the "zeroth" day of the month after The_Date. Because there is no "zeroth" day of any month, this date is interpreted as the last day of the month containing The_Date. The DAY function then converts this date into a day number.

Or, to calculate the number of days in a month, given the month and year, use the following function
=DAY(DATE(my_year,my_month+1,1)-1)
where my_month is the month for which you want to return the number of days, and my_year 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.

Back to the top

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:
https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)
In Microsoft Excel, you can also use the following Visual Basic for Applications procedure:
Sub Test()
    Dim m As Integer, y As Integer, DaysInMonth As Integer
    ' define variable for number of the month, in this example, November
    m = 11
    ' define variable for the year
    y = 1994
    DaysInMonth = DateSerial(y, m + 1, 1) - DateSerial(y, m, 1)
    MsgBox DaysInMonth
End Sub
				

Back to the top

REFERENCES

For more information about functions, click Microsoft Excel Help on the Help menu, type about using functions to calculate values in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Back to the top


APPLIES TO
Microsoft Excel 2000 Standard Edition
Microsoft Excel 2002 Standard Edition
Microsoft Excel 97 Standard Edition
Microsoft Excel 2001 for Mac
Microsoft Excel 98 for Macintosh

Back to the top

Keywords: 
kbhowto KB214313

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.