Link to home
Start Free TrialLog in
Avatar of dsteers
dsteers

asked on

Date Subtraction

I wish to enter a day date (e.g. 15) and have it subtracted by
another date that is also entered (in the same format).
It's a normal subtraction except when it goes over months.

eg.      2 March
      4 April
      -------
      33 Days      

P.S I'm using VB 6
Thanks
Dave Steers
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pinshah
pinshah

if u have two dates use
datediff("d","march 2 2000", "april 4 2000")

else if u want to subtract or add days/months etc

use DateAdd
pinshah,

You steal my comment! Change it to a comment now.

dsteers,

Is it working now?
Avatar of dsteers

ASKER

Think so,
I'll try it out
Thanks
Avatar of dsteers

ASKER

Why Copy emoreau's Comment and post it as an answer.
Sorry
Avatar of dsteers

ASKER

Thanks emoreau,
Great help,
also (for another 50 points),
I was wondering if there is some sort of code that would allow subtraction of time,where the times are over two different days.
E.g. 1340 (PM) Wednesday 8 March
     0200 (AM) Thursday 9 March

The Code saying how many days,hours minutes between the two times.

Thanks

Dave Steers
Try this:

Option Explicit

Private Sub Command1_Click()
Dim dtm1 As Date
Dim dtm2 As Date
Dim lngDiff As Long
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long

    dtm1 = "2000/03/08 13:40"
    dtm2 = "2000/03/09 2:00"
   
    'Returns number of minutes
    lngDiff = DateDiff("n", dtm1, dtm2)
   
    'Extract days
    If lngDiff > (24 * 60) Then
        lngDays = Int(lngDiff / (24 * 60))
        lngDiff = lngDiff - (lngDays * 24 * 60)
    End If
   
    'Extract Hours
    If lngDiff > 60 Then
        lngHours = Int(lngDiff / 60)
        lngDiff = lngDiff - (lngHours * 60)
    End If
   
    'Extract Minutes
    lngMinutes = lngDiff
   
    MsgBox "Difference between dates: " & _
           lngDays & " days, " & _
           lngHours & " hours and " & _
           lngMinutes & " minutes."
End Sub
Avatar of dsteers

ASKER

I'll Try it out.