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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate no if days from 2 dates

Status
Not open for further replies.

696796

Programmer
Aug 3, 2004
218
GB
Code:
    Public Sub calculateDays()
        ' Calendar Days
        Dim dtpDateCarSent As New DateTime
        Dim dtpDateClosed As New DateTime
        Dim dys As Integer = dtpDateClosed.Date.Subtract(dtpDateCarSent.Date).Days
    End Sub

Hi, im trying to use the above code but it isnt working... I am trying to calculate the no of days between two dates that i choose on my form using a dateTimepicker control. So for example if dateCarSent is 01/01/2006 and dateCarClosed is 05/01/2006, the answer would be 4 days.

Any help much apreciated,

Al
 
got it...

Dim lngHowLong As Long
Dim date1 As Date = dtpDateCarSent.Text
Dim date2 As Date = dtpDateClosed.Text

lngHowLong = DateDiff("d", date1, date2)
MsgBox("There are " & lngHowLong & " days between the two dates.")
 
You should just be able to do this:
Code:
Dim dys As Integer = (dtpDateClosed.Subtract(dtpDateCarSent)).Days
This works because the subtraction method (operator in C#) returns a TimeSpan object, which has a Days property. The parenthesis help the compiler understand that you want the Days property of the result of the subtraction.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top