Results 1 to 17 of 17

Thread: [RESOLVED] How do you work out the difference between two dates.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Resolved [RESOLVED] How do you work out the difference between two dates.

    Basically I have two Date/Time Pickers within my program. One for Arrival and One for Departure.

    I need to work out the difference between the two dates selected so I can use them in a calculation later on.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    ''
    'Displaying Prices for Liverpool Pod
    If cboxPod.Text = "Liverpool" Then
    TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate - ArrivalDate))
    VAT.Text = ("£" & TotalPrice.Text * 0.2)
    Subtotal.Text = ("£" & TotalPrice.Text * 0.8)
    End If
    ''


    This is the code I currently have, I'm getting the error codes:

    ''
    Severity Code Description Project File Line Suppression State
    Error BC30452 Operator '-' is not defined for types 'DateTimePicker' and 'DateTimePicker'. GloriousGetaways C:\Users\THopw\Documents\Year 13\Computer Science\Glorious Getaways\GloriousGetaways\Forms\Bookings\BookAPod.vb 86 Active
    ''

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you work out the difference between two dates.

    There are a number of ways you could do this.
    Perhaps try this (I didn't test it)
    Code:
    TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Subtract(ArrivalDate).Days)
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    Code:
    TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate - ArrivalDate).Days)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    Thank you for your reply, It has given me the error 'Subtract is not a member of DateTimePicker'

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    Are DepartureDate and ArrivalDate DateTimePickers?

    Code:
    TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Value - ArrivalDate.Value).Days)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    @.paul. I was given this error


    Attachment 179860

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you work out the difference between two dates.

    Paul's code may work for you. (p.s. I guess not, as Paul assumed as I did that you were subtracting dates, not trying to subtract DateTimePickers).
    My code would need to be updated to use the Value of the DateTimePicker.
    Code:
    TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Value.Subtract(ArrivalDate.Value).Days)
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    Your attachment is not working. What type of control is People?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    People is NumericUpDown, although this is working

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    So what is the problem? You need to explain. It's not working doesn't give anyone a clue

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    The calculation is working, but without the difference between the two dates included within it. So at the minute, the program is only doing 9 * People.Value instead of 9 * People.Value * (ArrivalDate.Value - Departure Date.Value). If it means anything, this code is within a Private Sub cboxPod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxPod.SelectedIndexChanged, whether it should be placed somewhere else, I'm unsure

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    Does this give you the result that you're expecting?

    Code:
    TotalPrice.Text = ("£" & 9 * People.Value * Math.Abs((DepartureDate.Value - ArrivalDate.Value).Days))

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    If you look at DepartureDate and ArrivalDate, what do you expect the subtraction to be? also, where you calculate your totals is a good place for your code to be, whether that is a Button.Click or a ListBox.SelectedIndexChanged, etc... It depends whether you're doing running calculations, or all at the end.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    I want to do running calculations.

    For example: If I changed the ComboBox Input, the price would update. If I changed the NumericUpDown Value, the price would update. If I changed the Arrival or Departure Date, The price would update.

    In terms of the DateTimePickers, I would like the subtraction to be the Day the customer attends the Pod (Arrival Date) minus the Day the Customer Leaves the Pod (Departure Date) which will equal the number of days the customer is staying at the pod. This number will then be used to calculate the price.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you work out the difference between two dates.

    Use a multiple event handler, like this...

    Code:
    Private Sub doTotals(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, _
                                                                                                    People.ValueChanged, _
                                                                                                    DepartureDate.ValueChanged, _
                                                                                                    ArrivalDate.ValueChanged
    
        TotalPrice.Text = ("£" & 9 * People.Value * Math.Abs((DepartureDate.Value - ArrivalDate.Value).Days))
    End Sub

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do you work out the difference between two dates.

    Brilliant, That's worked perfectly. Thank you very much.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width