Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Date Subtraction

Posted on 2000-02-22
Medium Priority
544 Views
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
0
Question by:dsteers
• 4
• 3

LVL 70

Accepted Solution

Éric Moreau earned 200 total points
ID: 2548740
DateDiff do it very easily.

msgbox datediff("d","march 2 2000", "april 4 2000")
0

Expert Comment

ID: 2557165
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

0

LVL 70

Expert Comment

ID: 2558573
pinshah,

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

dsteers,

Is it working now?
0

Author Comment

ID: 2560192
Think so,
I'll try it out
Thanks
0

Author Comment

ID: 2563685
Why Copy emoreau's Comment and post it as an answer.
Sorry
0

Author Comment

ID: 2563693
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
0

LVL 70

Expert Comment

ID: 2564861
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
0

Author Comment

ID: 2571108
I'll Try it out.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll

#### 963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.