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.    
asansAsked:
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.

fanpagesCommented:
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.
Harisha M GEngineerCommented:
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
Harisha M GEngineerCommented:
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 :)
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Mike TomlinsonHigh 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
PaulHewsCommented:
> 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.
asansAuthor 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.
veeru_friendCommented:
'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

Your issues matter to us.

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

Start your 7-day free trial
asansAuthor 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 ?
veeru_friendCommented:
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.