# Date/Time

Given date 1, time 1 and date 2, time 2, how do i find the difference in minutes. Foe example, the total time between 15/05/2005, 5:25pm  and 16/05/2005, 7:15pm.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi,

In your SQL query statement, use the DateDiff() function.

If your Date1 & Time1, and Date2 & Time2 columns are separate [i.e 4 different columns] use:

SELECT DateDiff("n", Date1 + Time1, Date2 + Time2) FROM Your_Table

Otherwise, if Date1 contains a date & a time, and Date2 contains a date & a time, use:

SELECT DateDiff("n", Date1, Date2) FROM Your_Table

BFN,

fp.
EngineerCommented:
Hi asans,

Try this...

Private Sub Command1_Click()
Dim Date1 As Date, Date2 As Date
Date1 = "13/5/2005 12:41 PM"
Date2 = "12/5/2005 12:41 PM"
MsgBox Round((Date1 - Date2) * 24 * 60)
End Sub

Bye
---
Harish
EngineerCommented:
asans,
In VB each day is counted as one. But in one day, you will have 24*60 minutes. So you should multiply the difference with 24*60 or 1440

Hope this helps :)
High School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Or using DateDiff() without SQL:

Private Sub Command1_Click()
Dim DateA As Date, DateB As Date
Dim mins As Long
DateA = "5/15/2005 5:25 PM"
DateB = "5/16/2005 7:15 PM"
mins = DateDiff("n", DateA, DateB)
MsgBox "mins = " & mins
End Sub
Commented:
> Or using DateDiff() without SQL:

Yes, it makes a lot more sense to use a built in feature of the language than the odd kludge that mgh_mgharish posted.
Author Commented:
i forgot to mention, i have 4 textboxes for date 1, time 1, date 2 and time 2. I see something like
DateA = "5/15/2005 5:25 PM"
DateB = "5/16/2005 7:15 PM"
How do i link that to my 4 textboxes? In other words one textbox capture the date only and the other time. but from the above, the DateA, say, has both date and time. How do i link the 2 to have one variable like DateA.
Commented:
'try this

Private Sub Command1_Click()
Dim DateA as Date, DateB as Date

DateA = CDate(date1 & " " & time1)
DateB = CDate(date2 & " " & time2)

mins = DateDiff("n", DateA, DateB)

MsgBox "mins = " & mins

End Sub

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Now i want to use the date pickers.
If i try to use this

Private Sub Command3_Click()
Dim DateA As Date, DateB As Date

DateA = CDate(DTPicker1 & " " & DTPicker3)
DateB = CDate(DTPicker2 & " " & DTPicker4)

mins = DateDiff("n", DateA, DateB)
MsgBox "mins = " & mins

i get a type mismatch
where DTPicker 1 and 2 contain dates
and 3 and 4 contain time. What can i do ?
Commented:
My dear friend,

Date picker holds both date and time, so you need to extract date as separate and time as separate,

i am giving example try this.....

Private Sub Command3_Click()
Dim DateA As Date, DateB As Date

DateA = CDate(format(DTPicker1,"MMM/dd/yyyy") & " " & format(DTPicker3,"h:m:s"))
DateB = CDate(format(DTPicker2,"MMM/dd/yyyy") & " " & format(DTPicker4,"h:m:s"))

mins = DateDiff("n", DateA, DateB)
MsgBox "mins = " & mins

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.