Results 1 to 15 of 15

Thread: [RESOLVED] DateDiff Month

  1. #1

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Resolved [RESOLVED] DateDiff Month

    Is there a way to make datediff for months show a decimal number? I need to find out partial dates.

    The user will enter a value such as 4/1 (mm/dd) and 12/5(mm/dd) and I need to find out the monthly value between it.

    I was thinking of counting the days and dividing by 30..but that wouldn't be very pretty.

    also weeks wouldnt work very well as sometimes theyre only partial weeks..
    Last edited by kfcSmitty; Aug 18th, 2005 at 12:43 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DateDiff Month

    So your trying to find a decimal for number of days or weeks?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: DateDiff Month

    Sorry, Guess I was unclear :/

    I want to find out the # of months in decimal form.


    So if a user entered "4/1" and "5/15" it would give me like 1.5 months or something along those lines

  4. #4

  5. #5

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: DateDiff Month

    Quote Originally Posted by MartinLiss
    What's wrong with dividing by 30 and formatting the output to the nearest tenth?

    If it comes down to that I will have to do it...But I am doing a calculation and i'm kinda being stingy on it, so the closer I can get the monthly difference value, the more accurate the calculation will be

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DateDiff Month

    It can get messy real fast with datediff on days then check the number of days in that month to calculate a decimal fraction of months. How accurate do you need it or the extent of what you want to do?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: DateDiff Month

    VB Code:
    1. MsgBox Format((DateDiff("d", "04/01/05", "05/15/05") / 30), "#.#")
    Given that a month may have anywhere from 28 to 31 days, I don't think you can be more accurate than that.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DateDiff Month

    Ok, you can do it in three parts. First calculate the datediff using the "m" identifier to get the whole number of months difference. Then second, you calculate the fractional portion with a datediff using the "d" identifier but for only the part that is after the whole complete number of months. Then third you can get that fractional part and convert it to decimal after you calculate the number of days in that ending month.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: DateDiff Month

    Accurate enough...Basically what I am doing, is I have an employees annual salary..say $74,132.32

    Then, they have splits..IE From "4/1" until "8/15" they will work 45% in department 03032

    So I need to take $74,132.32 and multiply it by 0.3 * (("8/11" - "4/1") / 12)

    actually...that might have just solved my problem...

  10. #10

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DateDiff Month

    Yes, easier but then you need to take into account weekends or work days only.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: DateDiff Month

    Quote Originally Posted by MartinLiss
    I think you should calculate the salary per day and multiply that by the total number of days worked.

    Thats how I am getting annual salary...but to get 15 different splits (its possible) and then figure out how many increases the user receives..and their anniversary (pay increase)....its a headache

    Guess I will just divide by 30..thanks to the both of ya

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] DateDiff Month

    Somthing like this. Fraction of a month isn't hard, but what I did was subtract the fraction of the first month, and added the fraction of the second month. Not sure if that's right, though.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim s As Date, t As Integer, u As Long, v As Long
    5.   Dim u1 As Long, v1 As Long
    6.   ' 2 months
    7.   t = DateDiff("m", Now, DateSerial(5, 10, 5)) ' months
    8.   ' u = days part
    9.   ' v = days in month
    10.   u = CDbl(DateDiff("d", DateSerial(Year(Now), Month(Now), 1), Now))
    11.   v = CDbl(Day(DateSerial(Year(Now), Month(Now) + 1, 0)))
    12.  
    13.   u1 = CDbl(10) ' second date days
    14.   v1 = CDbl(Day(DateSerial(5, 10 + 1, 0)))
    15.   ' 2 - 17/31 + 10/31 = 1.77 months
    16.   MsgBox Format(t - (u / v) + (u1 / v1), "0.##")
    17.  
    18. End Sub

  14. #14

  15. #15

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: [RESOLVED] DateDiff Month

    Quote Originally Posted by MartinLiss
    When dealing with salary you can calculate the daily salary, and calulating days worked is easy, so why would you want to do anything but what I previously suggested?

    Because I am not just calculating their work days. I have to do several different calculations for which department they would be in, or what % they have allocated to each program.

Posting Permissions

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



Click Here to Expand Forum to Full Width