How do i calculate date and time in vb. What i mean is i have a date 8/27/01 5:42 PM and 9/06/01 12:32 AM, how do i find out how many day,hours,min that is.
Thanks
Printable View
How do i calculate date and time in vb. What i mean is i have a date 8/27/01 5:42 PM and 9/06/01 12:32 AM, how do i find out how many day,hours,min that is.
Thanks
You have to use the DateDiff function. Look into your VB-Help to read more.
Good Luck, Matt-D ;)
mrstuff68,
try the following example:
create a form with 2 textboxes, 3 labels and 1 button.
Set Text1.Text = "StartDateTime" and Text2.Text = "EndDateTime"
Copy and paste the following sample code:
Private Sub Command1_Click()
Label1.Caption = DateDiff("s", Text1, Text2) & "seconds"
Label2.Caption = DateDiff("n", Text1, Text2) & "minutes"
Label3.Caption = DateDiff("d", Text1, Text2) & "days"
End Sub
Cheers, Roy
The following code was what I used to find out how many working days there were between to dates. You could modify it I guess if your looking for specifics.
VB Code:
Public Function WeekDayDifference(Date1 As Date, Date2 As Date) As Integer Dim CountofDays As Integer CountofDays = 0 Do While Date1 < Date2 If Format(Date1, "ddd") = "Sat" Or Format(Date1, "ddd") = "Sun" Then Date1 = DateAdd("d", 1, Date1) Else Select Case Date1 Case #7/1/02# Date1 = DateAdd("d", 1, Date1) Case #8/5/02# Date1 = DateAdd("d", 1, Date1) Case #9/2/02# Date1 = DateAdd("d", 1, Date1) Case #10/14/02# Date1 = DateAdd("d", 1, Date1) Case #11/11/02# Date1 = DateAdd("d", 1, Date1) Case #12/25/02# Date1 = DateAdd("d", 1, Date1) Case #12/26/02# Date1 = DateAdd("d", 1, Date1) Case Else Date1 = DateAdd("d", 1, Date1) CountofDays = CountofDays + 1 End Select End If Loop Storage CountofDays WeekDayDifference = CountofDays End Function