calculate timespan (days) between 2 dates from timepicker in a class

Marie-Andree

Member
Joined
Sep 9, 2015
Messages
8
Programming Experience
Beginner
Hello,
I am trying to pass 2 variables check in date and check out date from 2 different date time picker on a form. The goal is two calculate the number of day between the 2 dates.

Here is what I have so far:
From the form:

Code:
[COLOR=#00008B]Public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Class[/COLOR][COLOR=#000000] FormReservations

[/COLOR][COLOR=#808080]'Declare the new objects:[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#000000] TheRegularPriceObject [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] ClassRegularPrice


[/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] ButtonCalculateRate_Click[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]sender [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Object[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] e [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] EventArgs[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Handles[/COLOR][COLOR=#000000] ButtonCalculateRate[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Click

TheRegularPriceObject [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]New[/COLOR][COLOR=#000000] ClassRegularPrice[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]DateTimePickerCheckIn[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Value[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"dd/MM/yyyy"[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#000000]DateTimePickerCheckOut[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Value[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"dd/MM/yyyy"[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]




        LabelNumberOfDay[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Text [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] TheRegularPriceObject[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]CalculateNumberOfDay[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ToString[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]


[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub

[COLOR=#222426][FONT=Arial]End Class

[/FONT][/COLOR]

This my class:

Code:
[COLOR=#00008B]Public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Class[/COLOR][COLOR=#000000] ClassRegularPrice



[/COLOR][COLOR=#00008B]Protected[/COLOR][COLOR=#000000] CheckInDateVar [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#00008B]Protected[/COLOR][COLOR=#000000] CheckOutDateVar [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Protected[/COLOR][COLOR=#000000] NumberOfDaySpan [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#808080]'Constructor: [/COLOR][COLOR=#000000]

[/COLOR][COLOR=#00008B]Public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]New[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] CheckInDateConst [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] CheckOutDateConst [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]

CheckInDate [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CheckInDateConst
CheckOutDate [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CheckOutDateConst
CalculateNumberOfDay[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#00008B]Property[/COLOR][COLOR=#000000] CheckInDate[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Get[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] CheckInDateVar
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Get[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Set[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] Value [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        CheckInDateVar [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Value
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Set[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Property[/COLOR][COLOR=#000000]


[/COLOR][COLOR=#00008B]Property[/COLOR][COLOR=#000000] CheckOutDate[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Get[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] CheckOutDateVar
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Get[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Set[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] Value [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        CheckOutDateVar [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Value
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Set[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Property[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] 


 [/COLOR][COLOR=#808080]'Methods:[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] CalculateNumberOfDay[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]

    NumberOfDaySpan [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]CheckOutDateVar [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#000000] CheckInDateVar[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR]
It is not returning a value and I'm not sure I converted my date to string also, but this is what I have been told to do. I am not sure from there how the calculation should work since this is a string... Any help would be greatly appreciated.:)
[/COLOR]
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
Firstly, you've made your code harder for us to read. In future, please post it as plain text like so:

[xcode=vb]your code here[/xcode]
That will add appropriate syntax highlighting.

Don't convert your Dates to Strings. Strings are for text. Dates are not text. The ONLY reason to convert a Date to a String is for display or serialisation. If you want to perform a calculation then do it on binary Dates, NOT text. To calculate the number days between the dates selected in two DateTimePickers you would do this:
Dim startDate As Date = startDatePicker.Value.Date
Dim endDate As Date = endDatePicker.Value.Date
Dim time As TimeSpan = endDate - startDate
Dim days As Integer = time.Days
The Value property of the DateTimePicker gives you a DateTime (a .NET DateTime and a VB.NET Date are the same thing) and getting the Date property of that gives you another DateTime with the same date portion and the time portion zeroed.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
Sub Procedures (Visual Basic)
A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.
Function Procedures (Visual Basic)
A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code.
 
Top Bottom