Results 1 to 6 of 6

Thread: Subtracting 2 Dates

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    7

    Subtracting 2 Dates

    Could someone help me how to subtract 2 Dates?

    The format of the Date is "mm/d/yyyy"

    Thanks.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Subtracting 2 Dates

    Dates do not have a format, only Strings (which includes tooltips for variables in the VB editor) have a format. Are you using Strings for this, or are they actually Dates?

    Also, given two dates (perhaps tomorrow and yesterday) and what kind of result are you after?

  3. #3
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: Subtracting 2 Dates

    Declare three double variables.
    Convert your string to date.
    Convert date to double.
    Subtract the two.

    Dim D1 as double, D2 as double
    Dim DDiff as double
    D1 = cdbl(cdate("01/01/2011")
    D2 = cdbl(cdate("12/18/2010")
    DDiff = D1 - D2

  4. #4
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: Subtracting 2 Dates

    Sorry I forgot the rightmost parenthesis

    Dim D1 as double, D2 as double
    Dim DDiff as double
    D1 = cdbl(cdate("01/01/2011"))
    D2 = cdbl(cdate("12/18/2010"))
    DDiff = D1 - D2

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Subtracting 2 Dates

    Why not just use the DateDiff function?

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

    Re: Subtracting 2 Dates

    Quote Originally Posted by baja_yu View Post
    Why not just use the DateDiff function?
    Why recreate the wheel. This is what I would suggest too
    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

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