Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DATEDIFF in VB.Net 1

Status
Not open for further replies.

kermitforney

Technical User
Mar 15, 2005
374
US
Trying to get this to run correctly in VB.NET (code behind ASP.Net). Something is off with the Syntax and I am not sure what it is. Any suggestions??

Code:
intDateDiff = Me!txtToDate - Me!txtFromDate
      If intDateDiff > 84 Or (intDateDiff Mod 7) <> 0 Then
        MsgBox "The difference between the From and To dates must be no greater than 12 weeks."
 
Try this.

Code:
If IsDate(TextBox1.Text) And IsDate(TextBox2.Text) Then
                Dim dt1 As DateTime = DateTime.Parse(TextBox1.Text)
                Dim dt2 As DateTime = DateTime.Parse(TextBox2.Text)
                Dim days As Int16 = DateDiff("d", dt1, dt2)
                Label1.Text = "Date Interval is: " & days.ToString & " days."
            Else
                Label1.Text = "Date interval not calculated: Input boxes must contain valid dates."
            End If

The risk with keeping an open mind is having your brains fall out.
Shaunk

 
Actually, what I need is to compare the DateDiff between the startdate text box and the enddate text box. If the difference is greater than 84 days or not divisible by 7 (. . Mod <> 7), then I would like it to error out.
 
use a custom field validator and define the logic in the servervalidate event handler.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Can't I accomplish this in VB via Code Behind?? I am not familiar with ASP.Net.
 
I am not familiar with ASP.Net
but your using asp.net, at that's implied because this is the asp.net forum.
Can't I accomplish this in VB via Code Behind??
you are because you will wire the validation logic to the control via a custom field validator.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I apologize for the mispost, but I wasn't sure where to post this. :)

I will look into custom field validation.

Thanks, Jmeck.
 
I think the following code-behind meets your requirement, but the validation is better placed in a customvalidator control. I prefer client-side Javascript for custom validation logic that doesn't require database access, because it avoids a round-trip to the server.
A brief and crude comparison of client-side validation versus server-side validation is:
"Client side validation is fast but vunerable to security risks and can be disabled by a hacker. Server-side validation is slow but safe."

Code:
If IsDate(StartDate.Text)And IsDate(EndDate.Text) Then
 Dim StartRange As DateTime = DateTime.Parse(StartDate.Text)
 Dim EndRange As DateTime = DateTime.Parse(EndDate.Text)
 Dim days As Int16 = DateDiff("d", StartRange, EndRange)
If days Mod 7 <> 0 Or days > 84 Then
    Response.Write("Perform your error routine")
End If
 Label1.Text = "Date Interval is: " & days.ToString & " days."
 Else
   Label1.Text = "Date interval not calculated: Input  boxes must contain valid dates."
End If

The risk with keeping an open mind is having your brains fall out.
Shaunk

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top