Results 1 to 4 of 4

Thread: Calculating Date and Time

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Calculating Date and Time

    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

  2. #2
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    You have to use the DateDiff function. Look into your VB-Help to read more.

    Good Luck, Matt-D

  3. #3
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    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

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    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:
    1. Public Function WeekDayDifference(Date1 As Date, Date2 As Date) As Integer
    2. Dim CountofDays As Integer
    3.  
    4. CountofDays = 0
    5. Do While Date1 < Date2
    6.      If Format(Date1, "ddd") = "Sat" Or Format(Date1, "ddd") = "Sun" Then
    7.           Date1 = DateAdd("d", 1, Date1)
    8.      Else
    9.           Select Case Date1
    10.                Case #7/1/02#
    11.                    Date1 = DateAdd("d", 1, Date1)
    12.                Case #8/5/02#
    13.                     Date1 = DateAdd("d", 1, Date1)
    14.                Case #9/2/02#
    15.                     Date1 = DateAdd("d", 1, Date1)
    16.                Case #10/14/02#
    17.                     Date1 = DateAdd("d", 1, Date1)
    18.                Case #11/11/02#
    19.                     Date1 = DateAdd("d", 1, Date1)
    20.                Case #12/25/02#
    21.                     Date1 = DateAdd("d", 1, Date1)
    22.                Case #12/26/02#
    23.                     Date1 = DateAdd("d", 1, Date1)
    24.                Case Else
    25.                     Date1 = DateAdd("d", 1, Date1)
    26.                     CountofDays = CountofDays + 1
    27.           End Select
    28.      End If
    29. Loop
    30. Storage CountofDays
    31. WeekDayDifference = CountofDays
    32. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width