+ Reply to Thread
Results 1 to 6 of 6

Datedif incorrect month count - February problem??

  1. #1
    Registered User
    Join Date
    12-06-2005
    Posts
    3

    Datedif incorrect month count - February problem??

    Thought I was losing my mind when I set up some conditional formatting based on datedif # of months. The number of months between the start date and the end of January and the end of February (using eomonth function) were the same. To double check - I input the following functions manually inserting the date.

    =DATEDIF("11/30/2005","02/28/2006","M")
    Answer = 2

    =DATEDIF("11/30/2005","01/31/2006","M")
    Answer = 2

    I've used upper and lower case M with no difference. Any thoughts? I've been using the datedif function for some forecasting and I'm a bit concerned with this problem.

    Thanks, JMK

  2. #2
    Gary L Brown
    Guest

    RE: Datedif incorrect month count - February problem??

    After some experimentation, I noted that the DateDif function uses the
    month-end date of the start month to decide if a month has passed.
    In otherwords, if the start date has 31 days, Apr/Jun/Sep/Nov all have
    issues if your start date is the 31st.

    Only Feb has issues if your start date is the 30th.

    Feb does NOT have problems if the year is a leap year and the start date is
    the 29th.

    No months have issues if your start date is less than or equal to the 28th.

    HTH,
    --
    Gary Brown
    gary_brown@ge_NOSPAM.com
    If this post was helpful, please click the ''Yes'' button next to ''Was this
    Post Helpfull to you?''.


    "JMKCT" wrote:

    >
    > Thought I was losing my mind when I set up some conditional formatting
    > based on datedif # of months. The number of months between the start
    > date and the end of January and the end of February (using eomonth
    > function) were the same. To double check - I input the following
    > functions manually inserting the date.
    >
    > =DATEDIF("11/30/2005","02/28/2006","M")
    > Answer = 2
    >
    > =DATEDIF("11/30/2005","01/31/2006","M")
    > Answer = 2
    >
    > I've used upper and lower case M with no difference. Any thoughts?
    > I've been using the datedif function for some forecasting and I'm a bit
    > concerned with this problem.
    >
    > Thanks, JMK
    >
    >
    > --
    > JMKCT
    > ------------------------------------------------------------------------
    > JMKCT's Profile: http://www.excelforum.com/member.php...o&userid=29394
    > View this thread: http://www.excelforum.com/showthread...hreadid=491085
    >
    >


  3. #3
    Ron Rosenfeld
    Guest

    Re: Datedif incorrect month count - February problem??

    On Tue, 6 Dec 2005 09:31:41 -0600, JMKCT
    <JMKCT.1zmkcb_1133883306.6132@excelforum-nospam.com> wrote:

    >
    >Thought I was losing my mind when I set up some conditional formatting
    >based on datedif # of months. The number of months between the start
    >date and the end of January and the end of February (using eomonth
    >function) were the same. To double check - I input the following
    >functions manually inserting the date.
    >
    >=DATEDIF("11/30/2005","02/28/2006","M")
    >Answer = 2
    >
    >=DATEDIF("11/30/2005","01/31/2006","M")
    >Answer = 2
    >
    >I've used upper and lower case M with no difference. Any thoughts?
    >I've been using the datedif function for some forecasting and I'm a bit
    >concerned with this problem.
    >
    >Thanks, JMK


    Your observations are correct and congruent with my own about the limitations
    of DATEDIF.

    Here is a UDF that I've devised to try to work around this problem. It gives a
    choice of two possible outputs, depending on the setting of the optional
    FracMonth argument.

    FracMonth = FALSE then output full calendar months + the extra days in the
    beginning and ending month. This can result in outputs such as 2 months 45
    days.

    FracMonth = TRUE then output full calendar months + a fraction computed based
    on the excess days in the first and last months.

    A Calendar month is a month that includes both the first and last days of the
    month.

    The earliest date is not counted.

    Given your data, the results would be:

    3 months for the first and 2 months for the second set of dates you show.

    ==============================================
    Function CalendarMonths(d1 As Date, d2 As Date, _
    Optional FracMonth As Boolean = False)
    'FracMonth --> output as Month+fraction of months based on
    ' days in the starting and ending month
    'Without FracMonth, output is in years, full calendar months, and days

    Dim Temp As Date
    Dim i As Double
    Dim yr As Long, mnth As Long, dy As Long
    Dim FirstFrac As Double, LastFrac As Double
    Dim Yrstr As String, Mnstr As String, Dystr As String
    Dim NegFlag As Boolean

    NegFlag = False
    If d1 > d2 Then
    NegFlag = True
    Temp = d1
    d1 = d2
    d2 = Temp
    End If

    Temp = 0
    Do Until Temp >= d2
    i = i + 1
    Temp = EOM(d1, i)
    Loop

    If Temp <> d2 Then
    i = i - 1
    End If

    If FracMonth = True Then
    FirstFrac = (EOM(d1, 0) - d1) / Day(EOM(d1, 0))
    LastFrac = (d2 - EOM(d2, -1)) / Day(EOM(d2, 0))
    LastFrac = LastFrac - Int(LastFrac)
    CalendarMonths = i + FirstFrac + LastFrac
    If NegFlag = True Then CalendarMonths = -CalendarMonths
    Else
    yr = Int(i / 12)
    mnth = i Mod 12
    dy = d2 - EOM(d1, i) + (EOM(d1, 0) - d1)
    Yrstr = IIf(yr = 1, " yr ", " yrs ")
    Mnstr = IIf(mnth = 1, " month ", " months ")
    Dystr = IIf(dy = 1, " day", " days")
    CalendarMonths = yr & Yrstr & mnth & Mnstr & dy & Dystr
    If NegFlag Then CalendarMonths = "(Neg) " & CalendarMonths
    End If
    End Function
    =======================================


    --ron

  4. #4
    Registered User
    Join Date
    12-06-2005
    Posts
    3

    EOM Error?

    I've tried using the UDF, but I get a "sub ior function not defined at "Temp = EOM(d1, i)". Is there a reference I need to load in VBA for this?
    Thanks, JMK

  5. #5
    Peo Sjoblom
    Guest

    Re: Datedif incorrect month count - February problem??

    I believe Ron forgot to include another UDF called EOM (End Of Month) A
    quick google on Ron's name and EOM gives this


    -------------------------------------------------------
    Function EOM(DT As Date, mnths As Double) As Date
    Dim Day1ofDT As Date
    Dim temp As Date


    Day1ofDT = DT - Day(DT) + 1


    'add requisite number of months
    temp = DateAdd("m", mnths, Day1ofDT)


    'go to end of month


    EOM = temp + 32 - Day(temp + 32)


    End Function
    --------------------------------------------


    you can put that in the same module and now it should work (haven't tested
    it but I am sure it will)


    --

    Regards,

    Peo Sjoblom



    "JMKCT" <JMKCT.201bcz_1134571502.1057@excelforum-nospam.com> wrote in
    message news:JMKCT.201bcz_1134571502.1057@excelforum-nospam.com...
    >
    > I've tried using the UDF, but I get a "sub ior function not defined at
    > "Temp = EOM(d1, i)". Is there a reference I need to load in VBA for
    > this?
    > Thanks, JMK
    >
    >
    > --
    > JMKCT
    > ------------------------------------------------------------------------
    > JMKCT's Profile:

    http://www.excelforum.com/member.php...o&userid=29394
    > View this thread: http://www.excelforum.com/showthread...hreadid=491085
    >




  6. #6
    Registered User
    Join Date
    06-24-2011
    Location
    Raleigh, NC, USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Datedif incorrect month count - February problem??

    Hi -- I know this is a *really* old thread, but I'm struggling with errors on correct ages, especially for pediatric patients, using DateDif(). So, I tried the above with some wacky results for days calculations. See copy of a few below, this is a small portion of a list of 879 patients whose age includes >31 days. Anyone have any ideas?

    Age @ Cplt Dt Complete Date DOB
    1 yr 2 months 32 days 10/25/2014 7/24/2013
    14 yrs 0 months 40 days 3/19/2015 2/7/2001
    16 yrs 2 months 42 days 1/27/2015 10/16/1998
    15 yrs 5 months 58 days 6/29/2015 12/2/1999
    0 yrs 1 month 33 days 7/15/2014 5/13/2014
    0 yrs 0 months 50 days 9/26/2014 8/7/2014
    0 yrs 8 months 40 days 11/23/2014 2/11/2014
    10 yrs 9 months 44 days 10/19/2014 12/6/2003
    17 yrs 10 months 38 days 12/19/2014 1/12/1997
    1 yr 1 month 39 days 3/11/2015 1/3/2014
    19 yrs 4 months 44 days 8/20/2014 3/7/1995
    4 yrs 1 month 37 days 8/13/2014 6/6/2010
    15 yrs 6 months 33 days 7/30/2014 12/28/1998
    9 yrs 8 months 37 days 9/16/2014 12/10/2004
    0 yrs 9 months 42 days 10/16/2014 12/5/2013
    1 yr 4 months 33 days 12/13/2014 7/11/2013
    2 yrs 5 months 38 days 3/18/2015 9/10/2012
    3 yrs 0 months 36 days 3/29/2015 2/22/2012
    11 yrs 8 months 45 days 8/22/2014 11/7/2002
    16 yrs 11 months 38 days 5/22/2015 5/15/1998
    4 yrs 11 months 42 days 9/25/2014 9/13/2009
    16 yrs 11 months 53 days 9/28/2014 9/5/1997

    Update: I did not solve this problem, but did solve the DateDif() problem - it was not a problem with that part of the formula, but with one of the many conditions in the overall formula. Thanks!
    Last edited by ExcelOldTimer; 08-21-2015 at 03:27 PM. Reason: See edit. Thanks! :)

+ 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