Results 1 to 11 of 11

Thread: [RESOLVED] Date difference

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Resolved [RESOLVED] Date difference

    Hi everyone!

    I've read somewhere a code for getting the difference between two dates...can't remember it, does anyone know how to do?

    For example; if you have one date (2006-05-04) and another date (2006-05-09) then the result should be 5.
    Now this is an easy example that's very easy to build a code for like:

    VB Code:
    1. result = Right(date2, 2) - Right(date1, 2)

    But this code that I've seen works even for dates that crosses months like 2006-05-28 and 2006-06-12.

    The code I've seen was a built in function in VB...does anyone know what it's called?

    Happy for help!

    //Alex
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  2. #2

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Date difference

    and if you want to calculate the days you can just subtract:
    VB Code:
    1. Result = date2 - date1

  4. #4
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Date difference

    Just remember that all dates are just numbers to the computer, and you can get that number by converting to a long, using CLNG()
    Don't pay attention to this signature, it's contradictory.

  5. #5

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Unhappy Re: Date difference

    If you use:

    VB Code:
    1. result = "2006-06-10" - Date

    you get a mismatch error.

    The DateDiff code doesn't either work for me...am I using it wrong?
    If I write this:
    VB Code:
    1. result = DateDiff(, "2006-06-10", Date)

    I get an "Argument not optional" error.

    Can anyone help?
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Re: Date difference

    That's right RhinoBull...got lots of errors trying that to.... what am I going to do!?!?
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Unhappy Re: Date difference

    even this gives a mismatch error:

    VB Code:
    1. MsgBox CDbl("2006-06-10") - CDbl(Date)

    Plz help me!
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Lightbulb Re: Date difference

    I've got it!!

    The trick is that you have to declare the "future date" before using it:

    VB Code:
    1. Dim thedate As Date
    2. thedate = "2006-06-10"
    3. result = DateDiff("d", date, thedate)

    Tnx for the tip about DateDiff...I'll reputate at once!
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date difference

    Quote Originally Posted by cyber_alex
    If you use:

    VB Code:
    1. result = "2006-06-10" - Date

    you get a mismatch error...
    That's because first argument is string and second is date - you need to convert firt to date:

    result = CDate("2006-06-10") - Date

    But I still say DateDiff() is better (safer that is).

  11. #11
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Date difference

    Quote Originally Posted by RhinoBull
    Nooooooooooooooooooooooooo...

    Dates are stored as Double so converting it to Long may cause some problem(s), are you kidding ?
    Sorry, I meant you can get the number of days by converting to a long. Usually you don't want the hours, minutes and seconds.
    Don't pay attention to this signature, it's contradictory.

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