DateDiff

Snookie

Active member
Joined
Dec 13, 2010
Messages
29
Programming Experience
Beginner
Hey..

I have this code...
VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dateDifference As Integer ' this variable keeps the difference between the dates.

        Dim todaydte As Date = Now()   'Current date'

        ' Checks if date is valid
        If (IsDate(TxtBox1.Text)) Then
            ' Get the difference between the dates (in days)
            dateDifference = DateDiff("d", TxtBox1.Text, todaydte)
            ' Date difference more than 7 days?
            If (dateDifference > 7) Then
                ' if its then displays error message'
                MessageBox.Show("Date is out of range", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
                'and form closes'
                Me.Close()
            Else
                'if the date is 7 days or less message box will say
                MessageBox.Show("Continue", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
                'next screen will show
                'form hides
                Form5.Show()
                Me.Hide()

            End If
        Else
            'if a date is not entered then this will display
            MessageBox.Show("Please input a valid date", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        End If

And i have a question..
I want to check the defrence between time
Time1" 19:02:04 " NowTime

And the time in textbox
Time2" 10:49:34 "

I want check i f time1 its more in 7 hour later in Time 2 then....
msgbox("Its 7 hour later")

Can some one rewrite me code so i can use it to check time and not day
 
Last edited by a moderator:
Comparing times is very simple:
VB.NET:
If TimeSpan1 > TimeSpan2 + TimeSpan.FromHours(7) Then
    'TimeSpan1 is more than 7 hours after TimeSpan2
End If
Where you get the TimeSpans from is irrelevant to the calculation itself. If you have a Date object then you can use its TimeOfDay property. If the user enters the value in a TextBox, the TimeSpan structure has Parse and TryParse methods.
 
Back
Top