Results 1 to 8 of 8

Thread: The Number of Years, Months and Days Between Two Dates

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    14
    Hi Rick,

    I fear you used an earlier version of my code.
    I amended it to perform the zero days difference calculation.
    It's so slight you can easily overlook it.

    Code:
    Function snb(x0 as string, x1 as string)
        x2 = DateDiff("m", x0, x1) - IIf(Day(x0) > Day(x1), 1, 0)
        c01 = x2 \ 12
        c02 = x2 Mod 12
        c03 = Day(x1) - Day(x0) + IIf(Day(x1) >= Day(x0), 0, Day(CDate(x1) - Day(x1)))
        snb = c01 & " year" & IIf(c01 = 1, ", ", "s, ") & c02 & " month" & IIf(c02 = 1, ", ", "s, ") & c03 & " day" & IIf(c03 = 1, "", "s")
    End Function
    Last edited by snb; 06-08-2012 at 09:49 PM.

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by snb View Post
    I fear you used an earlier version of my code.
    I amended it to perform the zero days difference calculation.
    It's so slight you can easily overlook it.
    Yes, I have been looking at the output from our formulas since early yesterday morning, so the odds are I grabbed your code before you edited it. Any thoughts on the other problem I raised (which both our codes exhibit)?

  3. #3
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    14
    Yes, it is no problem, unless you assume that a 'month' is a fixed quantity. Since it isn't it will produce odd results at first glance only.

    From 28 feb to 28 may is 4 months, until the 7th of june is 29,30,31 may + 7 days in june: 10 days in total
    From 01 march to the first of june is also 4 months (but comprising more days than the 3 months from 28 feb to 28 may).
    If you count in days, the difference must be 1, if you count in months & days, the result will differ dependent of the period in the year you are counting with.

    That's the nice thing about dates: it's a mixed 365,25 -number system, 12-number system, alternating 28,29,30,31-number system.

    So both our methods produce the correct results.

    Maybe a slight modification to make it more elegant:

    Code:
    Function snb(x0 As String, x1 As String)
        x2 = DateDiff("m", x0, x1) + (Day(x1) < Day(x0))
        c01 = x2 \ 12
        c02 = x2 Mod 12
        c03 = Day(x1) - Day(x0) -(Day(x1) < Day(x0)) * Day(CDate(x1) - Day(x1))
        snb = c01 & " year" & IIf(c01 = 1, ", ", "s, ") & c02 & " month" & IIf(c02 = 1, ", ", "s, ") & c03 & " day" & IIf(c03 = 1, "", "s")
    End Function
    Last edited by snb; 07-07-2012 at 09:27 PM.

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by snb View Post
    Yes, it is no problem, unless you assume that a 'month' is a fixed quantity. Since it isn't it will produce odd results at first glance only.

    From 28 feb to 28 may is 4 months, until the 7th of june is 29,30,31 may + 7 days in june: 10 day in total
    From 01 march to the first of june is also 4 months (but comprising more days than the 3 months from 28 feb to 28 may).
    If you count in days, the difference must be 1, if you count in months & days, the result will differ dependent of the period in the year you are counting with.

    That's the nice thing about dates: it's a miixed 365,25 -number system, 12-number system, alternating 28,29,30,31-number system.

    So both our methods produce the correct results.
    Yes, but of course you are right. When I quickly scanned the output list, the 4-day gap caught my eye and I didn't pay attention to the February/March switch-over. I am going to blame my confusion on not enough sleep and nowhere near enough coffee to compensate. Thanks for following up with this. Much appreciated.
    Last edited by Rick Rothstein; 06-08-2012 at 10:34 PM.

  5. #5
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    14
    A nice occurrence of a bug 'between the ears' instead of in the code ,,,,

Similar Threads

  1. Replies: 0
    Last Post: 09-17-2012, 02:05 PM
  2. How to Extracting dates and days between 2 dates.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 9
    Last Post: 08-11-2012, 09:11 PM
  3. Excel Datedif - Days of February
    By Excelfun in forum Excel Help
    Replies: 6
    Last Post: 06-10-2012, 02:32 PM
  4. Visual Plotter basis given dates and activity
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 2
    Last Post: 03-07-2012, 02:37 PM
  5. Number of Days In A Month
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-14-2011, 08:00 PM

Posting Permissions

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