+ Reply to Thread
Results 1 to 23 of 23

counting months between dates and returning a decimal

  1. #1
    Registered User
    Join Date
    01-08-2004
    Posts
    12

    counting months between dates and returning a decimal

    I need to calculate the number of months between two dates 1/1/07 and the current date on a paystub. I need the answer as a decimal so that I can YTD income properly. What is the easiest way to do this?

    1/1/07
    6/15/07

    should return a value of 5.5

    Currently I am using =DATEDIF(B2,B10,"m") but it rounds the months down.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,703
    To do this you need to do some estmation of the average month length. Assuming that's 30.5 days try

    =(B10-B2)/30.5

    format cell as number

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    have a look at
    http://www.ozgrid.com/forum/showthread.php?t=38258

    It gives a method of calculating last day of month to give full decimal answer
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,703
    Quote Originally Posted by mudraker
    It gives a method of calculating last day of month to give full decimal answer
    hello mudraker, are you referring to this formula?

    =DATEDIF(A1,B1,"m")+(DATEDIF(A1,B1,"md")/DAY(EOMONTH(B1,0)))

    You can get some odd results with DATEDIF formulas. If A1 contains the date 30th November 2006 then look at the results obtained by putting the following successive dates in B1

    26/02/2007 2.96
    27/02/2007 3.00
    28/02/2007 3.04
    01/03/2007 2.97
    02/03/2007 3.00
    03/03/2007 3.03
    04/03/2007 3.06

    [note dates in dd/mm/yyyy format]

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    daddylonglegs

    Yes I was refering to that formula

    I have never used Datedif in a formula. I have used datediff in macros for calculating number of days but never months except in the threads of the links below

    http://www.excelforum.com/showthread.php?t=598055
    http://www.ozgrid.com/forum/showthread.php?t=67891

  6. #6
    Registered User
    Join Date
    07-22-2015
    Location
    Not given
    MS-Off Ver
    Not given
    Posts
    1

    Re: counting months between dates and returning a decimal

    The below formula is ugly but it works!!!!.... all in one cell, no macro needed! It takes in to account each month's days regardless if it is has 30, 31, or in the instance of February 28 days. It also takes into account leap year where February has 29 days.

    A1 = Start Date
    B2 = Last Date

    =(IF(MONTH(A1)=MONTH(B1),(YEAR(B1)-YEAR(A1))*11,((YEAR(B1)-YEAR(A1))*12)-12+(12-MONTH(A1))+MONTH(B1)-1))+(EOMONTH(A1,0)-A1+1)/DAY(EOMONTH(A1,0))+(1-(EOMONTH(B1,0)-B1)/DAY(EOMONTH(B1,0)))

    Try it! It works!

  7. #7
    Registered User
    Join Date
    03-04-2016
    Location
    Luxembourg
    MS-Off Ver
    MS Office 2013
    Posts
    3

    Re: counting months between dates and returning a decimal

    Hello Claudster,

    Great formula! It works fantastically when the gap between the two dates is over a month. Whereas when it is lower than a month the result is always 1 (month) higher than is should be. I have attempted to correct it but been unsuccessful, could you please help me to correct it? Below shows a small copy paste out of my spreadsheet where you can see the result of 1.87 is exactly one month too much.

    Many thanks in advance,
    Jake

    From: 05 April 2016
    To: 30 April 2016
    Months: 1.87

  8. #8
    Registered User
    Join Date
    03-04-2016
    Location
    Luxembourg
    MS-Off Ver
    MS Office 2013
    Posts
    3

    Re: counting months between dates and returning a decimal

    Just in case someone else needs this solution I fixed it by using the following formula:

    =IF(MONTH(D2)=MONTH(D3),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))))

    Where the start date is D2 and the end date is D3.

  9. #9
    Registered User
    Join Date
    03-15-2016
    Location
    Not given
    MS-Off Ver
    2013
    Posts
    1

    Re: counting months between dates and returning a decimal

    Hi Jake,

    May I know if this is applicable if it's between two dates more than one year?

  10. #10
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: counting months between dates and returning a decimal

    learnmoreexcel, welcome to the forum

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  11. #11
    Registered User
    Join Date
    03-04-2016
    Location
    Luxembourg
    MS-Off Ver
    MS Office 2013
    Posts
    3
    Quote Originally Posted by learnmoreexcel View Post
    Hi Jake,

    May I know if this is applicable if it's between two dates more than one year?
    Yes it is, I hope it works for you.

  12. #12
    Registered User
    Join Date
    05-01-2015
    Location
    NZ
    MS-Off Ver
    2013
    Posts
    1

    Re: counting months between dates and returning a decimal

    Quote Originally Posted by Claudster View Post
    The below formula is ugly but it works!!!!.... all in one cell, no macro needed! It takes in to account each month's days regardless if it is has 30, 31, or in the instance of February 28 days. It also takes into account leap year where February has 29 days.

    A1 = Start Date
    B2 = Last Date

    =(IF(MONTH(A1)=MONTH(B1),(YEAR(B1)-YEAR(A1))*11,((YEAR(B1)-YEAR(A1))*12)-12+(12-MONTH(A1))+MONTH(B1)-1))+(EOMONTH(A1,0)-A1+1)/DAY(EOMONTH(A1,0))+(1-(EOMONTH(B1,0)-B1)/DAY(EOMONTH(B1,0)))

    Try it! It works!
    Awesome work, I am sure I've had a solution to this before, but this was EXACTLY what i was looking for today. Why reinvent the wheel - Excelforum is an awesome place to look for solutions! Cheers Claudster! Happy Easter!
    Last edited by MrBoris173; 03-24-2016 at 08:33 PM.

  13. #13
    Registered User
    Join Date
    07-23-2017
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    2

    Re: counting months between dates and returning a decimal

    Thanks for the previous contributors, much appreciated. You have allowed me to determine solutions for determination of months as a fraction between two dates. Below is my contribution.

    E28 is the finish date and
    D28 is the start date

    The number of months as a fraction is given by the following;

    This method evaluates the difference between 2 dates and expresses this difference as fractions of a month. Note: This method is not inclusive as number of days does not include the start date. For example, if we determine the number of days between the 4th July and the 21st July an inclusive method would result in 18 days whereas a difference method would result in 17 days. Hence the fraction of the month would be 17 / 31 = 0.5484. Also as the number of days vary in a month (28 to 31), this method will produce unusual results e.g. if the start date is the 28/08/2014 and the finish date is 28/02/2015 then the calculated number of months is 6.0950 not 6. This is due to August having 31 days and February having 28 days. 0.0950 = 27.5 / 28 - 27.5 / 31.

    If you wish to have the time of the dates at midday then the formula is

    = (YEAR(E28) -YEAR(D28)) * 12 + MONTH(E28) - MONTH(D28) + (DAY(E28) - 0.5) / DAY((EOMONTH(E28,0))) - (DAY(D28) - 0.5) / DAY((EOMONTH(D28,0)))

    If you wish to have the time of the dates at midnight AM then the formula is

    = (YEAR(E28) -YEAR(D28)) * 12 + MONTH(E28) - MONTH(D28) + (DAY(E28) - 1.0) / DAY((EOMONTH(E28,0))) - (DAY(D28) - 1.0)/DAY((EOMONTH(D28,0)))

    If you wish to have the time of the dates at midnight PM then the formula is

    = (YEAR(E28) -YEAR(D28)) *12 + MONTH(E28) - MONTH(D28) + (DAY(E28) - 0) / DAY((EOMONTH(E28,0))) - (DAY(D28) - 0) / DAY((EOMONTH(D28,0)))

    Note the variation in result between the above three methods is small.

    Explanation
    The first two portions of the formula (see below) determine the number of months at the beginning of the two months of concern. This is simply the number of years times 12 and the difference of the number of months. This portion works correctly even if the difference in months is either positive, zero or negative.

    (YEAR(E28) -YEAR(D28)) * 12 + MONTH(E28) - MONTH(D28)

    The third portion of the formula (see below) determines the period from the beginning of the "finish date month" to the finish date. The "Finish Date" occurs outside the period of the beginnings of the "Start Date" and "Finish Date" months and hence the fraction of the month has to be added. The reason for the -0.5 is as follows; 0.5 represents midday, hence if the Start Date is the fourth day of the month then 3.5 days have passed since the beginning of the month and hence the 0.5 has to be subtracted from the month's day number.

    + (DAY(E28) - 0.5) / DAY((EOMONTH(E28,0))).

    The fourth portion of the formula (below) determines the period from the beginning of the "start date month" to the start date. The "Start Date" occurs within the period of the beginnings of the "Start Date" and "Finish Date" months and hence the fraction of the month has to be trimmed. The inclusion of the -0.5 is the same as per above.

    - (DAY(D28) - 0.5) / DAY((EOMONTH(D28,0)

    Simplified method

    A simplified method (see below) is to determine the number of days and convert them into months by multiplying by 12 and dividing by 365.25, the average number of days in a year. This method has an error of the order of 1%.

    = DATEDIF(D28,E28,"d") * 12 / 365.25

  14. #14
    Registered User
    Join Date
    08-14-2017
    Location
    Indianapolis, IN, USA
    MS-Off Ver
    2013
    Posts
    1

    Re: counting months between dates and returning a decimal

    The same formulas are not working for me. Any thoughts?

    Start 8/27/2017
    End 6/26/2018
    months 10.03

  15. #15
    Registered User
    Join Date
    07-23-2017
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    2

    Re: counting months between dates and returning a decimal

    Not sure which formula you are using.
    Using justjakehog's formula I get 10.0280.
    Using my method I get 9.9952. Justjakehog formula is an inclusive method i.e. includes the start date whereas mine does not. Hence an additional day is approx 1/30 of month or 0.0333 of a month.

  16. #16
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: counting months between dates and returning a decimal

    Quote Originally Posted by aandrews1122 View Post
    The same formulas are not working for me. Any thoughts?

    Start 8/27/2017
    End 6/26/2018
    months 10.03
    heres a thought - Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

  17. #17
    Registered User
    Join Date
    09-18-2017
    Location
    Adelaide, Australia
    MS-Off Ver
    2014
    Posts
    1

    Re: counting months between dates and returning a decimal

    Thanks justjakehogan - this worked for me.

    Quote Originally Posted by justjakehogan View Post
    Just in case someone else needs this solution I fixed it by using the following formula:

    =IF(MONTH(D2)=MONTH(D3),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))))

    Where the start date is D2 and the end date is D3.

  18. #18
    Registered User
    Join Date
    09-30-2012
    Location
    Phnom Penh
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: counting months between dates and returning a decimal

    it shows the result '#NUM!' when use [=datedif(B1,A1,"m"]

    started date [A1] : 01/09/2017
    end date [B1] : 28/09/2017

    WHY?

  19. #19
    Registered User
    Join Date
    07-03-2013
    Location
    Russia
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: counting months between dates and returning a decimal

    Quote Originally Posted by justjakehogan View Post
    Just in case someone else needs this solution I fixed it by using the following formula:

    =IF(MONTH(D2)=MONTH(D3),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))))

    Where the start date is D2 and the end date is D3.
    This method won't work properly. I have tried with several dates and it shows incorrect amount of months.

    I made my own algorithm, here it is:
    I used Defined Names for easy debugging.
    Cell B2 - Start date
    Cell B3 - End date

    Lease_IntMonths = DATEDIF( B2; B3+1; "m" )
    Calculate an integer amount of months between Start and End dates.

    NewEndDate = EDATE(B2; Lease_IntMonths )
    Add the amount of months to the original Start date and get a new End date.

    Lease_DaysInLastMonth = B3+1 - NewEndDate
    Calculate how many days are in the last month of the period.

    CalendarDaysInLastMonth = DAY( EOMONTH( NewEndDate; 0 ) )
    Calculate the amount of calendar days in the last month of the period.

    =Lease_IntMonths + IF( Lease_DaysInLastMonth > 0; Lease_DaysInLastMonth / CalendarDaysInLastMonth; 0 )
    Calculate the final integer part of the amount of months plus a fraction.

  20. #20
    Registered User
    Join Date
    08-18-2020
    Location
    Houston
    MS-Off Ver
    office 365
    Posts
    1

    Re: counting months between dates and returning a decimal

    I just learned about yearfrac. Try: =YEARFRAC(cell_of_start_date,cell_of_end_date)*12
    Last edited by rwagner88; 08-18-2020 at 01:15 PM. Reason: error corrected

  21. #21
    Registered User
    Join Date
    10-06-2020
    Location
    PAKISTAN
    MS-Off Ver
    OFFICE 365
    Posts
    1

    Re: counting months between dates and returning a decimal

    its excellent its really work. But their is one problem
    it didnit count date from 1jan to 31 jan as one month it calculate it as 2 month so please check the problem

    your reply will be appreciated

    Thanks alot....

  22. #22
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: counting months between dates and returning a decimal

    Quote Originally Posted by ALNORAINAHMAD View Post
    its excellent its really work. But their is one problem
    it didnit count date from 1jan to 31 jan as one month it calculate it as 2 month so please check the problem

    your reply will be appreciated

    Thanks alot....
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

  23. #23
    Registered User
    Join Date
    09-12-2023
    Location
    Austin, TX
    MS-Off Ver
    365
    Posts
    1

    Re: counting months between dates and returning a decimal

    Quote Originally Posted by justjakehogan View Post
    Just in case someone else needs this solution I fixed it by using the following formula:

    =IF(MONTH(D2)=MONTH(D3),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))),((YEAR(D3)-YEAR(D2))*12)-12+(12-MONTH(D2))+MONTH(D3)-1+(EOMONTH(D2,0)-D2+1)/DAY(EOMONTH(D2,0))+(1-(EOMONTH(D3,0)-D3)/DAY(EOMONTH(D3,0))))

    Where the start date is D2 and the end date is D3.

    I came across this thread after developing a similar, albeit, simplified solution. Try this and you should get the same result:

    =DATEDIF(EOMONTH(D2,0)+1,EOMONTH(D3,0)+1,"m")+(DAY(D3)/DAY(EOMONTH(D3,0)))-((DAY(D2)-1)/DAY(EOMONTH(D2,0)))

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1