Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the number of days between two dates / DateTimePickers

In my code below, how do I set up arrivaldate and depaturedate so I can get an amount due? I need to set them up as days stayed at the hotel so I can come up with a total. If this makes any sense? I am using datetimepickers in Visual Basic.

Public Class RentalForm
    'declare constants
    Const tax_rate_decimal As Decimal = 12.25D
    Const king_price_decimal As Decimal = 110.9D
    Const queen_price_decimal As Decimal = 105.9D
    Const double_price_decimal As Decimal = 95.9D

    'declare variables
    Private roomchargesumdecimal, taxamountsumdecimal, amountduesumdecimal As Decimal

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxnameofguest.TextChanged

    End Sub

    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        'dimension local variables
        Dim numberofguestsinteger As Integer
        Dim roomchargedecimal, taxamountdecimal, amountduedecimal, taxratedecimal As Integer
        Dim arrivaldate, departuredate As Date

        Try
            'dates
            arrivaldate = Now

            'convert quantity to numeric
            numberofguestsinteger = Integer.Parse(TextBoxNumberofguests.Text)

            'calculate values for single person
            roomchargedecimal = numberofguestsinteger * (arrivaldate + departuredate)
            taxratedecimal = roomchargedecimal * tax_rate_decimal   

        Catch ex As Exception

        End Try

    End Sub

    Private Sub DateTimePickerarrivaldate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerarrivaldate.ValueChanged

    End Sub

    Private Sub Label16averagelengthofstay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label16averagelengthofstay.Click


    End Sub

    Private Sub RentalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class              
like image 937
Ryan Harvell Avatar asked Sep 26 '13 00:09

Ryan Harvell


People also ask

How to get the number of days between two dates?

The DateTime allows you to subtract its object from another object of the same type. then You can make use of the .TotalDays function to get the number of days. between those dates. DateTime futurDate = Convert.ToDateTime ("08/21/2016"); DateTime TodayDate = DateTime.Now; var numberOfDays = (futurDate - TodayDate).TotalDays;

How to get the difference between two DateTime objects?

You can get the difference two DateTime objects by subtracting one from the other. P.S. Don't eat all exceptions like that. It's uncool. to calculate number of date intervals (days) betveen two dates.

How to do datetime math with DateTimePicker variables?

DateTime math can be confusing at first. But it doesn't really matter if they are DateTimePicker controls or variables because myDateTimePicker.Value is a DateTime Type. So, you can mix and match variables and controls such as Arrival as Now and Departure from a picker, and just use subtraction:

How do I calculate the difference between two dates in Excel?

Calculate the difference between two dates. 1 1. Use DATEDIF to find the total years. In this example, the start date is in cell D17, and the end date is in E17. In the formula, the “y” returns ... 2 2. Use DATEDIF again with “ym” to find months. 3 3. Use a different formula to find days. 4 4. Optional: Combine three formulas in one.


2 Answers

DateTime math can be confusing at first. But it doesn't really matter if they are DateTimePicker controls or variables because myDateTimePicker.Value is a DateTime Type. So, you can mix and match variables and controls such as Arrival as Now and Departure from a picker, and just use subtraction:

Dim arrivaldate As DateTime = DateTime.Now
Dim departuredate As DateTime = Me.DeparturePicker.Value

Dim DaysStayed as Int32 = departuredate.Subtract(arrivaldate).Days

The thing to remember is that the result is a TimeSpan object. If you look at the structure, you'll see it provides the time elapsed in units from Days to Ticks.

The code above plucks the Days value from the TimeSpan without creating a temp TimeSpan var. Another way:

Dim tsHotelStay = detarturedate.Value - arrivalDate
wholeDays = tsHotelStay.Days              ' e.g 7
totalDays = tsHotelStay.TotalDays         . e.g. 7.53
totalHrs = tsHotelStay.TotalHours         . eg 180.397

This time, the code does create a TimeSpan variable (tsHotelDay). Note that all the properties are available in whole and fractional forms (except Ticks).

Finally, the 2 subtraction methods shown (DateTime.Subtract(dt) and myTs = dtA - dtB) are functionally identical: both return a TimeSpan object.

like image 61
Ňɏssa Pøngjǣrdenlarp Avatar answered Sep 20 '22 00:09

Ňɏssa Pøngjǣrdenlarp


The DateTimePicker objects have a Value property, which will give you a DateTime for each. You can get the difference two DateTime objects by subtracting one from the other.

P.S. Don't eat all exceptions like that. It's uncool.

like image 25
Justin R. Avatar answered Sep 18 '22 00:09

Justin R.